gpt4 book ai didi

c# - 手动实例化的 SessionState 提供程序的问题

转载 作者:可可西里 更新时间:2023-11-01 06:38:09 25 4
gpt4 key购买 nike

我正在开发一个原型(prototype)程序,它应该测试 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);

一切似乎都运行良好,因为我可以在创建的 session 中写入一些值,并且可以从那里读取它们:

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 .

最佳答案

您在数据库中看不到任何数据,因为 SessionStateModule等待请求端调用命令SetAndReleaseItemExclusive .在调用此类命令之前,数据一直保存在内存中。事实上,文档指出:

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 .

鉴于此,要在数据库中查看您的 session 数据,您应该执行如下操作:

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);

关于c# - 手动实例化的 SessionState 提供程序的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15820806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com