【问题标题】:Windows service gives error 1053 - System.IO.DirectoryNotFoundExceptionWindows 服务给出错误 1053 - System.IO.DirectoryNotFoundException
【发布时间】:2010-06-28 11:40:37
【问题描述】:

我有一个小型控制台应用程序,其中包含一个用 c# 编写的 Web 服务器。尝试将其转换为 Windows 服务时,我收到错误 1053,当我查看错误日志时它显示:

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,但没有成功。我总是得到同样的错误。 我是否设置了某种权限/安全性以便我的服务访问此文件夹? 网络服务器基于一个 codeplex 项目:http://webserver.codeplex.com/

我的 onstart 和 onstop 方法:

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

【问题讨论】:

    标签: c# exception windows-services


    【解决方案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);
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-11-09
        • 1970-01-01
        • 2014-08-13
        • 1970-01-01
        • 1970-01-01
        • 2010-10-22
        • 1970-01-01
        • 2011-11-16
        相关资源
        最近更新 更多