ASP。NET会话状态Sql Server模式

时间:2023-01-16 09:30:13

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.

我的问题是关于上面写的题目。目前我有一些功能依赖于会话来完成这项工作。如果任其违约,一切都会顺利进行。但是,我不喜欢在崩溃的情况下丢失会话的想法,而且我已经修改了我的web。配置为使用Sql Server模式。

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

我已经运行了必要的命令,以便使用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.

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

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.

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

1 个解决方案

#1


4  

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.

假定您的代码没有错误是不安全的。我想到的一个场景是您正在缓存的对象没有标记为serializable。通常,这会抛出一个异常,您应该知道它。但是,如果您还在捕获块中抑制异常,那么您似乎可以存储值,但不能检索它们。

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.

为什么突然需要让所有对象都是可序列化的?当你使用InProc时,你的对象存储在内存中。但是,当您使用SQL时,需要将它们写到数据库中(即序列化)。

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.

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

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

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

#1


4  

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.

假定您的代码没有错误是不安全的。我想到的一个场景是您正在缓存的对象没有标记为serializable。通常,这会抛出一个异常,您应该知道它。但是,如果您还在捕获块中抑制异常,那么您似乎可以存储值,但不能检索它们。

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.

为什么突然需要让所有对象都是可序列化的?当你使用InProc时,你的对象存储在内存中。但是,当您使用SQL时,需要将它们写到数据库中(即序列化)。

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.

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

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

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