ASPNET : Switch between Session State Providers ?‏

孤者浪人 提交于 2019-12-11 01:36:25

问题


I want to develop a new custom session state provider or use an existing (distributed caching, sql ...). Our main website renders more than 10 000 000 visits per day.It is really important for us to provide an easy rollback/switch in case of an error or a performance hit. Change web.config is not optimal because we have more than 20 front end servers. Our idea is to switch between session provider (from our custom to InProc) with a simple config in database.

Is it possible to have multiple session state providers or easily switch between providers ?

i found here http://netpl.blogspot.fr/2007/06/wrapped-inprocsessionstatestore.html, a solution for having a generic wrapper, but it does not seems quite robust.

Thanks,


回答1:


Just for future references, it is not possible to dynamically change the session state provider.




回答2:


it is not.. totally true.. you can do it like this using Reflection:

var privateFieldFlags = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic;

//Get session state section
var sessionStateSection = ConfigurationManager.GetSection("system.web/sessionState") as SessionStateSection;
var values = typeof(ConfigurationElement).GetField("_values", privateFieldFlags).GetValue(sessionStateSection);
var entriesArray = values.GetType().BaseType.GetField("_entriesArray", privateFieldFlags).GetValue(values);

//Get "Mode" entry (index: 2)
var modeEntry = (entriesArray as System.Collections.ArrayList)[2];
var entryValue = modeEntry.GetType().GetField("Value", privateFieldFlags).GetValue(modeEntry);

//Change entry value to InProc
entryValue.GetType()
        .GetField("Value", privateFieldFlags)
        .SetValue(entryValue, System.Web.SessionState.SessionStateMode.InProc);

references:
Dynamic session state provider
http://www.answerandquestion.net/questions/4447903/dynamic-session-state-provider



来源:https://stackoverflow.com/questions/11667234/aspnet-switch-between-session-state-providers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!