Marko
Marko

Reputation: 1626

Azure Service Fabric - different publish settings for different environments

This seems like an easy question, but I still haven't found an answer to it, despite having spent a lot of time reading the documentation and related articles on the internet. I have a SF application with several services that I wish to be able to package (from Visual Studio) for different environments: dev, test, prod. These environments require different settings, for example the user the application will run as, the connection string to the database, etc. I have managed to change the environment variables on a particular cluster with Start-ServiceFabricApplicationUpgrade command, but this way I can't set everything I need, and anyway I'd prefer to have all the configuration I need in the application package. What should I do? In the case it matters, I use Windows standalone clusters (except in dev environment, which is a local development cluster).

Upvotes: 0

Views: 590

Answers (1)

Preben Huybrechts
Preben Huybrechts

Reputation: 6141

Service level

In your ServiceManifest.xml located under {YourService}\PackageRoot\ServiceManifest.xml.

You'll have an entry CodePackage add the tag EnvironmentVariables underneat it with the EnvironmentVariables you want to use with an empty value.

<CodePackage Name="Code" Version="1.0.0">
    <EntryPoint>
        <!--...-->
    </EntryPoint>
    <EnvironmentVariables>
        <EnvironmentVariable Name="Connectionstring" Value=""/>
    </EnvironmentVariables>
</CodePackage>

To access it in your code you can use

Environment.GetEnvironmentVariable("Connectionstring", EnvironmentVariableTarget.Process);

Application level

In your ApplicationManifest.xmllocated under {YourApplication}\ApplicationPackageRoot\ApplicationManifest.xml

There is a section <Parameters> add a parameter for every value you want to set at deploy time.

<Parameters>
    <Parameter Name="Connectionstring" DefaultValue="{your connectionstring}" />
</Parameters>

In the same file (application manifest) there is ServiceManifestImport you need to extend this with EnvironmentOverrides for every service.

<ServiceManifestImport>
    <ServiceManifestRef ServiceManifestName="MyServicePkg" ServiceManifestVersion="1.0.0" />
    <ConfigOverrides />
    <EnvironmentOverrides CodePackageRef="code">
      <EnvironmentVariable Name="Connectionstring" Value="[Connectionstring]" />
    </EnvironmentOverrides>
</ServiceManifestImport>

The value in the square brackets, is a value thats replaced by the parameter defined in <Parameters> tag.

Environment specific configs

Under the {YourApplication}\ApplicationParameters folder. Create a file for every environment. Example Test.xml and specify a parameter with the value for the environment.

<?xml version="1.0" encoding="utf-8"?>
<Application xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Name="fabric:/{YourApplication}" xmlns="http://schemas.microsoft.com/2011/01/fabric">
  <Parameters>
    <Parameter Name="Connectionstring" Value="{Your test connection string}" />
  </Parameters>
</Application>

After creating the Settings file, you need to create a publish profile that uses this settings file. Uner {YourApplication}\PublishProfiles\ create a file for the environment. Example Test.xml

<?xml version="1.0" encoding="utf-8"?>
<PublishProfile xmlns="http://schemas.microsoft.com/2015/05/fabrictools">
  <ClusterConnectionParameters ConnectionEndpoint="{YourEndpoint}" />
  <!--Reference the settings file-->
  <ApplicationParameterFile Path="..\ApplicationParameters\Test.xml" />
  <UpgradeDeployment Mode="UnmonitoredAuto" Enabled="true">
  
    <!--Specify additonal upgrade parameters here--> 
    <Parameters />
  </UpgradeDeployment>
</PublishProfile>

After you've done this, Its just a matter of deploying the application with the correct publish profile.

Upvotes: 4

Related Questions