我有一个 Windows 服务,它使用来自具有相同 asp.net 解决方案的类库的方法。在类库中,我有一个带有以下行的方法:
reader = XmlReader.Create(HttpContext.Current.Server
.MapPath("~/TestDevice/Data.xml"), settings);
当控制来到这条线时。我得到异常(exception)。我尝试调试代码,发现当服务尝试访问此方法时
HttpContext.Current.Server
是 null
.什么是替代语法。我试图从 Web 应用程序访问这个类库方法,它工作正常。
请提出解决方案。
最佳答案
HttpContext.Current 返回 null,因为您的 Windows 服务不在 IIS 或其他一些 Web 服务器提供商的保护伞下运行。
但是,您可以使用反射找到服务的执行路径:
System.Reflection.Assembly.GetExecutingAssembly().Location
^ 应该返回执行服务的路径..
关于asp.net - HttpContext.Current.Server 空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6304532/