Jeffrey Kevin Pry
Jeffrey Kevin Pry

Reputation: 3296

Creating and Consuming a C# Windows Service

I have a Windows 2008 R2 Server running IIS 7.5. Currently, I use WCF to expose an interface to the outside where the code then calls routines from my DLLs.

I would like to move away from this direct access and create some sort of daemon in C# that I can run in the background. I will use the daemon to monitor threads, accept requests, and balance performance. I plan on allowing the daemon full access to the DLLs of my main application and then will have WCF services pass on commands to the daemon as they are received.

I have looked on the Internet and found a few examples about creating a Windows Service, building installers, and registering the service; however, I cannot seem to find any documentation on how to interact with running services via a different application.

Here's more or less an example of what I am looking to do:

Let's say I've built and installed the sample service depicted here: http://blogs.msdn.com/b/bclteam/archive/2005/03/15/396428.aspx

Now, a customer tells me I need to extend it to allow jobs to run on demand. I have built the necessary functions to allow me to do so, but now I am faced with the issue of determining how to talk to the currently running service to tell it to start processing. How do I do this? Do you have any example links that describe this IPC ?

Thanks!

Upvotes: 1

Views: 9929

Answers (7)

Yahia
Yahia

Reputation: 70369

IPC can be done with different methods:

Upvotes: 0

Mike
Mike

Reputation: 773

So you need a windows service that performs a number of scheduled actions and also can listen for requests via WCF to perform on demand actions, did I get that right?

Here's an MSDN document explaining how to create a windows service that implements a WCF service: http://msdn.microsoft.com/en-us/library/ms733069.aspx

Upvotes: 0

Scott Chamberlain
Scott Chamberlain

Reputation: 127543

There is nothing magical about a service, it is just a program that starts on boot that is run by the system instead of the user. You need to program in to your service some form of IPC (be it something as simple as configuration files or something more advanced like loading all dlls from a directory and calling a known function as a entry point (This would be the plugin model).

You will need to write your original service to listen in some form to provide the funcationality you want.

Upvotes: 0

Brian
Brian

Reputation: 2778

You have to use a ServiceController. Once you have a ServiceController instance you can invoke ExecuteCommand(int). ExecuteCommand will invoke OnCustomCommand in your service instance as long as your command isn't the Start/Stop/Pause. Just override OnCustomCommand and you should be set...

Reference for ServiceController:

http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.aspx

Upvotes: 0

Peter
Peter

Reputation: 3956

You can send custom commands to a Windows Service, but these are only integer values:

protected override void OnCustomCommand(int command)

A better solution is to communicate with the Windows Service over "named pipes". Best way of implementing this is IMHO to create a WCF service with NetNamedPipe binding and host it in the Windows Service itself.

Upvotes: 1

Davide Piras
Davide Piras

Reputation: 44605

You can use WCF to communicate with your Windows service.

In fact let's say you have a class library with some APIs you would like to make available like a daemom on the network, no user login required on that machine...

you can have a WCF end-point which exposes the APIs to the callers and either host this service in IIS or in a Windows service, the only difference is the way you host the WCF (IIS does it for you with the usage of .svc file, Windows Service requires a bit of code to start the WCF hosting object); after this, nearly everything stays the same.

Upvotes: 1

Tomas Jansson
Tomas Jansson

Reputation: 23462

Have you looked at this: http://msdn.microsoft.com/en-us/library/ms733069.aspx

Also, if you would like an easy way to debug your service in visual studio I've written a blog post about it: http://blog.tomasjansson.com/2010/11/debugging-your-windows-service-in-visual-studio/

Upvotes: 0

Related Questions