views:

49

answers:

4

Is there some way to pass an object to a windows service? I know the method myServiceController.Star(string[] arg) but i need to pass a more complex object than a string array. Actually, i don't need the object to be passed as a parameter, what i really needs is that the service can use an object created in a windows forms application. I've tried using System.Web.Script.Serialization.JavaScriptSerializer.Serialize method to convert the object into a Json but i couldn't because the object contains a circular reference. I also tried using pointers, but i couldn't becouse it is a managed type object.

Any idea what can i do?

+1  A: 

Rather than trying to pass the object itslef (which will not work since the service executes in a separate process) - pass an exernal reference to the data. For example, the path to the file containings the serialized object.

I'm assuming your service is also implemented in .NET? If so, then use binary serialization (BinaryFormatter) as this will handle the circular references. You can then deserialize in your service by loading from the named file.

You might want to consider what happens if the user restarts your service - where does it get the data from in that case? There will be no startup parameters. It may be more robust to use the registry to store the filename of the serialized object, and have your service read from this on startup. Then it will always find the data even when invoked without startup parameters, as is the case the machine reboots, or the user restarts the service.

mdma
A: 

I use a database to share objects between a windows service and a windows app, using XML as the encoding format and doing the serializing myself. With a database you can wrap the communication in transactions (for locking), add extra fields for logging, treat the table as a queue, and have a convenient place to recover from if you have to restart from a crash.

ebpower
A: 

It totally depends on what channels the windows service are communicating via. It can be listening as a web service, running a proprietary socket/port listener, or (in .NET 4.0) watching a memory mapped file or a named pipe.

Your question mentions a (string[] args) method, which looks more like a command line "main" method. In which case, you're probably looking at the wrong thing if this is a windows service. You don't execute a windows service EXE to pass it arguments....not if you want it to run as a service.

Each of these methods involve their own quirks. But basically you've got to marshal your data via either a reference or value serialization.

Eric Falsken
+1  A: 

The easiest way is using a self hosted WCF service. If the Service and client are on the same machine you can use an in memory named pipe binding.

http://msdn.microsoft.com/en-us/library/system.servicemodel.netnamedpipebinding.aspx

http://omegacoder.com/?p=101

or you could use a tcp binding if the client resides on a different machine http://msdn.microsoft.com/en-us/library/ms733769.aspx

Doobi
Named pipes was exactly what I was going to suggest.
AaronLS