Hav*_*ock 7 c# mysql session-state
我正在研究一个原型程序,它应该测试ASP.NET的不同(SessionState,Profile等)提供程序,即MySQL,Oracle等.目前我正在使用MySQL提供程序.我刚刚设法实例化了提供程序,SessionStateContainer和SessionState本身.
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,
true,
System.Web.HttpCookieMode.UseDeviceProfile,
SessionStateMode.Custom,
false
);
Type sessionStateType = Type.GetType("System.Web.SessionState.HttpSessionState, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
HttpSessionState sessionState =
(HttpSessionState)sessionStateType
.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(IHttpSessionState) }, null)
.Invoke(new object[] { ssessionStateContainer });
provider.InitializeRequest(context);
Run Code Online (Sandbox Code Playgroud)
一切似乎工作正常,因为我能够在我创建的会话中编写一些值,并且我能够从那里读取它们:
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);
Run Code Online (Sandbox Code Playgroud)
这是有趣的部分 - 当我检查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>
Run Code Online (Sandbox Code Playgroud)
你还需要MySQL Connector NET.
您在数据库中看不到任何数据,因为SessionStateModule等待请求结束来调用命令SetAndReleaseItemExclusive。数据将保留在内存中,直到调用该命令为止。事实上,文档指出:
SessionStateModule 对象在请求结束时、在 ReleaseRequestState 事件期间调用 SetAndReleaseItemExclusive 方法,以将当前会话项信息插入数据存储中或使用当前值更新数据存储中的现有会话项信息,以更新过期时间项,并释放数据上的锁定。仅更新与所提供的会话 id 和 lockId 值匹配的当前应用程序的会话数据。有关锁定的更多信息,请参阅 SessionStateStoreProviderBase 类概述中的“锁定会话存储数据”。
要进一步深入了解细节,您可以查看mono 代码库中的相关文件。
鉴于此,要查看数据库中的会话数据,您应该执行如下操作:
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);
Run Code Online (Sandbox Code Playgroud)