Asp.net(VB),在线程里使用 HttpContext.Current 为什么返回 Nothing 呢?

QB97 2004-03-17 01:52:03
Asp.net(VB),在线程里使用 HttpContext.Current 为什么返回 Nothing 呢?
---------------------------------------
就是在ClassA里开始一个线程ClassB,而ClassB里调用的ClassC中使用
HttpContext.Current
但 HttpContext.Current 却返回的是 Nothing ???
---------------------------------------
麻烦各位指点小弟一下,都折腾我一周多了还是弄不明白……5555~~~~


Asp.net(VB),在线程里使用 HttpContext.Current 为什么返回 Nothing 呢?
---------------------------------------
就是在ClassA里开始一个线程ClassB,而ClassB里调用的ClassC中使用
HttpContext.Current
但 HttpContext.Current 却返回的是 Nothing ???


附:

大概就是下面这种环境:
---------------------------------------
1 Public Class ClassA
2 ……
3 Public Sub Send()
4 Dim cClassB As New cClassB
5 Dim ThreadClassB As New Thread(AddressOf cClassB.Send)
6 ThreadClassB.Start()
7 ……
8 End Sub
9 End Class
---------------------------------------
10 Public Class ClassB
11 ……
12 Public Sub Send()
13 Dim cClassC As New ClassC
14 Dim myModuleSettings As ModuleSettings = cClassC.GetSettings
15 ……
16 End Sub
17 End Class
--------------------------------------
18 Public Class ClassC
19 ……
20 Public Shared Function GetSettings() As ModuleSettings
21 Dim context As HttpContext = HttpContext.Current
22 Dim data As ModuleSettings = CType(context.Cache("Settings"),
ModuleSettings)
23 ……
24 End Function
25 End Class
---------------------------------------
26 Public Class ModuleSettings
27 ……
28 End Class
---------------------------------------
出现问题:
---------------------------------------
在 
----------
22 Dim data As ModuleSettings = CType(context.Cache("Settings"),
ModuleSettings) 
----------
产生 System.NullReferenceException 异常。
单步调试发现:
----------
21 Dim context As HttpContext = HttpContext.Current
----------
中 HttpContext.Current 返回 Nothing ???
---------------------------------------
...全文
77 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
QB97 2004-03-19
  • 打赏
  • 举报
回复
明白了!哈,非常感谢saucer兄了!
saucer 2004-03-17
  • 打赏
  • 举报
回复
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=%23KFRJgoTDHA.2128%40TK2MSFTNGP12.phx.gbl

"....
The Current static method will work without any problem on the current thread. If you want to create new thread and work with the context you can pass it as parameter to the thread. You can see my example:

http://www.developersdex.com/gurus/code/662.asp
...."

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧