如何调用从另一个Windows服务运行Windows服务的方法? [英] How to call a method of running windows service from another windows service?

查看:311
本文介绍了如何调用从另一个Windows服务运行Windows服务的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为WindwosService1的Windows服务,它已在后台运行。我必须创建另一个Windows服务,它将在运行时调用WindwosService1的方法。如何实现这一目标。我是c#的新手,即使是最轻微的帮助也会非常感激!谢谢!



编辑:WindowsService1已经创建了我无法更改的服务。我只需要安装它并运行它。这个Windows服务有一个所需的方法,我想用另一个我将创建的Windows服务(WindowsService2)调用。是否有可能使用WCF公开已创建服务的方法(WindowsService1,我没有权利修改)?



我尝试过:



我尝试添加对服务的引用并通过该引用调用方法。

I have a windows service called WindwosService1 which is already running in the background. I have to create another windows service which will call the method of WindwosService1 while its running. how to achieve this. I am new to c# and even a slightest help will be much appreciated! thank you!

WindowsService1 is already created service which i cannot change. i just have to install it and run it. this windows service has a desired method which i want to call using another windows service (WindowsService2) which i will be creating. is it possible to expose method of already created service(WindowsService1 which i dont have rights to modify) using WCF?

What I have tried:

I have tried adding a reference to the service and calling the method by that reference.

推荐答案

假设:服务1和服务2是作为Windows服务运行的两个不同进程

答案:您将无法通过以下方式调用此方法引用,因为两个不同的服务将有自己的内存分配和执行。如果您对共享服务内存和进程&你有一个单独的程序集中的逻辑,你可以添加引用并开始使用它。如果您需要在其他服务的相同进程和内存中执行逻辑,实现此目的的最佳方法是公开基于休息的API或基于套接字TCP的连接。您可以使用WCF或Web API基础自托管。还要寻找单例,适配器和Creational设计模式。



我们使用WCF自托管和事件驱动实现实现了类似的逻辑。



谢谢,

Ashwin
Assumption: Service 1 and Service 2 are two different processes running as windows service
Answer: You will not be able to call this method by reference because two different services will have its own memory allocation and execution. If you are not interested in sharing service memory and process & you have logic in a separate Assembly you can add reference and start using it. If you need to execute logic inside same process and memory of other service, best way to achieve this would be to expose an rest-based API or socket TCP based connection. You can go with WCF or Web API base self hosting. Also look for singleton, adapter and Creational design pattern.

We implemented similar logic using WCF self hosting with Event-driven implementation.

Thanks,
Ashwin


这篇关于如何调用从另一个Windows服务运行Windows服务的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆