Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing an object as a parameter to a windows service

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?

like image 645
Leandro Galluppi Avatar asked May 24 '10 22:05

Leandro Galluppi


1 Answers

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.

like image 187
mdma Avatar answered Sep 23 '22 01:09

mdma