views:

240

answers:

1

I am developing a custom Windows service with C# that will manage state for several instances .NET applications. How can I pass data (such as token) to the service through the ServiceController?

Is this possible or am I not fully understanding the design intent behind Windows services?

+3  A: 

JonnyD,

Just to make sure we're on the same page: if you're creating your own service, you're going to derive from the ServiceBase class in the System.SystemProcess namespace. The ServiceController can be used to interact with a Windows service (such as the one you're creating), but the intent of the ServiceController class is that it be used primarily for service control in the same way that the Windows Service Control Manager (SCM) starts, stops, pauses, etc., a Windows service. You could probably carry out some form of "business interactions" with your service through the ServiceController's ExecuteCommand method, but that's not really its primary purpose.

Most Windows services that are designed to interact with other applications typically establish listeners and other well-known endpoints that can be used by callers for purposes of interactions. These listeners may be handled via remoting, web services, WCF, etc., and are may be created on service startup (e.g., OnStart method firing) and torn-down on service top (OnStop).

I don't know the specific requirements you're attempting to meet with your service, but you indicated that you're looking to create a state management service. These are relatively common services, and they're oftentimes created as singletons to ensure that all callers access one instance of the desired state management class/type. The following link may give you some ideas and guidance regarding how to implement such a service using remoting. If you're more familiar with WCF you could also use it in place of .NET remoting:

http://www.codeproject.com/KB/IP/Chat_Application.aspx

Instead of a ChatServer (as shown in the example), you'd have a state management server/type.

I hope this helps!

Sean McDonough
Thanks for the info Sean.
JonnyD