开发者

Windows service using C#

开发者 https://www.devze.com 2023-03-15 19:03 出处:网络
Am trying to develop a windows service which listens to a rabbitMQ listener and stores the messages it listens into database...the code is working perfectly in debug mode bu开发者_高级运维t is not wor

Am trying to develop a windows service which listens to a rabbitMQ listener and stores the messages it listens into database...the code is working perfectly in debug mode bu开发者_高级运维t is not working as a windows service....

Debug and service code:

if (!Environment.UserInteractive)
{
    ServiceBase[] ServicesToRun;
    ServicesToRun = new ServiceBase[] 
    { 
        new RabService() 
    };
    ServiceBase.Run(ServicesToRun);
}
else
{
    var service = new RabService();
    service.OnStart(null);
    Thread.Sleep(Timeout.Infinite);
}

Any inputs would be appreciated!


If your connection string to the database is using windows security, it is possible that your debug user is able to access the database. But the windows service runs under another user.

So just to discard this option I would recommend the following:

  • Go to the services window. cmd SERVICES.MSC
  • Look for your windows service
  • Open Properties
  • LogOn properties
  • Select "This account" and please set an account that is granted to access the database.

Then restart the windows service and debug it.

Regards,


Please cross check whether you have done the following steps:

  1. After creating the windows service project go to the service class's design view(just double click the service1.cs class).

  2. In the design view right click and select Add Installer. This will create an Installer class named ProjectInstaller.cs. Without ProjectInstaller.cs or any error in configuring ProjectInstaller.cs may result in non-showing of the service in service console.

  3. Go to the design view of ProjectInstaller.cs you will find two installers there->

    a. ServiceInstaller1

    b. ServiceProcessInstaller1

  4. Right click ServiceInstaller1 and go to the properties tab

    a. Edit the ServiceName with the name you want to see your service in the service console

    b. Change the StartType to Automatic.

  5. Right click ServiceProcessInstaller1 and go to the properties tab

    a. Change the account to LocalService

Save and try it.

Hope this will help you.

0

精彩评论

暂无评论...
验证码 换一张
取 消