How to call method from running windows service

前端 未结 5 1382
面向向阳花
面向向阳花 2020-12-06 12:17

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 va

相关标签:
5条回答
  • 2020-12-06 13:00

    You should have a look at Remoting

    • Remoting in C# (Link no Longer available)
    • How To Host .NET Remoting Objects in Windows Service Application
    • How to Use .Net Remoting Using (Link no Longer available)
    • A Simple Introduction to .NET Remoting
    0 讨论(0)
  • 2020-12-06 13:01

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

    0 讨论(0)
  • 2020-12-06 13:03

    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.

    0 讨论(0)
  • 2020-12-06 13:07

    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.

    0 讨论(0)
  • 2020-12-06 13:11

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

    0 讨论(0)
提交回复
热议问题