Error 5 : Access Denied when starting windows service

前端 未结 30 2427
滥情空心
滥情空心 2020-12-04 18:49

I\'m getting this error when I try to start a windows service I\'ve created in C#:

\"alt

My Code

30条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-04 19:25

    This error happens when there is a error in your OnStart method. You cannot open a host directly in OnStart method because it will not actually open when it is called, but instead it will wait for the control. So you have to use a thread. This is my example.

    public partial class Service1 : ServiceBase
    {
        ServiceHost host;
        Thread hostThread;
        public Service1()
        {
            InitializeComponent();
             hostThread= new Thread(new ThreadStart(StartHosting));
    
        }
    
        protected override void OnStart(string[] args)
        {
            hostThread.Start();
        }
    
        protected void StartHosting()
        {
            host = new ServiceHost(typeof(WCFAuth.Service.AuthService));
            host.Open();
        }
    
        protected override void OnStop()
        {
            if (host != null)
                host.Close();
        }
    }
    

提交回复
热议问题