【问题标题】:WCF duplex connection (Server <-> Client communication without 2nd Client)WCF 双工连接(服务器 <-> 没有第二个客户端的客户端通信)
【发布时间】:2014-05-08 14:40:07
【问题描述】:

我正在尝试使用 WCF 实现服务器/客户端通信,并且偶然发现了跨双工通信,但正如我所见,您需要一个服务器和 2 个客户端,并且只有 2 个客户端可以通过服务器相互通信?! ?

有没有办法只获得 2 台机器/进程与 WCF 的通信? 让客户端直接和服务器通信?

到目前为止我做了什么:

创建包含接口、类、OperationContract/ServiceContract/ServiceBehaviour 的项目

创建作为“服务器”的项目,它基本上就是这段代码:

static void Main(string[] args)
{
    using (var serviceHost = new ServiceHost(typeof(ServiceInbound)))
    {
        // Open the host and start listening for incoming messages.
        serviceHost.Open();
        // Keep the service running until the Enter key is pressed.
        Console.WriteLine("The service is ready.");
        Console.WriteLine("Press the Enter key to terminate service.");
        Console.ReadLine();
    }
}

<system.serviceModel>
  <services>
    <service name="WCFLibary.ServiceInbound" behaviorConfiguration="WCFLibaryMEXBehavior">
      <endpoint address="service" binding="wsDualHttpBinding" contract="WCFLibary.IServiceInbound"/>
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      <host>
        <baseAddresses>
          <add baseAddress="http://localhost:8080/WCFServer_Service"/>
        </baseAddresses>
      </host>
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="WCFLibaryMEXBehavior">
        <serviceMetadata httpGetEnabled="true"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>

创建一个客户端项目并在其中向服务器添加一个 ServiceReference localhost:8080/WCFServer_Service

我实际上不需要第二个客户端,我只想在服务器/客户端之间推送数据。

【问题讨论】:

  • 双工连接是为了让服务器回调打开连接的客户端机器。您与第二个客户端的事情偏离了轨道,WCF 双工不允许客户端相互通信,至少不能直接通信。

标签: c# wcf


【解决方案1】:

您正在寻找的是客户端回调。客户端通过服务调用提供回调函数。

this page on MSDN

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-06
    • 2019-09-11
    • 1970-01-01
    相关资源
    最近更新 更多