ASP.NET 2.0:为什么我的类在本地计算机上运行时可以访问 Session 对象,但在将其部署到服务器时却不能访问?

发布于 2024-10-22 18:22:38 字数 1061 浏览 0 评论 0原文

我有一个在 ASP.NET 2.0 应用程序中使用的自定义 MembershipProvider。在扩展成员资格提供程序的类内部,我有一个名为 AttemptLogin() 的函数,该函数在用户有效时设置会话变量。在该函数内部,有一些类似于以下内容的会话变量分配:

HttpContext.Current.Session["id"] = "12345";

global.asax 中的 Application_BeginRequest 函数正在调用 AttemptLogin()。当我在 Visual Studio 中打开该代码,然后通过单击“开始调试”在内置开发服务器中运行它时,该代码工作正常。但是,当我将其部署到我们的测试服务器(以 32 位模式运行 IIS 的 Windows 2003 Server 64 位)时,执行到上面的代码时会中断,并给出以下消息:

[NullReferenceException: Object reference not set to an instance of an object.]
   CustomMembershipProvider.AttemptLogin() in c:\Inetpub\wwwroot\Josh\App_Code\CustomMembershipProvider.cs:1097
   ASP.global_asax.Application_BeginRequest(Object sender, EventArgs e) in c:\Inetpub\wwwroot\Josh\Global.asax:14
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +68
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

How can I access the session state success in这种情况?为什么它可以在本地运行而不是在服务器上运行?

I have a custom MembershipProvider that I'm using in an ASP.NET 2.0 application. Inside of the class that extends membership provider, I have a function called AttemptLogin() that sets session variables if the user is valid. Inside that function, are a number of session variable assignments similar to the following:

HttpContext.Current.Session["id"] = "12345";

AttemptLogin() is being called by the Application_BeginRequest function in global.asax. This code works fine when I open it in visual studio, and then run it in the built-in development server by clicking "Start Debugging". However, when I deploy it to our testing server (Windows 2003 Server 64-bit running IIS in 32-bit mode), execution breaks when it reaches the code above, giving me the following message:

[NullReferenceException: Object reference not set to an instance of an object.]
   CustomMembershipProvider.AttemptLogin() in c:\Inetpub\wwwroot\Josh\App_Code\CustomMembershipProvider.cs:1097
   ASP.global_asax.Application_BeginRequest(Object sender, EventArgs e) in c:\Inetpub\wwwroot\Josh\Global.asax:14
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +68
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

How can I access the session state successfully in this situation? Why does it work locally and not on the server?

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

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

发布评论

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

评论(2

九公里浅绿 2024-10-29 18:22:38

会话变量在 Application_BeginRequest 处不可用,您应该稍后通过处理 Application_AcquireRequestState 在你的 global.asax 中,老实说我很困惑为什么你在开发盒上没有得到空引用。

Session Variables aren't available at Application_BeginRequest you should look later in the pipeline by handling the Application_AcquireRequestState in your global.asax, I'm honestly puzzled why you don't get a null ref on your dev box.

神回复 2024-10-29 18:22:38

检查服务器上的 web.config 文件并确保启用了 sessionState 模式

<sessionState mode="InProc" />

Check the web.config file on the server and make sure sessionState mode is enabled

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