Adding Controls in Web.config from code behind, results session gets cleared RRS feed

  • Question

  • Hi All,

    I need to add controls like Ajax Control Toolkit in web.config from code behind on basis of some condition. same as below-

                    <add tagPrefix="ajaxtoolkit"

    To achieve above, i added the below code in web Application at Application_Start 

    Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
    PagesSection pagesSection = (PagesSection)configuration.GetSection("system.web/pages");
    TagPrefixInfo tagPrefixInfo1 = new TagPrefixInfo("ajaxToolkit", "AjaxControlToolkit", "AjaxControlToolkit,Culture=neutral", "", "");

    after adding above code my session is not working.

    on each post back session is getting cleared.

    Thanks in Advance.

    Ravikash Verma

    Wednesday, August 30, 2017 9:42 AM

All replies

  • Yes. The session is lost because IIS will automatically recycle (terminate and restart) the worker process when anything in /bin or web.config changed. And by default the Session collections are stored InProc (i.e.: inside the worker process) so all such collections goes with the recycled worker process.

    In order for the session to persist between such reloads, you have to change the <sessionState> element in web.config from "InProc" to other things such as SQLServer (and you have to configure corresponding features according to your choice)

    Also, if any of the objects you put into session collection had not tagged with SerializableAttribute, they have to add that. If it's POCO object that comes from EntityFramework, you may have to alter T4 template to add it accordingly.

    • Edited by cheong00 Wednesday, August 30, 2017 9:51 AM
    Wednesday, August 30, 2017 9:49 AM
  • Thanks for your reply.

    i added this in Application_Start which is executed only once, still App_Pool is recycling on each request. 

    Wednesday, August 30, 2017 10:08 AM
  • The problem you face in this timeline.

    1) Worker process 1 started.
    2) Application_Start is executed, web.config saving, triggering reload. But since the reload is triggered by FileSystemWatcher and the runtime need to run timestamp check to see if any recompilation to corresponding files in "Temporary ASP.NET Files" folder, there is still some time to execute the request (unless the page you visit need to run a long time)
    3) Worker process i got killed and worker process (i + 1) started.
    4) Goto 2.

    So each request seems to be new to the webserver.

    • Edited by cheong00 Wednesday, August 30, 2017 10:49 AM
    Wednesday, August 30, 2017 10:47 AM
  • Is there any other way to achieve the same from code behind.

    Our requirement is to load custom control on conditional basis, if there is any other approach to achieve the same then please let us know. 

    Wednesday, August 30, 2017 12:19 PM
  • Actually, I'm not sure why would you want to do that. It's not like that you can automatically detect the tag prefix to add this way.

    It seems more natural to me to change web.config to add to the Controls element when deploying new controls. Why do you want to use code to do that instead of editing web.config directly?

    That said, if you really want to keep it this way, you may just add a check to pagesSection.Controls for the target tag name, and skip your code if already exists. In this way, at most the web.config will be changed once.
    • Edited by cheong00 Wednesday, August 30, 2017 1:18 PM
    Wednesday, August 30, 2017 1:15 PM
  • Please post questions related to ASP.NET and web development in the ASP.NET forums.
    Wednesday, August 30, 2017 1:56 PM
  • Hi Ravikash ,

    According to your question is more related to ASP.NET, I will move the thread to  ASP.NET forum  for suitable support.

    The Visual C# discuss and ask the C# programming language, IDE, libraries, samples and tools . If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution .

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 31, 2017 6:11 AM