1

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!

Jeffrey Kevin Pry
  • 3,266
  • 3
  • 35
  • 67
  • Are you saying that you want to run your WCF service as a self hosted Windows Service like this? http://stackoverflow.com/questions/3206132/windows-service-self-hosted-wcf-compression – Kane Jul 28 '11 at 13:41
  • Nope, I'm saying WCF just provides an interface to the service. WCF will be one of the many ways to access the service. – Jeffrey Kevin Pry Jul 28 '11 at 13:43
  • see my answer below. In previous company we were using .NET remoting and MessageQueue to communicate with windows services, from calling applications we queues messages and the windows service was listening on that queue and processing incoming messages. To do the same in a new application we are designing now, we will use TIBCO EMS queues. – Davide Piras Jul 28 '11 at 13:46

7 Answers7

1

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.

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
1

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.

Peter
  • 3,916
  • 1
  • 22
  • 43
0

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/

Tomas Jansson
  • 22,767
  • 13
  • 83
  • 137
0

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

Brian
  • 2,772
  • 15
  • 12
0

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.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
0

IPC can be done with different methods:

Yahia
  • 69,653
  • 9
  • 115
  • 144
0

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

Mike
  • 753
  • 1
  • 7
  • 16