简体   繁体   中英

Windows service using C#

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 but 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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