Thursday, March 21, 2013

[C#] How to read custom config Section in C#

step 1. Create a class to handle custom configuration elements. in this sample called "MyAttribute"

and this class will be responsible for reading following element from config file



<add myAttributeId="6" myAttributes="1" />



using System.Configuration;

namespace MyApp.Configurations
{
    public class MyAttributeConfigurationElement : ConfigurationElement
    {
        [ConfigurationProperty("myAttributeId", IsRequired = true)]
        public int MyAttributeId
        {
            get
            {
                return (int)this["myAttributeId"];
            }
            set
            {
                this["myAttributeId"] = value;
            }
        }


        [ConfigurationProperty("myAttributes", IsRequired = true)]
        public string MyAttributes
        {
            get
            {
                return (string)this["myAttributes"];
            }
            set
            {
                this["myAttributes"] = value;
            }
        }
    }
}

step 2 create class to handle config element collection. as follows


using System.Collections;
using System.Collections.Generic;
using System.Configuration;

namespace MyApp.Configurations
{
    public class MyAttributeConfigurationCollection : ConfigurationElementCollection, IEnumerable
    {
        ///





        ///
        ///
        ///
        ///
        public MyAttributeConfigurationElement this[int index]
        {
            get
            {
                return BaseGet(index) as MyAttributeConfigurationElement;
            }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }

        ///





        ///
        ///
        public IEnumerable Elements
        {
            get
            {
                for (var i = 0; i < Count; ++i)
                {
                    yield return this[i];
                }
            }
        }

        ///





        ///
        ///
        ///
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        ///





        ///
        ///
        ///
        public new IEnumerator GetEnumerator()
        {
            for (var i = 0; i < Count; ++i)
            {
                yield return this[i];
            }
        }

        ///





        ///
        ///
        ///
        protected override ConfigurationElement CreateNewElement()
        {
            return new MyAttributeConfigurationElement();
        }

        ///





        ///
        ///
        ///
        ///
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((MyAttributeConfigurationElement)element).MyAttributeId;
        }
    }
}




step 3. Create Class to handle custom configuration section. Here called "MyAttributeSection"


using System.Configuration;

namespace MyApp.Configurations
{
    public class MyAttributeConfiguration : ConfigurationSection
    {
        private static readonly MyAttributeConfiguration MyAttributeConfigSection
            = ConfigurationManager.GetSection("myAttributeConfigSection") as MyAttributeConfiguration;

        public static MyAttributeConfiguration Section
        {
            get
            {
                return MyAttributeConfigSection;
            }
        }


        [ConfigurationProperty("myAttributeConfig")]
        public MyAttributeConfigurationCollection MyAttributeConfig
        {
            get
            {
                return this["myAttributeConfig"] as MyAttributeConfigurationCollection;
            }
        }
    }
}

step 4 create a config file entry to be read by the classes we have created so far



<configSections>
    <section name="myAttributeConfigSection" type="MyApp.Configurations.MyAttributeConfiguration,MyApp" />
</configSections>
<myAttributeConfigSection>
    <myAttributeConfig>
      <add myAttributeId="6" myAttributes="1" />
    </myAttributeConfig>
</myAttributeConfigSection>





Friday, March 1, 2013

[ASP .Net MVC3] ASP.Net MVC Execution flow

Ref

A typical flow of Web Request comes to MVC Application is handled as follows

WebRequest ->  UrlRoutingModule -> MVCRouteHandler (Extracted from Route Object and implements IRouteHandler) 
  1. MvcHandler (IHttpHandler) 
  2. RequestContext
-> Controller

So to understand it better let see the flow simpler way :-

At very first request comes to UrlRoutingModule which is a http module very similar to the any web request processor (Asp.Net engine or IIS). This parse the request and select route from requested URL. Once parsing complete UrlRoutingModule select the first route object which matches to the parsed request. (Route object is the object which is used at Global.asax). read more about routing object

if no route is matched url is processed as normal url (as processed on asp.net or iis).


After route object is selected RequestContext is created and an instance of MvcRouteHandler is extract from the route object creates MvcHanlder object and pass RequestContext to it.

Once MvcHandler object is obtained, MVCHandler selects appropriate Controller to serve request.

Later Execute method is performed on the controller to perform the action.

For controllers that inherit from the ControllerBase class, the ControllerActionInvoker object that is associated with the controller determines which action method of the controller class to call, and then calls that method.

Once Action is executed Result is served as Allowed Result Type (i.e. ViewResult, RedirectToRouteResult, RedirectResult, ContentResult, JsonResult, FileResult, and EmptyResult.)