Type MysqLType = Type.GetType("MysqL.Web.SessionState.MysqLSessionStateStore,MysqL.Web,Version=6.6.4.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d",true,true); SessionStateStoreProviderBase provider = (SessionStateStoreProviderBase)Activator.CreateInstance(MysqLType); System.Collections.Specialized.NameValueCollection providerConfig = new System.Collections.Specialized.NameValueCollection(); providerConfig.Add("connectionStringName","LocalMysqLServer"); providerConfig.Add("applicationName","/"); providerConfig.Add("autogenerateschema","True"); providerConfig.Add("enableExpireCallback","False"); provider.Initialize("MysqLSessionStateProvider",providerConfig); HttpContext context = new HttpContext( new HttpRequest("fake","http://localhost:14359",""),new HttpResponse(new StringWriter())); SessionStateStoreData storeData = provider.CreateNewStoreData(context,100); System.Web.SessionState.HttpSessionStateContainer sessionStateContainer = new System.Web.SessionState.HttpSessionStateContainer( "mySession",storeData.Items,storeData.StaticObjects,100,System.Web.HttpCookieMode.UseDeviceProfile,SessionStateMode.Custom,false ); Type sessionStateType = Type.GetType("System.Web.SessionState.HttpSessionState,System.Web,Version=4.0.0.0,PublicKeyToken=b03f5f7f11d50a3a"); HttpSessionState sessionState = (HttpSessionState)sessionStateType .GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance,null,new Type[] { typeof(IHttpSessionState) },null) .Invoke(new object[] { ssessionStateContainer }); provider.InitializeRequest(context);
一切似乎工作正常,因为我能够在我创建的会话中编写一些值,并且我能够从那里读取它们:
sessionStateContainer.Add("SomeKey","SomeValue"); var test = sessionStateContainer["SomeKey"]; Console.WriteLine("var test: " + test.ToString()); // outputs "var test: SomeValue" sessionState.Add("AnotherKey","SomeOtherValue"); var test2 = sessionState["AnotherKey"]; Console.WriteLine("var test2: " + test2.ToString()); // outputs "var test2: SomeOtherValue" // End the request provider.EndRequest(context);
这是有趣的部分 – 当我检查数据库中的my_aspnet_sessions表时,那里什么都没有.
所以我想知道数据写在哪里或者我做错了什么?
更新:
即使这个问题不再是一个阻止者,我仍然很想知道,如果有人想尝试一下,这里是我使用过的connectionString:
<connectionStrings> <remove name="LocalMysqLServer" /> <add name="LocalMysqLServer" connectionString="server=localhost;database=session_test;User Id=user;password=pass" providerName="MysqL.Data.MysqLClient" /> </connectionStrings>
你还需要MySQL Connector NET.
解决方法
The SessionStateModule object calls the SetAndReleaseItemExclusive method at the end of a request,during the ReleaseRequestState event,to insert current session-item information into the data store or update existing session-item information in the data store with current values,to update the expiration time on the item,and to release the lock on the data. Only session data for the current application that matches the supplied session id and lockId values is updated. For more information about locking,see “Locking Session Store Data” in the SessionStateStoreProviderBase class overview.
要深入了解详细信息,您可以查看relevant files from the mono codebase.
鉴于此,要在数据库中查看会话数据,您应执行以下操作:
object storeLockId = new object(); sessionStateContainer.Add("SomeKey","SomeValue"); var test = sessionStateContainer["SomeKey"]; Console.WriteLine("var test: " + test.ToString()); sessionState.Add("AnotherKey","SomeOtherValue"); var test2 = sessionState["AnotherKey"]; Console.WriteLine("var test2: " + test2.ToString()); // End the request provider.SetAndReleaseItemExclusive (context,sessionStateContainer.SessionID,storeData,storeLockId,true); provider.EndRequest(context);