Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
2 views
in Azure by (45.3k points)

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.

1 Answer

+2 votes
by (16.8k points)
edited by

To run per environment variables to run service fabric locally and in the cloud, try this:

Add a 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. 

Are you interested in learning Azure from basics! Here's the right video for you on Azure provided by Intellipaat:

<?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>

In ApplicationManifest.xml file of Service Fabric project, you will have <ServiceManifestImport> elements for each of the projects that you have included. 

Below this, there will be a  <ConfigOverrides> element where we have to 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>

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>

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"];

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...