azure-service-fabric - Service Fabric 上的 Asp.net Core 2.1
问题描述
我正在将托管在 Service Fabric 中的 Asp.net Core 2.0 应用程序迁移到 Asp.net Core 2.1。Service Fabric 还没有适用于 Asp.net Core 2.1 的模板,因此我按照此处的官方教程进行操作,但无法对“Program.cs”文件进行更改,尤其是在非 SF 托管应用程序中:
namespace WebApp1
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}
在 Service Fabric 中
namespace Web1
{
internal sealed class Web1 : StatelessService
{
public Web1(StatelessServiceContext context)
: base(context)
{ }
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
return new ServiceInstanceListener[]
{
new ServiceInstanceListener(serviceContext =>
new KestrelCommunicationListener(serviceContext, "ServiceEndpoint", (url, listener) =>
{
ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting Kestrel on {url}");
return new WebHostBuilder()
.UseKestrel()
.ConfigureServices(
services => services
.AddSingleton<StatelessServiceContext>(serviceContext))
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
.UseUrls(url)
.Build();
}))
};
}
}
}
如果不调用WebHost.CreateDefaultBuilder(args),主机归档将无法按此处指定的方式工作
我如何使它工作?迁移到 Service Fabric 2.1 是否为时过早?
解决方案
您可以查看源代码以查看需要添加的内容。也许是这样的:
.ConfigureServices((hostingContext, services) =>
{
// Fallback
services.PostConfigure<HostFilteringOptions>(options =>
{
if (options.AllowedHosts == null || options.AllowedHosts.Count == 0)
{
// "AllowedHosts": "localhost;127.0.0.1;[::1]"
var hosts = hostingContext.Configuration["AllowedHosts"]?.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
// Fall back to "*" to disable.
options.AllowedHosts = (hosts?.Length > 0 ? hosts : new[] { "*" });
}
});
// Change notification
services.AddSingleton<IOptionsChangeTokenSource<HostFilteringOptions>>(
new ConfigurationChangeTokenSource<HostFilteringOptions>(hostingContext.Configuration));
services.AddTransient<IStartupFilter, HostFilteringStartupFilter>();
})
推荐阅读
- node.js - 非法参数:数字、字符串
- ruby-on-rails - Rails Api 多对多如何发送和处理 json
- mysql - TypeORM 使用 ManyToOne 字段和 String 字段创建唯一约束
- android - WebRTC 远程流未在 Web 上显示
- airflow - Airflow v2 警告 No module named 'airflow.providers.mysql' 尽管没有使用这个模块
- javascript - 如何使用 python-requests 登录并向 .js url 发出 POST 请求
- javascript - React 重用组件而不是创建新组件,因此我必须依靠 componentWillReceiveProps 来加载新数据
- cucumber - 黄瓜测试失败 - java.lang.NullPointerException
- javascript - 节点子进程生成不执行命令
- python - 如何正确地将我的 tkinter 按钮与空格对齐