Monday 5 December 2011

SQL Master Data Services (MDS) - How to I retrieve the connection string from Microsoft.MasterDataServices.Workflow.Properties.Settings? - it is not available via ConfigurationManager.AppSettings or ConfigurationManager.ConnectionStrings

A question today from one of my Colleagues surrounded the retrieval of a value from an app.config file used by a Master Data Services Workflow Extender.

Master Data Services (MDS) workflow extenders support the creation of Business Logic (amongst other things such as external WCF Service calls) against data changes in the Master Data Services catalogs. These workflow extenders run in the context of the SQL Server Master Data Services executable (Microsoft.MasterDataServices.Workflow.exe) - and consequently rely upon the app config file named Microsoft.MasterDataServices.Workflow.exe.config.

Unfortunately, it doesn't appear as though the MDS System Settings classes give you access to that connection string either (http://msdn.microsoft.com/en-us/library/ff487028.aspx). Consequently, you have to default to basic .NET functionality for handling configuration sections.

The traditional appSettings section of the Microsoft.MasterDataServices.Workflow.exe.config file is not used by MDS - it actually uses an applicationSettings section of the file that is not accessible by the usual ConfigurationManager.AppSettings and ConfigurationManager.ConnectionStrings nodes in the config file. Specifically, it uses a custom System.Configuration.ApplicationSettingsGroup of type System.Configuration.ClientSettingsSection.  


To consume this section, you can use the following method (GetSettingValueFromAppConfig):

using System.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CompanyName.MasterDataWorkflow.Tests
{
    /// 
    /// Summary description for GetConnectionSettingsTest
    /// 
    [TestClass]
    public class GetConnectionSettingsTest
    {
        public GetConnectionSettingsTest()
        {
        }

        [TestMethod]
        public void TestGetConnectionSettings()
        {
            //Should return the value from the local applicationSettings Node.
            //Fully qualified section name
            const string sectionName = "applicationSettings/Microsoft.MasterDataServices.Workflow.Properties.Settings";
            const string settingName = "ConnectionString";

            Assert.AreEqual(
                GetSettingValueFromAppConfig(settingName,sectionName),
                "Server=.;Database=MasterDataServices;Integrated Security=SSPI") ;

        }

        private string GetSettingValueFromAppConfig(string settingName, string sectionName)
        {
            System.Configuration.ClientSettingsSection section =
               (System.Configuration.ClientSettingsSection)
                System.Configuration.ConfigurationManager.GetSection(sectionName);
            foreach (SettingElement setting in section.Settings)
            {
                string value = setting.Value.ValueXml.InnerText;
                string name = setting.Name;
                if (name.ToLower().StartsWith(settingName.ToLower()))
                {
                    return value;
                }
            }
            return string.Empty;
        }
    }
}


DDK

No comments: