简体   繁体   English

Windows Service在调试中执行代码,但在发布中不执行

[英]Windows Service executes code in debug, but not in release

I'm working on a Windows Service that hosts a WCF-webservice, which works when I run it in debug mode: 我正在托管WCF-webservice的Windows Service上运行,当我在调试模式下运行它时,它可以正常工作:

#if DEBUG
        SynchroService myService = new SynchroService();
        myService.OnDebugStart();
#else
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new SynchroService() 
            };

            ServiceBase.Run(ServicesToRun);

#endif

The OnDebugStart() just calls the OnStart() method of the Windows service and the thread that runs is just a thread that runs infinitely (untill the OnStop() is called) to check if the service is still alive & online. OnDebugStart()只是调用Windows服务的OnStart()方法,运行的线程只是无限运行的线程(直到调用OnStop()为止),以检查该服务是否仍然在线和在线。

        protected override void OnStart(string[] args)
    {
        try
        {
            ServiceStatus serviceStatus = new ServiceStatus();
            serviceStatus.dwCurrentState = ServiceState.SERVICE_START_PENDING;
            SetServiceStatus(this.ServiceHandle, ref serviceStatus);

            syncService = new SynchroComService();
            myServiceHost = new ServiceHost(syncService);
            myServiceHost.Open();
            syncService.StartListening();

            thread = new Thread(CheckHost);
            thread.Start();
            serviceStatus.dwCurrentState = ServiceState.SERVICE_RUNNING;
            SetServiceStatus(this.ServiceHandle, ref serviceStatus);
        }
        catch (Exception ex)
        {
            WriteError(ex);
        }
    }

Now, when I create the WCF service (syncService), it should run code to start up SQLDependency and start listening to the database. 现在,当我创建WCF服务(syncService)时,它应该运行代码以启动SQLDependency并开始侦听数据库。 This works perfectly fine when I start it in debugmode, but when I run it in release, the service gets hosted, but the code doesn't execute. 当我在debugmode中启动它时,它工作得很好,但是当我在release中运行它时,该服务将被托管,但是代码不会执行。

I've looked everywhere for an answer or problems of the same type, but I've come up with nothing. 我到处都在寻找相同类型的答案或问题,但是我什么也没想出来。 So if anyone has every come across a problem of the same type, please do tell how you solved it. 因此,如果有人遇到相同类型的问题,请告诉您如何解决。

Kind regards 亲切的问候

The problem wasn't my code, it was with the permissions when running the service. 问题不在于我的代码,而是与运行服务时的权限有关。
The service was being started as local systemaccount, but it didn't have permissions to run the service/execute the code. 该服务已作为本地系统帐户启动,但没有运行该服务/执行代码的权限。

To change the account that runs the service: 要更改运行服务的帐户:

  1. Open services.msc 打开services.msc
  2. Right click the service name 右键单击服务名称
  3. Go to the logon tab and change the account to the/an account that has the correct permissions 转到登录选项卡,然后将帐户更改为具有正确权限的帐户
  4. Restart the service 重新启动服务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM