Global.asax 中的 Server.MapPath
在global.asax.cs的Application_Start事件中,我添加了一些初始化代码。此代码在生成的线程(使用 new Thread() 创建)中运行。初始化代码使用Server.MapPath来获取文件的物理路径。但是很多次我在日志文件中收到错误: 服务器操作在此上下文中不可用 无论如何,我都会捕捉到异常。初始化并不重要,因为即使它不成功,对我来说也不会成为问题。 我的问题是:
- 为什么我会收到服务器不可用错误?
- 处理的异常是否会在应用程序正常执行期间导致诸如无效视图状态之类的任何问题。我们在日志中注意到了很多这样的内容 - 无效的视图状态。 我相信不能。如果我错了,请纠正我。
谢谢维卡斯
In Application_Start event in global.asax.cs, I have added some initialization code. This code is run in a spawned thread (created using new Thread()). The initialization code uses Server.MapPath to get the physical path of a file. However many times I get an error in the log files :
Server Operation is not available in this context
In any case I am catching the exception. The initialization is not critical since even if it does not succeed, it will not be a problem for me.
My question is:
- Why am I getting the Server not available error?
- Can the handled exception cause any problems like Invalid Viewstate during normal execution of the application. We noticed a lot of these - Invalid Viewstate later in the logs.
I believe it cannot. Please correct me if I am wrong.
Thanks
Vikas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Server 对象通过 HttpContext.Current 引用,即相对于当前请求。我怀疑它无法从
Application_Start
获取,因为此时不存在任何请求。不管怎样,它不能从单独的线程中可靠地获得,因为线程彼此独立运行,因此任何请求可能在您的线程尝试访问它之前已经开始或完成。尝试使用 HostingEnvironment.MapPath() 来代替,因为这是一个静态方法。
the Server object is referenced via
HttpContext.Current
i.e. relative to the current request. I suspect its not available fromApplication_Start
since there is no request present at that point. Regardless it won't reliably be available from within separate thread because the threads run independently from each other, therefore any request may have started or finished before your thread tries to access it.Try using
HostingEnvironment.MapPath()
instead as that is a static method.