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

前端 未结 3 1265
爱一瞬间的悲伤
爱一瞬间的悲伤 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:41

    I ran into the same issue and was able to create a solution that worked for me.

    If you look at your ASP.NET Core project, you should see a Program.cs file. At the bottom of it you should see the following interface implementation:

    Task ICommunicationListener.OpenAsync(CancellationToken cancellationToken)
    {
    ...
    }
    

    You're going to first want to change it to something like the following:

    Task ICommunicationListener.OpenAsync(CancellationToken cancellationToken)
    {
        var context = FabricRuntime.GetActivationContext();
        var endpoint = context.GetEndpoint(_endpointName);
        var config = context.GetConfigurationPackageObject("Config");
        var environment = config.Settings.Sections["Environment"].Parameters["ASPNETCORE_ENVIRONMENT"].Value;
    
        var serverUrl = $"{endpoint.Protocol}://{FabricRuntime.GetNodeContext().IPAddressOrFQDN}:{endpoint.Port}";
    
        _webHost = new WebHostBuilder().UseKestrel()
                                       .UseContentRoot(Directory.GetCurrentDirectory())
                                       .UseStartup()
                                       .UseEnvironment(environment)
                                       .UseUrls(serverUrl)
                                       .Build();
    
        _webHost.Start();
    
        return Task.FromResult(serverUrl);
    }
    

    The key portion is the .UseEnvironment(environment) call, along with the supporting retrieval of the environment from the configuration. This will give ASP.NET Core the necessary information it needs to choose the environment.

    Having done this, you'll obviously need to add the ASPNETCORE_ENVIRONMENT setting to the config section. That looks like the following:

    Under your ASP.NET Core project you'll find a directory called PackageRoot/Config. Inside of that there should be a Settings.xml file. Add the following code inside the tag...

    Next, you're going to want to look at the ApplicationPackageRoot/ApplicationManifest.xml file inside the actual Service Fabric Project (this is NOT the ASP.NET Core project). Two file changes are required.

    1. Add the ASPNETCORE_ENVIRONMENT parameter inside the tag at the top of the file like so:

      
      
    2. Modify your tag to include a section like so:

      
        
          
            

    Finally, modify your ApplicationParameters/Local.1Node.xml and friends to contain the ASPNETCORE_ENVIRONMENT parameter:

    
    

    It's a lot of steps to add a freaking variable you can retrieve, but it does allow you a great deal of flexibility and follows the standard Service Fabric pattern to make deployments simple. I hope this helps!

提交回复
热议问题