1

I am currently injection the concrete ConfigFileSettings of the IConfigFileSettings into classes that require a name for the connection string. The simplified code looks as follows:

public interface IConfigFileSettings
{
    string GetConnectionString(string name);
}

public class ConfigFileSettings : IConfigFileSettings
{
    public string GetConnectionString(string name)
    {
        return ConfigurationManager.ConnectionStrings[name].Name;
    }
}

This works fine for webapi's hosted in iis, windows services and console application. I guess:

ConfigurationManager.ConnectionStrings[name].Name

won't work in worker roles. Can I adapt the GetConnectionString method to make it work in all environments transparently? Also even if I get the connection sting name (e.g. from a .cscfg file) my code will look for:

<connectionStrings> ... </connectionStrings>

I guess I cannot just add an entry into the .cscfg file?

cs0815
  • 16,751
  • 45
  • 136
  • 299

2 Answers2

1

You can put them in the .cscfg file like:

<?xml version="1.0"?>
<ServiceConfiguration serviceName="Web.Azure" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" osFamily="3" osVersion="*" schemaVersion="2013-03.2.0">
  <Role name="Worker">
    <Instances count="2" />
    <ConfigurationSettings>
      <Setting name="connectionstringname" value="connectionstringvalue" />
    </ConfigurationSettings>
  </Role>
</ServiceConfiguration>

Then you can read them by:

var connectionstring = RoleEnvironment.GetConfigurationSettingValue("connectionstringname");
Peter
  • 27,590
  • 8
  • 64
  • 84
  • Thanks. I want to make this work in all environment though. I think one can use RoleEnvironment to detect which environment I am in. Correct? – cs0815 Mar 02 '16 at 22:49
1

This is a small utility for strong typed config in azure implemented with Castle Windsor DictionaryAdapter. I use it in my projects. Usage :

  1. Explore the code a bit to get the idea
  2. Define your strongly typed config interfaces - Look at Configuration.Interfaces assembly
  3. Define your factories / use azure config provider in composition root and use dictionary adapter to fill the config dictionaries -
  4. Inject your config like in MyWorkerService.cs

    using Configuration.Interfaces; using Persistence.Interfaces; using Worker.Services.Interfaces;

    namespace Worker.Services { public class MyWorkerService : IMyWorkerService { private readonly IConnectionStrings _connectionStrings; private readonly IAzureServiceConfiguration _azureServiceConfiguration; private readonly IMicrosoftStorageConfig _microsoftStorageConfig; private readonly IPersitenceServiceConfigDependent _persitenceServiceConfigDependent; private readonly IAppConfigSettings _appConfigSettings;

    public MyWorkerService(
        IPersitenceServiceConfigDependent persitenceServiceConfigDependent,
    
        IConnectionStrings connectionStrings,
        IAzureServiceConfiguration azureServiceConfiguration,
        IMicrosoftStorageConfig microsoftStorageConfig,
        IAppConfigSettings appConfigSettings)
    {
        _connectionStrings = connectionStrings;
        _azureServiceConfiguration = azureServiceConfiguration;
        _microsoftStorageConfig = microsoftStorageConfig;
        _persitenceServiceConfigDependent = persitenceServiceConfigDependent;
        _appConfigSettings = appConfigSettings;
    }
    
    public string DoWork()
    {
        _persitenceServiceConfigDependent.ConfigDependentAction("blah");
        var configSetting = _microsoftStorageConfig.StorageConnectionString;
        return $"Job done :" +
               $" <br> msConfig : {configSetting}, " +
               $" <br> azureConfig.ServiceBusConnectionString:{_azureServiceConfiguration.ServiceBusConnectionString} " +
               $" <br> webConfig.SubscriptionId:{_appConfigSettings.SubscriptionId} " +
               $" <br> connectionStrings.DefaultConnection :{_connectionStrings.DefaultConnection}";
    }
    

    } }

Ognyan Dimitrov
  • 6,026
  • 1
  • 48
  • 70