Multiple instances of the same application as the windows service?

I have an application that manages heavy processing for my project and need to convert it to Windows Service. I need to allow multiple instances of application processing versions to run, which seems to be a pretty normal requirement.

I can see at least three approaches for this:

  • Create one installed directory (EXE, DLL, config), but install it as multiple service instances.
  • If a single instance of services starts several instances of itself after starting, a la Apache.
  • Ask a single service instance to invoke multiple threads running in the same process space.

My intention was approach number 1, but I continued to overcome the limitations, both in design and in documents for services:

  • Are OnStart () parameters passed by standard service mechanisms on the system unattended? If so, when / why?
  • Passing runtime parameters through the ImageKey registry seems kludge, is there a more efficient mechanism?
  • I got an application to install / uninstall myself as a pair of services ("XYZ # 1", "XYZ # 2", ...), using ImageKey to pass it an instance number of the command line parameter ("- x 1", "-x 2 "), but I missed something. When you try to start a service, it will fail: "The executable program for which this service is configured to start does not run the service.

So the questions are:

  • Is there a brief description of what happens when the service starts, especially for situations where the ServiceName is not hardcoded (see above).
  • Has anyone used approach number 1 successfully? Any comments?

NOTE. I posed the problem using approach number 3, so I can not justify a lot of time to understand this. But I thought that someone might have information on how to implement No. 1, or about good reasons why this is not a good idea.

[Edit] I initially had the 4th option (install multiple copies of the application on the hard drive), but I deleted it because it just feels, um, a hacker. That is why I said "at least three approaches."

However, if the application is not recompiled, it must dynamically set its ServiceName, therefore, it has a solution to the third problem / problem above. Thus, if an instance does not need to modify its installation files, # 1 should work fine with the N configuration files in the directory and in the registry entry indicating which instance should use.

+7
c # windows-services
source share
3 answers

Although I cannot answer your questions related to option No. 1, I can say that option No. 2 worked very well for us. We wanted to create an application domain for each "child" service to run under and use a different configuration file for each of them. In our service configuration file, we saved the application domains to run and the configuration file. Therefore, for each entry, we simply created an application domain, installed a configuration file, etc., and we went. This separation of configuration allowed us to easily uniquely determine the location of ports and log files for each instance. An added benefit for us was that we wrote our โ€œchild serviceโ€ as an exe command line and simply called AppDomain ExecuteAssembly () in a new thread for each โ€œchildโ€ service. The only "clunk" in the solution was closing, we did not bother to create a "good" solution for it.

February 2012 update

Some time ago, we started using the named services (for example, SQL Server). I described the entire process in detail on my blog in the series "Creating a Windows Service - Part 1 through Part 7 ". They take you through the creation of a hybrid command line / windows, complete with self-installation. The following goals in which they met:

  • Creating a service that can also be used from the console
  • Own logging of service start / close and other actions
  • Resolving multiple instances using command line arguments
  • Self-service service and event log installation
  • Correct Service Exception and Error Logging
  • Manage startup, shutdown and reboot options
  • Handling custom maintenance commands, credentials, and session events
  • Security and access control settings

The full Visual Studio project template is available in the last article in the Building Windows Service series - Part 7: Finishing Strokes .

+6
source share
0
source share

There is option number 4, which I successfully use in my project, for example, "named instances".

Each installation of your application has its own name, and each installation has its own service. They are completely independent and isolated from each other. MS SQL Server uses this model if you try to install it several times on the same machine.

0
source share

All Articles