views:

40

answers:

2

I have a small console app containing a web server written in c#. When trying to convert it to windows service, i get a error 1053, and when i view the error log it shows:

Application: YCSWebServerService.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.DirectoryNotFoundException
Stack:
   at HttpServer.Resources.FileResources.Add(System.String, System.String)
   at HttpServer.Resources.FileResources..ctor(System.String, System.String)
   at YCSWebServer.WebServer..ctor(SerialCommunication.SerialCommunicationWrapper, YCSInterfaces.IBuilder, SerialCommunication.SerialCommunication)
   at YCSConfiguration.Builder..ctor()
   at YCSWebServerService.YCSWebServerService..ctor()
   at YCSWebServerService.Program.Main()

which is due to the following code:

server = new Server();

// Where to find the html page to render
server.Resources.Add(new FileResources("/", ".\\Webpages"));
server.Add(new FileModule(server.Resources, false));

//Create a http listener
HttpListener listener = HttpListener.Create(IPAddress.Any, 8888);

// use one http listener.
server.Add(listener);

server.RequestReceived += ServerRequestReceived;

// start server, can have max 10 pending accepts.
server.Start(10);

I have trying setting a relative path to where my html files are located, and i have trying giving it a fixed path fx: c:\webpages, but without success. I always get the same error. Do i have set some kind of permission/security in order for my service to access this folder?? The webserver is based on a codeplex project: http://webserver.codeplex.com/

My onstart and onstop methods:

public partial class YCSWebServerService : ServiceBase
    {
        private Builder _builder;
        public YCSWebServerService()
        {
            InitializeComponent();
            _builder = new Builder();
        }

        protected override void OnStart(string[] args)
        {
            _builder.Start();
        }

        protected override void OnStop()
        {
            _builder.Stop();
        }
    }
A: 

On the server, if do Start -> Run, type services.msc (on server 2008 just Start -> type services.msc) and press enter, you will get the list of services. Find yours in the list and view the properties. If you go to the Log On tab, you will see the user that the service is configured as. If it is set for Local System, you shouldn't have to worry about security permissions of the files/directories. If it is set for something else, make sure that account has access to the files/directories.

Nate Pinchot
+1  A: 

I had this exact same problem about a week ago. The problem is there is no "startup path" setting for windows services, so relative paths to files in the solution won't work.

This function gets the folder the service is executing in, you need to prepend that to "\\Webpages" and not use .\\WebPages"

Hope this helps

private static string ExecutingFolder()
{
    string exeUri = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
    Uri uri = new Uri(exeUri);
    string physicalExePath = uri.LocalPath;
    return IO.Path.GetDirectoryName(physicalExePath);
}
Binary Worrier
That did the trick! thanks!
lmkk