首页 > 解决方案 > 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 是否为时过早?

标签: azure-service-fabricasp.net-core-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>();
   })

推荐阅读