views:

451

answers:

5

Hi,

I have created and started windows service Service1 (with exe as MyService.exe) using c# 2005. . I have included a method GetMyRandomNumber() that returns a random double value.

The problem here is how could use this running service and how could i call the method.

I have tried adding reference of MyService.exe and access the method as -

Service1 s = new Service1();
MessageBox.Show(s.GetMyRandomNumber().ToString());

But found that the method is not called from the running instance of the service i.e. even though i stop the service the statements are executed.

Could someone explain me how can I call the method from running instance of the service.

Thanks for sharing your valuable time.

+1  A: 

You could use Windows Communication Foundation and IPC (inter-process communication) to communicate with your service and execute your method.

Andy West
+1  A: 

You should have a look at Remoting

astander
Remoting is not a good choice if on .NET 3.0+. I prefer Alan's answer.
Mark Seemann
Thanks all you guys! But I am using .net 2.0 thats why not able to use WCF. Remoting is the best solution for me.
IrfanRaza
+4  A: 

In your code, you aren't actually calling the service, instead you are referencing the executable and invoking a method from that assembly (at run time the .NET Framework will use a local assembly to execute the code, not your running service).

To do what you want, you have a number of options.

In .NET 2.0, you would make use of .NET Remoting. You create a remoting interface, which other assemblies can use to invoke methods across executables.

In .NET 3.0, remoting was replaced by WCF. Your service would become a WCF service, which would expose the GetRandomNumber() as part of its data contract. Applications can consume the contract and connect to your service to call the method.

There are a number of good tutorials on the web for both .NET Remoting or its replacement, Windows Communication Foundation.

Alan
+1 for explaining the *difference* between Remoting and WCF
Oliver
+1 WCF would be the correct choice in this scenario if on .NET 3.0+. For completeness sake I'd like to add that although WCF replaces Remoting in almost all cases, it still has a niche for cross-AppDomain communication - I wouldn't use WCF for communicating across AppDomain boundaries, but in all other cases, WCF is the correct choice.
Mark Seemann
Thanks Alan for explaining what should i choose and why.
IrfanRaza
+1  A: 

Communicating with a running service is no different from invoking methods on any other running process. That means that you will need to dig out your standard tools for process-to-process communication.

Windows Communication Foundation (WCF) would be my default choice. You can host a WCF service in your Windows Service and expose it through a Named Pipe endpoint for efficient communication.

Mark Seemann
+1  A: 

WCF will be an overkill for communication on the same computer. Pipes is a simpler and more effective solution.

Giorgi
WCF can use named pipes. http://msdn.microsoft.com/en-us/library/ms733769.aspx
Andy West
He is using C# 2005 (.Net 2.0 ?) so WCF is not an option.
Giorgi
Good observation. I hadn't noticed that.
Andy West