2010-06-28 82 views
1

我有一个小型控制台应用程序,其中包含用c#编写的Web服务器。当试图将其转换为Windows服务,我得到一个错误1053,当我查看错误日志它显示:Windows服务提供错误1053 - System.IO.DirectoryNotFoundException

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

这是由于下面的代码:

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

我已经尝试设置我的html文件所在位置的相对路径,我试图给它一个固定路径fx:c:\ webpages,但没有成功。我总是得到同样的错误。 为了让我的服务能够访问此文件夹,我是否设置了某种权限/安全性? 的Web服务器是基于一个CodePlex项目:http://webserver.codeplex.com/

我的OnStart和的onStop方法:

​​

回答

1

我在一个星期前有这个完全相同的问题。问题是windows服务没有“启动路径”设置,因此解决方案中文件的相对路径将不起作用。

该函数获得服务正在执行的文件夹,您需要预先设置,为"\\Webpages",而不是使用.\\WebPages"

希望这有助于

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); 
} 
+0

这奏效了!谢谢! – lmkk 2010-06-28 13:20:18

0

在服务器上,如果你开始 - >运行,输入services.msc(2008年服务器刚刚开始 - >型services.msc),然后按回车键,您将看到服务列表。在列表中查找您的属性并查看属性。如果您转到登录选项卡,您将看到该用户该服务配置为。如果它设置为本地系统,则不必担心文件/目录的安全权限。如果它被设置为其他内容,请确保该帐户可以访问文件/目录。

相关问题