1

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.

System.IO.Path.GetFullPath("/TestDevice/Data.xml") returns C:\\TestDevice\\Data.xml instead of the actual directory path

I want to get full path of the folder.

Please suggest solution.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
DotnetSparrow
  • 27,428
  • 62
  • 183
  • 316
  • What folder do you want to get the path of? Are you suggesting that the windows service should somehow know the root directory of your asp.net application as well? – Joel Etherton Jun 10 '11 at 13:33
  • http://stackoverflow.com/questions/6304532/httpcontext-current-server-null - this is a continuation of a previously asked question. – The Evil Greebo Jun 10 '11 at 13:35
  • @Joel: Windows service is accessing a class library. which has a function with this line (trying to populate XMLreader from xml file). – DotnetSparrow Jun 10 '11 at 13:49

3 Answers3

3

http://msdn.microsoft.com/en-us/library/aa457089.aspx

string path;
path = System.IO.Path.GetDirectoryName( 
  System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
Joel Martinez
  • 46,929
  • 26
  • 130
  • 185
  • 2
    This assumes that his windows service is running in the same location as the website, which is probably _not_ the case. – CodingGorilla Jun 10 '11 at 13:30
  • @Coding Gorilla - I up-voted your comment, but I think it might be more accurate to say `which SHOULD not be the case`. Given the nature of the question, it's quite probably that it is the case, though. – Joel Etherton Jun 10 '11 at 13:38
  • Thank you for the up-vote; you're probably right though, I would hope he wouldn't run a windows service out of a website, but I guess anything is possible. – CodingGorilla Jun 10 '11 at 13:40
  • @Joel: I used above code and path variable has bin folder's path luike this: "file:\\D:\\Freelance Work\\SuperExpert\\git EnviroTrack\\EnviroTrack\\EnviroTrackerService\\bin\\Release" – DotnetSparrow Jun 10 '11 at 14:53
  • Yes, you have to understand that you are running in different contexts in both cases. So you will need to have special code. One way you could do it is by checking the http context. If it's null, you know you're running in the windows service and you can prepend "../" or something similar to get at the right relative path, otherwise, if the httpcontext is not null you can just use the Server.MapPath as you were. – Joel Martinez Jun 10 '11 at 17:22
2

You will need a configuration file that can have the "root" directory set specifically. This will allow the windows service to know what directory to place files into regardless of where its executable sits and regardless of where the asp.net site is configured to run.

Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
  • how should I modify this line: reader = XmlReader.Create(HttpContext.Current.Server.MapPath("~/TestDevice/Data.xml"), settings); – DotnetSparrow Jun 10 '11 at 13:41
  • @DotnetSparrow: Since it's a windows service, there will not be any HttpContext in use. The only way I can see to do it is to apply an app.config file in your windows service project, and have an AppSetting called "RootWebSiteDirectory" or something that you use to do it: `XmlReader.Create(ConfigurationManager.AppSettings("RootWebSiteDirectory") + "/TestDevice/Data.xml", settings);` – Joel Etherton Jun 10 '11 at 13:51
0

I don't think the ~ will work in this case, you will need to provide a relative path. Something like "../../TestDevice/Data.xml" should work.

toby
  • 885
  • 3
  • 10
  • 21
  • What if his windows service executable isn't being run from `../../TestDevice/`? What if it's `../SomeDirectory/TestDevice/`? – Joel Etherton Jun 10 '11 at 13:35