Where do you install and access runtime configuration settings for a service environment?

In two environments, local and cloud, how do I configure user preferences or settings for resources such as Sql databases, storage accounts, etc. Ideally, this will be a single parameter name, called in the code, to indicate, point DbContext to a specific database, which will be different in configurations for a local or cloud environment. Thank.

+64
azure azure-sql-database azure-service-fabric
Nov 25 '15 at 23:28
source share
3 answers

To have environment variables for starting Service Fabric locally and in the cloud, this is what you should do:

  • Add your custom section and configuration parameters to the Settings.xml file of the Service / Actor project (located in \ PackageRoot \ Config \ Settings.xml from the project root). Leave the options blank as we will set them elsewhere for each environment. Here is an example.
<?xml version="1.0" encoding="utf-8" ?> <Settings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2011/01/fabric"> <!-- Add your custom configuration sections and parameters here --> <Section Name="UserDatabase"> <Parameter Name="UserDatabaseConnectionString" Value="" /> </Section> </Settings> 
  1. In the ApplicationManifest.xml file of your Service Fabric project, there will be <ServiceManifestImport> elements for each of your included projects. Below this is the <ConfigOverrides> element, where we will declare which values ​​for our configurations will be supplanted by the values ​​set for the environment in local and cloud XML files under ApplicationParameters in our Service Fabric project. In the same ApplicationManifest.xml file, you will need to add a parameter that will be present in local and cloud XML files, otherwise they will be overwritten after assembly.

Continuing the above example, this will install it.

 <Parameters> <Parameter Name="ServiceName_InstanceCount" DefaultValue="-1" /> <Parameter Name="UserDatabaseConnectionString" DefaultValue="" /> </Parameters> <ConfigOverrides> <ConfigOverride Name="Config"> <Settings> <Section Name="UserDatabase"> <Parameter Name="UserDatabaseConnectionString" Value="[UserDatabaseConnectionString]" /> </Section> </Settings> </ConfigOverride> </ConfigOverrides> 
  1. In the local.xml and cloud.xml files under ApplicationParameters in your Service Fabric project, you will specify environment variables such as this.
 <?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:/AppFabricName.ServiceFabric" xmlns="http://schemas.microsoft.com/2011/01/fabric"> <Parameters> <Parameter Name="ServiceName_InstanceCount" Value="1" /> <Parameter Name="UserDatabaseConnectionString" Value="Server=(localdb)\MsSqlLocalDb;Database=Users;User=ReadOnlyUser;Password=XXXXX;" /> </Parameters> </Application> 
  1. Finally, in your Service / Actor, you can access these configuration variables for each environment.
 var configurationPackage = Context.CodePackageActivationContext.GetConfigurationPackageObject("Config"); var connectionStringParameter = configurationPackage.Settings.Sections["UserDatabase"].Parameters["UserDatabaseConnectionString"]; 
+117
Nov 26 '15 at 17:39
source share
β€” -

You can simply use environment variables just like any other application, this also works with guest executables in the work structure as opposed to settings.xml because it requires a built-in runtime environment.

In your application, you can access environment variables, like any other.net application, although the GetEnvironmentVariable method in the Environment class:

 var baseUri = Environment.GetEnvironmentVariable("SuperWebServiceBaseUri"); 

Then we need to set some default environment variable values, this is done in the ServiceManifest.xml manifest file of the service.

 <?xml version="1.0" encoding="utf-8" ?> <ServiceManifest Name="MyServicePkg" Version="1.0.0" xmlns="http://schemas.microsoft.com/2011/01/fabric" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <!-- snip --> <CodePackage Name="Code" Version="1.0.0"> <!-- snip --> <EnvironmentVariables> <EnvironmentVariable Name="SuperWebServiceBaseUri" Value="http://localhost:12345"/> </EnvironmentVariables> </CodePackage> <!-- snip --> </ServiceManifest> 

You can then override this environment variable in the ApplicationManifest.xml file using the following code:

 <?xml version="1.0" encoding="utf-8"?> <ApplicationManifest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ApplicationTypeName="ChileTargetType" ApplicationTypeVersion="1.0.0" xmlns="http://schemas.microsoft.com/2011/01/fabric"> <Parameters> <!-- snip --> </Parameters> <ServiceManifestImport> <ServiceManifestRef ServiceManifestName="MyServicePkg" ServiceManifestVersion="1.0.0" /> <EnvironmentOverrides CodePackageRef="Code"> <EnvironmentVariable Name="SuperWebServiceBaseUri" Value="https://the-real-live-super-base-uri.com/"/> </EnvironmentOverrides> </ServiceManifestImport> <!-- snip --> </ApplicationManifest> 

This parameter can then be parameterized, like any other application manifest parameter, using local.xml and cloud.xml .

 <?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:/AppFabricName.ServiceFabric" xmlns="http://schemas.microsoft.com/2011/01/fabric"> <Parameters> <Parameter Name="MyService_SuperWebServiceBaseUri" Value="https://another-base-uri.com/" /> </Parameters> </Application> 

Then we will need to update ApplicationManifest.xml to support these parameters;

 <?xml version="1.0" encoding="utf-8"?> <ApplicationManifest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ApplicationTypeName="ChileTargetType" ApplicationTypeVersion="1.0.0" xmlns="http://schemas.microsoft.com/2011/01/fabric"> <Parameters> <Parameter Name="MyService_SuperWebServiceBaseUri" DefaultValue="https://the-real-live-super-base-uri.com/" /> </Parameters> <ServiceManifestImport> <ServiceManifestRef ServiceManifestName="MyServicePkg" ServiceManifestVersion="1.0.0" /> <EnvironmentOverrides CodePackageRef="Code"> <EnvironmentVariable Name="SuperWebServiceBaseUri" Value="[MyService_SuperWebServiceBaseUri]"/> </EnvironmentOverrides> </ServiceManifestImport> <!-- snip --> </ApplicationManifest> 
+29
Jan 10 '17 at 9:42 on
source share

The answers above explain well how to do this. I want to add a sidemark why this is "confusing":

It should be this way, because the services must be autonomous. They should be launched by default in any application to which they are attached. Regardless of Manifest statement. Thus, a service can only rely on parameters that are at least predetermined in its own configuration.

These settings can be overwritten by the application. This is the only universal approach.

+5
Sep 01 '17 at 10:35 on
source share



All Articles