简体   繁体   English

使用WCF的两个winform应用程序之间的通信?

[英]Communication between two winform application using WCF?

I have two different winform application, App1 and app2. 我有两个不同的winform应用程序,App1和app2。 App1 calls the exe of app2 (using DOS command window) and sends a message to start the app2. App1调用app2的exe(使用DOS命令窗口)并发送消息以启动app2。 The app2 starts executing and once it finishes its task it sends back the message to app1 that the execution was successful. app2开始执行,一旦完成任务,它就会向app1发送回执行成功的消息。 How can I acheieve this functionality using WCF. 如何使用WCF实现此功能。 earlier the same code was written in foxpro and this finc was achieved using memory management. 早先在foxpro中编写了相同的代码,这个finc是使用内存管理实现的。

I think what you want is peer-to-peer communication, where 2 applications (which may or may not be running on the same machine) send each other messages asynchronously. 我认为你想要的是点对点通信,其中2个应用程序(可能在同一台机器上运行也可能不运行)以异步方式发送彼此的消息。 This is the way chat programs such as MSN Messenger work. 这就是MSN Messenger等聊天程序的工作方式。

There's a "simple" tutorial about peer-to-peer communication using WCF at MSDN . 有一个关于在MSDN上使用WCF进行点对点通信的“简单”教程。

Mind you, this is not as easy as it sounds. 请注意,这并不像听起来那么容易。 You may prefer to just send messages using Windows' SendMessage . 您可能更喜欢使用Windows的SendMessage发送消息

This is just conceptual how to achieve this: 这只是概念性的实现方式:

You need to implement WCF service. 您需要实现WCF服务。 There are many ways how to do this task. 有很多方法可以完成这项任务。 One of this should be like this. 其中一个应该是这样的。

App1 calls service method and tell that app2 need to executed. App1调用服务方法并告诉app2需要执行。 App1 could than wait for the response. App1可以等待响应。

App2 from time to time pings service to see if it needs to be executed. App2不时ping服务以查看是否需要执行。 App2 finished its job and call service method to tell that it was done. App2完成了它的工作并调用服务方法来告诉它已经完成。

App1 will get the response when it was finished. App1将在完成后获得响应。

Other option is not to implement request/response but to ping service from App1 to see if App2 did its job. 其他选项不是实现请求/响应,而是从App1 ping服务以查看App2是否完成了它的工作。

For how to implement WCF service see, for example: http://wcftutorial.net/WCF-Getting-Started.aspx 有关如何实现WCF服务的信息,请参阅: http//wcftutorial.net/WCF-Getting-Started.aspx

Basically: 基本上:

on one side instanciate a "server" 一方面实现“服务器”

UIIServiceHost = new ServiceHost(typeof(UIInterop));
UIIServiceHost.Open();

where UIInterop is a class implementaing IUIInterop that is a Service Contract 其中UIInterop是一个实现IUIInterop的类,它是一个服务契约

using System.ServiceModel;

[ServiceContract]
public interface IUIInterop {
    [OperationContract]
    void SetControlValue (UIControl c);
}

[DataContract]
public class UIControl {        
    [DataMember]
    public String Name { get; set; }

    [DataMember]
    public String Value { get; set; }
}

Generate a proxy class => UIInteropClient 生成代理类=> UIInteropClient

on the other side implement a client using the proxy class 另一方面使用代理类实现客户端

using ( UIInteropClient proxy = new UIInteropClient("nameDependingOfAppConfig") ) {
    proxy.SetControlValue(new UIControl {});
}

===== EDIT ===== =====编辑=====

the name of classes and interfaces only reflect my lack of imagination 类和接口的名称只反映了我缺乏想象力

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM