Guide for environment configuration in Azure Service Fabric.
When I started with Azure Service Fabric, one of the issues was to separate development, staging and production environments. Standard .NET Core approach, with application settings for Staging and Development and ASPNETCORE_ENVIRONMENT variable added to the user/system profile in OS, doesn’t provide a foolproof configuration setup for working with scalable services. A virtual machine scale set can have any number of instantiated machines without proper environments variables in place.
The best way to do it is to use Azure Service fabric project XML files and put your variable into the corresponding section of a service publish profile.
It’s important to keep this setting applied via application publish manifest, otherwise things might get really messy, because you can have tons of services with different appsettings.json and publishing DevOps pipelines. I had this problem and hope this approach will help you to avoid it :).
Following example is based on .NET Core 2.1 Web API.
ASF configuration in your project.
Navigate to PackageRoot\Config\Settings.xml and add section below inside the Settings section.
<Section Name="YourConfiguration">
<Parameter Name="ASPNETCORE_ENVIRONMENT" Value="Development" /></Section>
Navigate to the project root and open Startup.cs, navigate to Startup method.
Set your environment name variable with the following code.
env.EnvironmentName = FabricRuntime.GetActivationContext()?.GetConfigurationPackageObject("Config")?.Settings.Sections["YourConfiguration"]?.Parameters["ASPNETCORE_ENVIRONMENT"]?.Value;
Service Fabric project configuration setup
Let's start with adjustment of ApplicationPackageRoot\ApplicationManifest.xml file
Adjust code as follows, by adding ASPNETCORE_ENVIRONMENT to Parameters section and inserting ConfigOverride section with name “Config”.
<Parameters>
<Parameter Name="WebApi_InstanceCount" DefaultValue="-1" /><Parameter Name="ASPNETCORE_ENVIRONMENT" DefaultValue="" /></Parameters><ServiceManifestImport>
<ServiceManifestRef ServiceManifestName="WebApiPkg" ServiceManifestVersion="1.0.0" />
<ConfigOverrides>
<ConfigOverride Name="Config">
<Settings>
<Section Name="YourConfiguration">
<Parameter Name="ASPNETCORE_ENVIRONMENT" Value="ASPNETCORE_ENVIRONMENT]" />
</Section>
</Settings>
</ConfigOverride>
</ConfigOverrides>
<Policies></Policies>
</ServiceManifestImport>
The next step is to add this parameter to the publish profile parameters.
Parameter files named by publishing profile name.
Just add your ASPNETCORE_ENVIRONMENT value to the corresponding profile, you should follow the .NET Core rules here and set proper string value.
Development => env.IsDevelopment()
Staging => env.IsStaging()
Production=> env.IsProduction()
<Parameters>
<Parameter Name="WebApi_InstanceCount" Value="1" />
<Parameter Name="ASPNETCORE_ENVIRONMENT" Value="Development" /></Parameters>
In my opinion, this approach is failproof solution. Cheers!