19

I am working on Azure Service Fabric Reliable Actor implementation. Any idea/link on where can I store the Configuration value (e.g. DB connection string) and how to access that in code.

Pratik Mehta
  • 1,310
  • 4
  • 15
  • 37

3 Answers3

15

A Service Fabric application consists of the code package, a config package, and the data (https://azure.microsoft.com/en-gb/documentation/articles/service-fabric-application-model/).

You can use the config package to store and retrieve any kind of key-value pairs you need e.g. a connection string. Have a look at this article https://azure.microsoft.com/en-us/documentation/articles/service-fabric-manage-multiple-environment-app-configuration/ for more information.

charisk
  • 3,190
  • 2
  • 24
  • 18
  • 2
    Thanks for the link, I stored the value in Settings.xml. but How to retrieve this value in code at runtime? I did not find any example on this. – Pratik Mehta Jan 26 '16 at 11:45
  • Ah, good point! You need to use the CodePackageActivationConext. Have a look at this SO answer http://stackoverflow.com/questions/33928204/where-do-you-set-and-access-run-time-configuration-parameters-per-environment-fo – charisk Jan 26 '16 at 11:59
  • Works like a charm. But one more question. As ServiceInitializationParameters is only accessible within the Actor/Service, how can I access this in other classes? I cant create static variable in Actor/Service because it is non static object. and If I create a normal property then I have to instantiate the Actor/Service class in other classes when I need the value. – Pratik Mehta Jan 26 '16 at 12:36
  • You can use it within a stateless service and then call that service whenever you need to retrieve configuration information. That's quite a nice solution in my opinion because it encapsulates all the configuration stuff and can be mocked out. – charisk Jan 26 '16 at 12:41
  • For a complete example of how to use config and data packages, check out this sample application. Most of the services in this application make use of config and data packages: https://github.com/Azure-Samples/service-fabric-dotnet-management-party-cluster – Vaclav Turecek Jan 26 '16 at 19:42
  • we have several hundred configuration parameters to manage, and it's getting to be a pain, any suggestions ? – Alex Gordon Jan 11 '18 at 01:15
  • This explanation on Binary Radix also helped me get a working sample: http://www.binaryradix.com/2016/10/reading-from-configuration-within-azure.html – Darrel K. Jan 15 '18 at 12:18
  • @AlexGordon, you can use Azure App Configurations Store to avoid maintaining tons of configuration key value pairs in the ApplicationManifest.xml and other .xml files. – It's actually me Aug 11 '23 at 04:18
3

You can add multiple ApplicationParameters file. Just copy and paste the same from Cloud.Xml and use for multiple environment configurations.

Steps to Make necessary changes

  1. The values given in the Settings.xml need to be overridden in the ApplicationManifest.xml when it imports the ServiceManifest.xml .Below is the code supporting the overriding changes add them in the ApplicationManifest.xml.

    a) Add the Parameter Default value first

      <Parameters>
         <Parameter Name="StatelessService1_InstanceCount" DefaultValue="-1" />
         <!-- Default Value is set to Point to Dev Database  -->
         <Parameter Name="DatabaseString"DefaultValue="Server=someserver.database.windows.net\;Database=DbDev;user id=[userid];password=[Password];Trusted_Connection=false;" />
      </Parameters>
    

    b) Then override it in the ServiceManifestImport

      <ServiceManifestImport>
              <ServiceManifestRef ServiceManifestName="StatelessServicePkg" 
                      ServiceManifestVersion="1.0.0" />          
          <ConfigOverrides>
               <ConfigOverride Name="Config">
                    <Settings>
                         <Section Name="DatabaseConnections">
                                 <Parameter Name="DbString" Value="[DatabaseString]" />
                        </Section>
                   </Settings>
             </ConfigOverride>
        </ConfigOverrides>
      </ServiceManifestImport>
    
  2. The above code change will override the following code in settings.xml

    <Section Name="DatabaseConnections">
        <Parameter Name="DbString" Value="Server=someserver.database.windows.net\;Database=DbDev;user id=[userid];password=[Password];Trusted_Connection=false;" />
    </Section> 
    
  3. Overall when the application is deployed the values in the ApplicationParameter DevParam.xml or QaParam.xml or ProdParam.xml will overtake all the setting values.

     <Parameters>
         <Parameter Name="StatelessService1_InstanceCount" Value="-1" />
              <Parameter Name="DatabaseString" Value="Server=someserverqa.database.windows.net\;Database=DbQA;user id=[userid];password=[Password];Trusted_Connection=false;" />
     </Parameters>
    
2

In addition to the above info, it is important to know the order in which ASF overrides application setting:

Service Fabric will always choose from the application parameter file first (if specified), then the application manifest, and finally the configuration package (source)

For more info: http://www.binaryradix.com/2016/10/reading-from-configuration-within-azure.html

Rotem Varon
  • 1,597
  • 1
  • 15
  • 32