Using code package environment variables in Service Fabric

In my previous post on configuring ASP.NET Core applications in Service Fabric using configuration packages, per environment overrides and a custom configuration provider I gave an example of how you could set a correct web host environment which allows you to adjust configuration and behavior of various components based on the current environment (staging, production, etc).

While everything from that post still stands there is a better way to set host environment as code packages also support environment variables which are set for the host process and which can be overwritten with per-environment values similar to configuration packages.

Service Fabric configuration

So if we consider the host environment example again the first thing that you need to do is to add ASPNETCORE_ENVIRONMENT in your service manifest:

<CodePackage Name="Code" Version="1.0.0">  
      <EnvironmentVariable Name="ASPNETCORE_ENVIRONMENT" Value="" />

Then make sure to override it in the application manifest:

  <ServiceManifestRef ServiceManifestName="BookFast.FacilityPkg" ServiceManifestVersion="1.0.0" />
  <EnvironmentOverrides CodePackageRef="Code">
    <EnvironmentVariable Name="ASPNETCORE_ENVIRONMENT" Value="[environment]" />

And finally define the environment parameter in the application manifest and provide its values in per-environment settings files.

You don't need to manually extract this setting from the configuration and provide it to WebHostBuilder anymore as it will be extracted from environment variables by the framework.

If you have more environment variables and you want to make them available through the standard configuration infrastructure just make sure you add the configuration provider from Microsoft.Extensions.Configuration.EnvironmentVariables package:

public Startup(StatelessServiceContext serviceContext)  
    var builder = new ConfigurationBuilder()

    Configuration = builder.Build();

AddServiceFabricConfiguration is the extension that adds a custom configuration provider that reads from Service Fabric configuration packages as explained in the previous post.

Andrei Dzimchuk

Andrei Dzimchuk

Hi, I'm Andrei, I'm a software developer building cloud enabled solutions. I'm mostly focused on Azure and .NET and I try to share my experience and knowledge here with you.