简体   繁体   中英

getting connection string name in azure worker role

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 (eg from a .cscfg file) my code will look for:

<connectionStrings> ... </connectionStrings>

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

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

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

    } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM