Azure service fabric 服务结构上的Asp.net Core 2.1

Azure service fabric 服务结构上的Asp.net Core 2.1,azure-service-fabric,asp.net-core-2.1,Azure Service Fabric,Asp.net Core 2.1,我正在将承载在Service Fabric中的Asp.net Core 2.0应用程序迁移到Asp.net Core 2.1。 服务结构还没有Asp.net Core 2.1的模板,因此 我遵循官方教程,但无法更改“Program.cs”文件,尤其是在非SF托管的应用程序中: namespace WebApp1 { public class Program { public static void Main(string[] arg

我正在将承载在Service Fabric中的Asp.net Core 2.0应用程序迁移到Asp.net Core 2.1。 服务结构还没有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>();
        }
    }
名称空间WebApp1
{
公共课程
{
公共静态void Main(字符串[]args)
{
CreateWebHostBuilder(args.Build().Run();
}
公共静态IWebHostBuilder CreateWebHostBuilder(字符串[]args)=>
WebHost.CreateDefaultBuilder(args)
.UseStartup();
}
}
在服务期间,使用织物

    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();
                    }))
            };
        }
    }
}
名称空间Web1
{
内部密封类Web1:无状态服务
{
公共Web1(无状态ServiceContext上下文)
:基本(上下文)
{ }
受保护的重写IEnumerable CreateServiceInstanceListeners()
{
返回新服务InstanceListener[]
{
新ServiceInstanceListener(serviceContext=>
新KestreCommunicationListener(serviceContext,“ServiceEndpoint”,(url,listener)=>
{
ServiceEventSource.Current.ServiceMessage(serviceContext,$“在{url}上启动Kestrel”);
返回新的WebHostBuilder()
.UseKestrel()
.配置服务(
服务=>服务
.AddSingleton(serviceContext))
.UseContentRoot(目录.GetCurrentDirectory())
.UseStartup()
.UseServiceFabricIntegration(侦听器,ServiceFabricIntegrationOptions.None)
.useURL(url)
.Build();
}))
};
}
}
}
如果不调用WebHost.CreateDefaultBuilder(args)主机文件环将无法按指定方式工作

我如何让它工作?
迁移到2.1 for Service Fabric是否为时过早?

您可以查看以了解需要添加的内容。 也许是这样的:

.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>();
   })
.ConfigureServices((hostingContext,services)=>
{
//退路
服务。后配置(选项=>
{
if(options.AllowedHosts==null | | options.AllowedHosts.Count==0)
{
//“AllowedHosts”:“localhost;127.0.0.1;[::1]”
var hosts=hostingContext.Configuration[“AllowedHosts”]?.Split(新[]{';'},StringSplitOptions.RemoveEmptyEntries);
//返回到“*”以禁用。
options.AllowedHosts=(hosts?.Length>0?hosts:new[]{“*”});
}
});
//变更通知
服务.AddSingleton(
新的ConfigurationChangeTokenSource(hostingContext.Configuration));
services.AddTransient();
})

是否需要使用命令行参数进行主机筛选?因为如果没有,而且我完全理解,您只需修改对
WebHost.CreateDefaultBuilder().ConfigureServices(…).UseStartup().UseServiceFabricIntergration(…).useURL(…).Build()的服务结构调用即可
。如果需要命令行参数,您可以从
Main(…)
Web1
类的ctor中传递它们。