Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do you set and access run-time configuration parameters per environment for service fabric?

For two environments, local and cloud, how would I set up custom settings or parameters for resources such as Sql databases, storage accounts, etc... Ideally it would be one parameter name called in code to say, point a DbContext towards a particular database, that in configurations for either a local or cloud environment be different. Thank you.

like image 575
StampyTurtle Avatar asked Nov 25 '15 23:11

StampyTurtle


People also ask

How will you define the Azure service fabric?

Azure Service Fabric is a distributed systems platform that makes it easy to package, deploy, and manage scalable and reliable microservices and containers. Service Fabric also addresses the significant challenges in developing and managing cloud native applications.

How do I open a fabric Explorer service?

For developer workstation setup, you can launch Service Fabric Explorer on your local cluster by navigating to https://localhost:19080/Explorer.

How many nodes can be maintained on a service fabric cluster?

A single Service Fabric node type/scale set can not contain more than 100 nodes/VMs. To scale a cluster beyond 100 nodes, add additional node types.


2 Answers

In order to have per environment variables for running Service Fabric locally and in the cloud this is what you must do:

  1. Add your custom config section and parameters to the Settings.xml file of the Service/Actor project (located at \PackageRoot\Config\Settings.xml from the project root). Leave the parameters blank as we will be setting these elsewhere per environment. Here is an example one.
<?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. Underneath that will be a <ConfigOverrides> element where we will declare what values for our configs will be supplanted by values set per environment in the local and cloud xml files underneath ApplicationParameters in our Service Fabric project. In that same ApplicationManifest.xml file, you'll need to add the parameter that will be present in the local and cloud xml files, otherwise they'll be overwritten upon build.

Continuing with the example above, this is how it would be set.

<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 underneath ApplicationParameters in your Service Fabric project, you will specify your environment specific variables like so.
<?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 per-environment configuration variables like so.
var configurationPackage = Context.CodePackageActivationContext.GetConfigurationPackageObject("Config");  var connectionStringParameter = configurationPackage.Settings.Sections["UserDatabase"].Parameters["UserDatabaseConnectionString"]; 
like image 95
StampyTurtle Avatar answered Oct 10 '22 01:10

StampyTurtle


You can just use environment variables just like any other application, this also works with guest executable within service fabric unlike the settings.xml as this requires the built-in service fabric runtime.

Within your application you can access environment variables just like any other .net application though the GetEnvironmentVariable method on the Environment class:

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

Then we need to setup some default environment variables values, this is done within 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> 

These environment variable can then be overridden within the ApplicationManifest.xml file by 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 then can be parameterised like any other application manifest setting using the 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'll have to update the 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> 
like image 40
Kevin Smith Avatar answered Oct 10 '22 00:10

Kevin Smith