HttpContext.Current.Server空 [英] HttpContext.Current.Server null
问题描述
我有使用方法,由一个类库用同样的asp.net解决方案Windows服务。在类库,我有以下行的方法:
I have a windows service which is using a method from a class library with same asp.net solution. in class library, I have a method with following line:
reader = XmlReader.Create(HttpContext.Current.Server
.MapPath("~/TestDevice/Data.xml"), settings);
在控制谈到此行。我得到异常。我试着调试code和发现,当服务尝试然后访问该方法 HttpContext.Current.Server
是空
。什么是替代语法。
When control comes to this line. I get exception. I tried to debug the code and found that when service tries to access this method then HttpContext.Current.Server
is null
. What is alternative syntax.
我试图来访问Web应用程序这个类库方法,它工作正常。
I tried to access this class library method from web application and it works fine.
请提出解决方案。
推荐答案
HttpContext.Current是返回null,因为您的Windows服务没有IIS的雨伞或一些其他的Web服务器provder下运行。
HttpContext.Current is returning null because your Windows Service is not running under the umbrella of IIS or some other web server provder.
然而,你可以使用反射找到你的服务的执行路径:
However, you can find the executing path of your service using reflection:
System.Reflection.Assembly.GetExecutingAssembly().Location
^应返回执行服务的路径。
^ should return the path of the executing service..
这篇关于HttpContext.Current.Server空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!