ASP.NET 会话状态 Sql Server 模式

发布于 2024-10-25 23:26:41 字数 636 浏览 0 评论 0原文

我的问题是关于上面写的标题。目前我有一些依赖会话来完成工作的功能。当保留默认值时,一切都会顺利进行。但是,我不喜欢在崩溃时丢失会话的想法,并且我已修改 web.config 以使用 Sql Server 模式。

我已经使用 aspnet_sql.exe 运行了必要的命令来在数据库中创建表和存储过程,

我能够将值存储到数据库中,但尚未成功检索它们。由于它在 InProc 模式下运行良好,我想我可以放心地假设我的代码没有任何问题。

这是我的 web.config 设置:

        <sessionState mode="SQLServer" allowCustomSqlDatabase="true" cookieless="false" timeout="120" sqlCommandTimeout="30" compressionEnabled="true" 
                                sqlConnectionString="Data Source=.\SQLExpress;Initial Catalog=mytest;Persist Security Info=True;User ID=mytest;Password=mytest;"/>

非常感谢您的帮助,提前致谢。

My problem is regarding title written above. Currently I have some functions that rely on Sessions to do the job. Everything works smoothly when it is left to default. However, I do not like the idea of losing sessions in case of crash and I have modified my web.config to use Sql Server mode instead.

I have run the necessary commands to create tables and stored procedures in my database using aspnet_sql.exe

I am able to store values into the database but have not been successful retrieving them. Since it has been working fine with InProc mode, I think I can safely assume that there's nothing wrong with my codes.

Here's my web.config setting:

        <sessionState mode="SQLServer" allowCustomSqlDatabase="true" cookieless="false" timeout="120" sqlCommandTimeout="30" compressionEnabled="true" 
                                sqlConnectionString="Data Source=.\SQLExpress;Initial Catalog=mytest;Persist Security Info=True;User ID=mytest;Password=mytest;"/>

Your help is very much appreciated, thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡写薰衣草的香 2024-11-01 23:26:41

假设您的代码没有错误是不安全的。我想到的一种情况是,您正在缓存的对象未标记为 可序列化。通常,这会抛出异常,并且您会意识到这一点。但是,如果您还在 catch 块中抑制异常,那么您似乎能够存储值,但无法检索它们。

为什么突然需要使所有对象可序列化?好吧,当您使用 InProc 时,对象存储在内存中。但是,当您使用 SQL 时,需要将它们写入(也称为序列化)数据库。

在代码中:

try {
    Session["MyKey"] = SomeNonSerializableObject
}
catch (Exception e)
{
    ' Suppress all exceptions 
}

我还将确认您的 ASPState 用户(示例中的 mytest)拥有他们所需的所有正确权限。测试这一点的简单方法是让他们成为 ASPState 数据库临时数据库的 db_owner。如果这解决了问题,那么您就知道这是数据库权限问题。

我听到你问为什么是 tempdb?好吧,默认情况下,会话状态表将在 tempdb 中创建。

It's not safe to assume that your code is not at fault. One scenario that comes to mind is that the objects you are caching are not marked as serializable. Ordinarily, this would throw an Exception, and you would be aware of it. However if you were also suppressing exceptions in a catch block, then you would seemingly be able to store values, but not retrieve them.

Why do you suddenly need to make all your objects serializable? Well, when you are using InProc, you're objects are stored in memory. However, when you use SQL, they need to be written (AKA serialized) to the database.

In code:

try {
    Session["MyKey"] = SomeNonSerializableObject
}
catch (Exception e)
{
    ' Suppress all exceptions 
}

I would also confirm that your ASPState user (mytest in your example) has all the correct permissions they require. The easy way to test this is to make them a db_owner on the ASPState database AND the temp database. If this fixes the problem, then you know that it's a DB permissions issue.

Why the tempdb I hear you ask? Well, by default, the session state tables will be created in the tempdb.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文