Setting ASP.NET State Service on Remote Machine

ASP.NET does allow to set the ASP.NET State Service on a separate machine. This is done through the appropriate setting on the web.config file.

But this is not the only change that we need to do we have set the registry settings on the machine where ASP.NET State Service is running.

We need to do change the the following key

HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\aspnet_state\Parameters

and change the value of AllowRemoteConnection to 1 instead of 0.

Advertisements

.NET Custom Settings

We normally tend to lazy and use the .NET Configuration AppSettings element for defining our own custom configuration keys. Though this does solve the purpose but sometime we need MORE. We may need our own tags like <myTag> to be used in the configuration file. .NET provides a standard mechansim to achieve this. Lets assume we want the following in our .config file

<MySettings>

    <Setting Name=”” Value=”” IsValid=”” />

</MySettings>

There are 5 things to be done

  1. Define a class that inherits from ConfigurationElement, this represent the element node i.e. <MySetting> for the above mentioned XML.
  2. Define a class that inherits from ConfigurationElementCollection, this represents the collection node that contain the element node i.e. <MySettings> for the above mentioned XML.
  3. Define a class that inherits from ConfigurationSection, this represent the master class through which the section is accessed.
  4. Add entry in the <configSections> of the config file.
  5. Use System.Configuration.ConfigurationManager.GetSection method to access the section defined in the code.

Now let’s drill with the code

Inherit from ConfigurationElement

public class MySetting : ConfigurationElement
    {
                
        [ConfigurationProperty(“Name”, IsRequired = true)]
        public String Name
        {
            get
            {
                return (string)this[“Name”];
            }
            set
            {
                this[“Name”] = value;
            }
        }

      
        [ConfigurationProperty(“Value”)]
        public String Value
        {
            get
            {
                return (string)this[“Value”];
            }
            set
            {
                this[“Value”] = value;
            }
        }

        [ConfigurationProperty(“IsValid”)]
        public bool IsValid
        {
            get
            {
                return (bool)this[“IsValid”];
            }
            set
            {
                this[“IsValid”] = value;
            }
        }
    }

Inherit from ConfigurationElementCollection

public class MySettings : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new MySetting();
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((MySetting)element).Name;
        }
        public override ConfigurationElementCollectionType CollectionType
        {
            get { return ConfigurationElementCollectionType.BasicMap; }
        }
        protected override string ElementName
        {
            get { return “MySetting”; }
        }
        public MySetting this[int index]
        {
            get{ return (MySetting)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                    BaseRemoveAt(index);
                BaseAdd(index, value);
            }
        }
        new public MySetting this[string Name]
        {
            get { return (MySetting)BaseGet(Name); }
        }
        public bool ContainsKey(string key)
        {
            bool result = false;
            object[] keys = BaseGetAllKeys();
            foreach (object obj in keys)
            {
                if ((string)obj == key)
                {
                    result = true;
                    break;
                }
            }
            return result;
        }
    }

Inherit from ConfigurationSection

public class MySection:ConfigurationSection
    {
       [ConfigurationProperty(“MySettings”,IsDefaultCollection=true)]
        public MySettings MySetting
        {
            get
            {
                return (MySettings)base[“MySettings”];
            }
        }
    }

 

Entry in App.Config/Web.Config

<configuration>
  <configSections>
    <section name=”MySettings” type=”DebasishPramanik.Configuration.MySection,
      CustomSettings”/>
  </configSections>
  <MySettings>
    <MySetting Name=”BlogID” Value=”12345″ IsValid=”true”/>
  </MySettings>
</configuration>

 

Use in Application

 

DebasishPramanik.Configuration.MySection section = (DebasishPramanik.Configuration.MySection)System.Configuration.ConfigurationManager.GetSection("MySettings");
            
foreach (DebasishPramanik.Configuration.Setting setting in section.MySettings)
 {
     Console.WriteLine(setting.Name);
}

 

 

    ASP.NET Config files: Un-necessary Strings

    We have been using ANTS Profiler lately to understand the memory behaviour of our ASP.NET application. What we observed is

    1. Too much memory have been allocated by Strings Objects.
    2. These string objects survives across many GC collection.

    When we drill those strings more, we discovered

    1. That these are not our strings.
    2. Actually ASP.NET loads the settings of machine.config and web.config into the memory.
    3. I can now make out why it need to unload the AppDomain when we change these config files as the environment have to reload the settings again.
    4. There are many settings in these config files which have no relevance to our application.
    5. So we started commenting or removing from the machine.config file. (We took the backup of the ORIGINAL file).
    6. For e.g. webPart settings. We removed the entire block.
    7. We tested our web application and found everything to work fine.
    8. We then started removing those un-necessary entries.
    9. We then used the ANTS profiler to profile the memory and found that we save some 58 MB of memory by doing so.
    10. There was direct impact on the Gen 0 collection.

    We haven’t decided whether we will do these on our production server but these can be done provided our application is the only app being used on that server.

    Please take appropriate backup before trying this out.