468,315 Members | 1,476 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,315 developers. It's quick & easy.

1 executable, multiple services?

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.

Jul 11 '06 #1
3 5408
hp*********@yahoo.com wrote:
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(ServicesToRu n)

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(ServicesToRu n)
Hope this helps a little.

Jul 11 '06 #2
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.
<hp*********@yahoo.comwrote in message
news:11*********************@75g2000cwc.googlegrou ps.com...
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.

Jul 11 '06 #3

Chris Dunaway wrote:
hp*********@yahoo.com wrote:
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?
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.
>
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.
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.
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(ServicesToRu n)

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(ServicesToRu n)
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?

Jul 11 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Jay Haslup | last post: by
1 post views Thread by Harald Schneider | last post: by
3 posts views Thread by Dakkar | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.