?
Solved

Windows Services Communication

Posted on 2004-11-29
19
Medium Priority
?
786 Views
Last Modified: 2010-05-18
I have to implement a solution using Windows Services however I have no experience in doing so. Basically I have a "master" windows service that calls an "agent" windows service on different servers to perform various tasks.  I have figured out most of it, however I need for the "agent" windows service to send a message back to the "master" when it has finished all its tasks, so that the "master" can then call the next "agent". Can anyone give me an idea on how to go about doing this? Thanks.
0
Comment
Question by:cute6481
  • 10
  • 8
19 Comments
 
LVL 11

Expert Comment

by:sachiek
ID: 12701848
You can use delegates kind of solutions.

Invoke all the agents as seperate functions from your master service.

or

Invoke each agent in one seperate thread. So it is autonomy. Once those task completed you will get notified automatically.

Still not covienced. Pls reply.


Sachi
0
 
LVL 5

Expert Comment

by:Kelmen
ID: 12702584
As you mentioned you figured most of it except the agent callback the master part.

Sounds to me its an asynch process.

How about using socket for the communication part?

http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemNetSockets.asp?frame=true

The TcpClient and TcpListener there may give some idea to kickoff.

HTH
0
 

Author Comment

by:cute6481
ID: 12702758
Thank you for your replies guys.

Is it possible to use a custom command for what i want to do?  I have been told that I can execute a cutom command and get a reply back to my calling service (which im thinking i might just change to a console app as it is not good practice to have a service controlling a service??). Any feedback is appreciated. Documentation on the custom command seems to be sparse...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:sachiek
ID: 12702892
Actually I had something similar requirement earlier.

i.e. A Master agent will call each sub agent for certain tasks to be completed. Doing those sub tasks sequentially.

I tried it with remoting. It means sub task will be invoked from master agent.

but then finally i changed the design to one single windows service.

You can see an example in windows itself. When you try to stop IIS from windows service window it will stop other three services automatically. i.e Index service, SMTP (is installed) then one other service. While starting also it does in that same way.
Surely your requirement can be done with remoting in C#.  Let me know if you want a sample which i got relatively similar to your requirement.


Sachi

 
 
0
 

Author Comment

by:cute6481
ID: 12702942
Thanks Sachi.

I have been told to use WMI to complete this project. I think I have figured out how to go about it.. I have a console application which calls custom commands on windows services to perform various tasks (using ServiceController.ExecuteCommand.. The only issue I have now is how my console app is going to know that the custom command has finished either successfully or otherwise - does anyone know of a way to do this???
0
 
LVL 11

Expert Comment

by:sachiek
ID: 12703010
Oh ok. That is intresting.

I managed to find an article from microsoft regarding this MMC which is inturn called WMI also.

http://msdn.microsoft.com/vstudio/default.aspx?pull=/library/en-us/dnhcvs04/html/vs04d6a.asp
Check this out.

Ya, It is one good thing to learn. Executecommand can be used.


I will post further soon.

Sachi
0
 
LVL 11

Expert Comment

by:sachiek
ID: 12703026
Hmm.. there is also ready avilable MMC library.

http://sourceforge.net/projects/mmclibrary/

Check this above url to get it.


Sachi
0
 
LVL 11

Expert Comment

by:sachiek
ID: 12703116
Further below are my findings.


*****
 Server has a number of Windows Services (I see at least 3) that run,
plus it has a UI program that runs, the services are listed under Services
in the "Control Panel - Administrative Tools"

The UI program (for SQL Server) is called "Service Manager" is under
Programs - Startup.

A service accept a "command" (from its UI) is to do something is to override the ServiceBase.OnCustomCommand method and have it call the same procedure your Timer.Elapsed event handler calls.

Then you can use ServiceController.ExecuteCommand to invoke this custom
command.

Note: Probably define an Enum of CustomCommands that my service
supported so its easier to keep track of them. A custom command for
OnCustomCommand is an integer between 128 & 256, which also means  you can
have multiple custom commands defined.

Remember that ServiceController can control services on your local machine
as well as services on remote machines. Note you may need to configure the
various machines to allow remote control of services.


Custom commands with a service are discussed in Matthew MacDonalds book "Microsoft Visual Basic .NET Programmer's Cookbook" from MS Press
*****

Sachi
0
 
LVL 11

Accepted Solution

by:
sachiek earned 1600 total points
ID: 12703176
0
 

Author Comment

by:cute6481
ID: 12703179
Thanks heaps for your help!! Looking into it all now....
0
 

Author Comment

by:cute6481
ID: 12711927
Great Information - thanks Sachi.

I have managed to just about complete it all, however I am having issues trying to Execute my custom command... I am using the code ServiceController.ExecuteCommand(130) however I get an error telling me that I "cannot control Service 1 on ".". All the reading i have done suggests this error occurs due to an invalid command number (so this is not the problem as i am using 130) or from security setting but i am just running it on my local machine so i don't believe this is the problem either. Any ideas??
0
 
LVL 11

Expert Comment

by:sachiek
ID: 12712146
Is there any error number?


Sachi
0
 
LVL 11

Expert Comment

by:sachiek
ID: 12712209
Can you double check your custom value. Is it surely 130?

I guess this error is thrown because of executecommand received number less than 128.


Sachi

0
 

Author Comment

by:cute6481
ID: 12712235
Nope. My application hangs for about 30 seconds and then the error is finally caught in my catch, saying "Cannot Control Service on computer '.'".
0
 

Author Comment

by:cute6481
ID: 12712255
My Custom value is definitley 130... i have even tried it with other values (200, 150) and still same result.
0
 
LVL 11

Expert Comment

by:sachiek
ID: 12712496
You should not impersonate the user. Try to use system local account.

Sachi
0
 

Author Comment

by:cute6481
ID: 12712517
I was using the local system account.  The problem was that I was running my test application in the same solution that I created the windows service in which was causing some kind of conflict.  Is all fixed now. Thanks for your help Sachi.
0
 
LVL 11

Expert Comment

by:sachiek
ID: 12712581
Can I have this sample application :) ?

Sachi
0
 

Author Comment

by:cute6481
ID: 12722323
Yeah no problem... as soon as i finish it! Having an issue with logging to an event log on a remote computer - have posted a seperate question about this. As soon as its done you can have a copy.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month16 days, 14 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question