2010-06-28 229 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),然後按回車鍵,您將看到服務列表。在列表中查找您的屬性並查看屬性。如果您轉到登錄選項卡,您將看到該用戶該服務配置爲。如果它設置爲本地系統,則不必擔心文件/目錄的安全權限。如果它被設置爲其他內容,請確保該帳戶可以訪問文件/目錄。

相關問題