6

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.

Intermernet
  • 18,604
  • 4
  • 49
  • 61
DotnetSparrow
  • 27,428
  • 62
  • 183
  • 316

3 Answers3

6

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

The Evil Greebo
  • 7,013
  • 3
  • 28
  • 55
  • 2
    I dont need the assembly path rather I need file path. actually I need the alternative of this: HttpContext.Current.Server.MapPath("~/TestDevice/Data.xml") – DotnetSparrow Jun 10 '11 at 09:50
  • 1
    So what you're saying is you want the service to know the location of a data file located under a web application. Problem with that is the web application could get moved. If you want both a service and a web application to be able to have access to the same location then you might want to define that path location in a config file for each application, both pointing to the same place, and store your data in that location instead of under the web site's folder tree. – The Evil Greebo Jun 10 '11 at 13:38
2

This method works much better:

string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
Lucia Minerba
  • 119
  • 1
  • 8
1

It could be that when you are using windows service, you are no longer running a web app, therefore HttpContext and web server is not available. Try using System.IO.File for mapping, see whether that takes you to the correct directory.

Edit

private String yourFullPath = System.IO.Path.GetFullPath("/YourDirectory") + @"\";
  • @vikp: yes it works with physical path. What is alternative. I dont want to give physical path in class library – DotnetSparrow Jun 10 '11 at 09:46
  • You are not giving physical path. All you have to do is specify directory name, which will reside in the root of your DLL. –  Jun 10 '11 at 09:50
  • @vikp: System.IO.file is giving error. What is exact syntax ? – DotnetSparrow Jun 10 '11 at 09:51
  • 1
    @vikp System.IO.Path.GetFullPath("/TestDevice/Data.xml") returns "C:\\TestDevice\\Data.xml" instead of the actual directory path – DotnetSparrow Jun 10 '11 at 13:06
  • Yep, I have a directory on a C drive which is mapped to a virtual directory. I upload files into this directory. Alternative is of course to use reflection on assemblies. –  Jun 10 '11 at 13:32