Wednesday, March 20, 2019

Web.Config with separate sections for DEV, STAG & PROD - Asp.Net / Dynamics CRM

Hi,
In this post I am going to show how to have separate sections for development, staging and production environment in web.config in asp.net / dynamics CRM Plugin. Same can be put in dynamics crm record with multi line field in (for instance custom configuration entity)

Below are steps to achieve this functionality

1) Web.config

Web.config will look like this

<configuration>

  <configSections>

    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->

    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />

    <section name="MyConfigs" type="MyConfigs.ABCGroupConfigsSection" />



  </configSections>

  <appSettings>

    <add key="CurrentEnvironment" value="DEV" />

  </appSettings>

  <MyConfigs>

  <DEV>

    <add key="crmconnection" value="https://samplecrm4.dynamics.com" />

  <add key="logFolderPath" value="E:\Log\" />

  <add key="key1" value="blahblahblah" />

  <add key="key2" value="blahblahblah" />

  <add key="key3" value="blahblahblah" />

  <add key="key4" value="blahblahblah" />




  </DEV>

  <STAG>

  <add key="crmconnection" value="https://samplecrm4.dynamics.com" />

  <add key="logFolderPath" value="E:\Log\" />

  <add key="key1" value="blahblahblah" />

  <add key="key2" value="blahblahblah" />

  <add key="key3" value="blahblahblah" />

  <add key="key4" value="blahblahblah" />


  </STAG>

  <PROD>

   <add key="crmconnection" value="https://samplecrm4.dynamics.com" />

  <add key="logFolderPath" value="E:\Log\" />

  <add key="key1" value="blahblahblah" />

  <add key="key2" value="blahblahblah" />

  <add key="key3" value="blahblahblah" />

  <add key="key4" value="blahblahblah" />


  </PROD>

    </MyConfigs>

<!-- Other configurations here -->

</configuration>


2) Code

 public class ABCGroups
    {
     
        public static ABCGroupConfigsSection _Config = ConfigurationManager.GetSection("MyConfigs") as ABCGroupConfigsSection;
        public static ABCElementCollection GetSection(string name)
        {
            try
            {
             
                if (name == "DEV")
                    return _Config.DEVGroups;
                else if (name == "STAG")
                    return _Config.STAGGroups;
                else if (name == "PROD")
                    return _Config.PRODGroups;
            }
            catch (Exception ex) {
         
                throw new Exception(ex.Message);
            }

            return null;
        }
    }
     
        public class ABCGroupConfigsSection : ConfigurationSection
        {
            //Fill the property with the tag of respective collection.
            [ConfigurationProperty("DEV")]
            public ABCElementCollection DEVGroups
            {
                get { return (ABCElementCollection)this["DEV"]; }
            }

        //Fill the property with the tag of respective collection.
        [ConfigurationProperty("STAG")]
        public ABCElementCollection STAGGroups
        {
            get { return (ABCElementCollection)this["STAG"]; }
        }

        //Fill the property with the tag of respective collection.
        [ConfigurationProperty("PROD")]
            public ABCElementCollection PRODGroups
            {
                get { return (ABCElementCollection)this["PROD"]; }
            }
    }

   
        [ConfigurationCollection(typeof(ABCGroupElement))]
        public class ABCElementCollection : ConfigurationElementCollection
        {
            public ABCGroupElement this[int index]
            {
                get { return (ABCGroupElement)BaseGet(index); }
                set
                {
                    if (BaseGet(index) != null)
                        BaseRemoveAt(index);
                    BaseAdd(index, value);
                }
            }
            protected override ConfigurationElement CreateNewElement()
            {
                return new ABCGroupElement();
            }
            protected override object GetElementKey(ConfigurationElement element)
            {
                return ((ABCGroupElement)element).key;
            }


        }
   
        public class ABCGroupElement : ConfigurationElement
        {
            [ConfigurationProperty("key", IsRequired = true)]
            public string key
        {
                get { return (string)this["key"]; }
                set { this["key"] = value; }
            }
            [ConfigurationProperty("value", IsRequired = true)]
            public string value
        {
                get { return (string)this["value"]; }
                set { this["value"] = value; }
            }
       
        }

Asp.Net MVC (5) - Exception Filter - HandleError

Applicable on Asp.Net MVC5 HandleError Filter This belongs to Exception Filters category (Authentication Filter, Authoriz...