0
votes

I'm migrating an Asp.net Core 2.0 app hosted in Service Fabric to Asp.net Core 2.1. Service Fabric does not have yet a template for Asp.net Core 2.1, so I followed the official tutorial here, but was not able to do the changes to "Program.cs" file, in particular in a non SF hosted app:

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>();
        }
    }

while in 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();
                    }))
            };
        }
    }
}

Without calling WebHost.CreateDefaultBuilder(args) the Host filering does not work as specified here

How do I make it work? Is it too soon to migrate to 2.1 for Service Fabric?

1
Do you need to use command line args for host filtering? Because if not and I understand everything right you can simply modify your service fabric call to WebHost.CreateDefaultBuilder().ConfigureServices(...).UseStartup<Startup>().UseServiceFabricIntergration(...).UseUrls(...).Build(). In case command line args are required you can pass them in ctor of Web1 class from Main(...).Oleg Karasik

1 Answers

0
votes

You could have a look at the source code to see what you need to add. Maybe something like this:

.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>();
   })