Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

c# - How to call method from running windows service

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...