1 executable, multiple services?

H

hpark2_1999

Hi!

I know how easy it is to create an NT services using .net framework
(using Visual Studio 2003).

Here is my problem.

I came across a situation where I would like to run 2 instances of a
service executable. From what I have seen the Service name has to be
specified as a "Name" property for the service.

Here is the gist of what I would like to do:
I have an executable which runs as a windows service.
This program connects to a database every 5 min. reads data, creates a
file.

Now, a need came up which I would like to connect to another database.

Database information is in configuration file so pointing to different
database consists of stopping the service, change the config file and
re-start the service.

However, now, I want to run the program to do the same thing on 2
different database simultaneously.

Even if I put 2 copies of executables in different directory, I can not
create 2 different instance of the same service using installTools
since the name of the service is embedded in the program itself (isn't
it??)

What would be the best way of doing this without keeping 2 different
version of the program?

Thanks.

Harry Park.
 
C

Chris Dunaway

Here is the gist of what I would like to do:
I have an executable which runs as a windows service.
This program connects to a database every 5 min. reads data, creates a
file.

Now, a need came up which I would like to connect to another database.

Is the functionality for the second database the same? Does it use the
same queries, tables etc. on the other database?
Database information is in configuration file so pointing to different
database consists of stopping the service, change the config file and
re-start the service.

However, now, I want to run the program to do the same thing on 2
different database simultaneously.

Well, the service app can read a config file, and then kick off one or
more threads in separate .dll's if need be to launch two processes at
the same time.
What would be the best way of doing this without keeping 2 different
version of the program?

A service application can have more than one service. When you create
a Service application in .Net, in the designer generated code, it has
these lines (or similar):

ServicesToRun = New System.ServiceProcess.ServiceBase() {New
ServiceClass1}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)

So the ServicesToRun array only has a single entry (ServiceClass1). If
you add an additional class to your project that inherits ServiceBase,
you can add as many individual services to the app as you need:

The code below would start three services (ServiceClass1,
ServiceClass2, ServiceClass3)

ServicesToRun = New System.ServiceProcess.ServiceBase() {New
ServiceClass1, New ServiceClass2, New ServiceClass3}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)


Hope this helps a little.
 
K

Kevin Spencer

Write a business class that does the work and host it in a service. This way
you can host it in as many different services as you need to.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

What You Seek Is What You Get.
 
H

hpark2_1999

Chris said:
Is the functionality for the second database the same? Does it use the
same queries, tables etc. on the other database?

Other than Database/Server name everything else is EXACTLY the same.
(Table name, query, functionality, etc..)
That is why I was wondering. we COULD just install this service in
another server with different database connection configuration, but
since the process is rather LIGHT, we were hoping to be able to just
install 2 instances of same executable in one box.
Well, the service app can read a config file, and then kick off one or
more threads in separate .dll's if need be to launch two processes at
the same time.

We would do this IF functionality is different (hack, we would just
create another service). However, in this case, other than
Database/server name, everything else is identical in terms of
functionality.
A service application can have more than one service. When you create
a Service application in .Net, in the designer generated code, it has
these lines (or similar):

ServicesToRun = New System.ServiceProcess.ServiceBase() {New
ServiceClass1}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)

So the ServicesToRun array only has a single entry (ServiceClass1). If
you add an additional class to your project that inherits ServiceBase,
you can add as many individual services to the app as you need:

The code below would start three services (ServiceClass1,
ServiceClass2, ServiceClass3)

ServicesToRun = New System.ServiceProcess.ServiceBase() {New
ServiceClass1, New ServiceClass2, New ServiceClass3}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)


Hope this helps a little.

Above is a good thought, but still requires un-necessary code changes
(in my opinion) since all I was hoping to do was:
Service A: Runs C:\InstanceA\A.EXE using A.Config in the same directory
Service B: Runs C:\InstanceB\A.EXE using A.Config in the same directory
Where A.EXE in those 2 different directory are EXACTLY the same code.

Another acceptable way would be:
Service A: Runs C:\InstanceA\A.EXE /UseConfigA
Service B: Runs C:\InstanceA\A.EXE /UseConfigB

Is it possible?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top