Environment variables not being used when debugging through a Service Fabric project

前端 未结 3 1261
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-13 00:56

When creating an ASP.NET Core app an environment variable called ASPNETCORE_ENVIRONMENT=Development will be set for you and when debugging you will see that the

3条回答
  •  迷失自我
    2021-01-13 01:38

    The answer from Duncan worked for me, but there is a small variation for me, maybe due to the version of ASP.NET Core and Service Fabric I use.

    I need to override the method CreateServiceInstanceListeners in my Web Stateless Service. So that means I'll have this code:

    protected override IEnumerable CreateServiceInstanceListeners()
    {
        return new ServiceInstanceListener[]
        {
            new ServiceInstanceListener(serviceContext =>
                new WebListenerCommunicationListener(serviceContext, "ServiceEndpoint", (url, listener) =>
                {
                    ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting WebListener on {url}");
    
                    var environment = FabricRuntime.GetActivationContext()
                        ?.GetConfigurationPackageObject("Config")
                        ?.Settings.Sections["Environment"]
                        ?.Parameters["ASPNETCORE_ENVIRONMENT"]?.Value;
    
                    return new WebHostBuilder().UseWebListener()
                        .ConfigureServices(
                            services => services
                                .AddSingleton(serviceContext))
                        .UseContentRoot(Directory.GetCurrentDirectory())
                        .UseStartup()
                        .UseEnvironment(environment)
                        .UseApplicationInsights()
                        .UseUrls(url)
                        .Build();
                }))
        };
    }
    

    Of course, you need to set the ASPNETCORE_ENVIRONMENT variable as explained by Duncan.

提交回复
热议问题