Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
577 views
in Technique[技术] by (71.8m points)

asp.net - HttpContext.Current.Server null

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);

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.

I tried to access this class library method from web application and it works fine.

Please suggest solution.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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..


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...