简体   繁体   English

WCF - 回拨给客户端(双工?)

[英]WCF - calling back to client (duplex ?)

I have a problem with what solution to choose.. I have a server running having a Service running that can receive orders from a website. 我有一个问题,选择什么解决方案..我有一个服务器正在运行服务运行,可以从网站接收订单。 To this server several client (remote computers) are connected somehow. 对于此服务器,以某种方式连接了多个客户端(远程计算机)。

I would really like to use WCF for all comunication, but not sure it's possible. 我真的想使用WCF进行所有通信,但不确定它是否可行。

I dont wanna configure all client firewall settings in their routers, so the clients would have to connect to the server. 我不想在他们的路由器中配置所有客户端防火墙设置,因此客户端必须连接到服务器。

But when an order is recieved on the server, it should be transferred to a specific client. 但是,当在服务器上收到订单时,应将其转移到特定客户端。

One solution could be to have the Client connect using a duplex binding, but it will have to somehow keep the connection alive in order to be able to received data from server... Is this a good way to do this ?? 一种解决方案可能是让客户端使用双工绑定进行连接,但是必须以某种方式保持连接处于活动状态才能从服务器接收数据...这是一个很好的方法吗?

Normally the connection times out and probably for a good reason... 通常连接超时,可能是有充分理由的......

Anyone have insight to this problem. 任何人都有这个问题的见解。

Thx alot for any advise :-) 很多任何建议:-)

Best Regards Søren Müller 最好的问候SørenMüller

This is exactly what duplex bindings were designed for. 这正是双工绑定的设计目标。 The two best choices you have are NetTcpBinding or PollingDuplexBinding . 您拥有的两个最佳选择是NetTcpBindingPollingDuplexBinding

The former uses a TCP protocol which may not be suitable for your clients if they aren't on your network. 前者使用TCP协议,如果它们不在您的网络上,可能不适合您的客户。 However, it does allow two-way communication over a client-initiated socket. 但是,它确实允许通过客户端启动的套接字进行双向通信。 So the client doesn't need to be able to accept incoming connections. 因此客户端不需要能够接受传入的连接。 I recently used this on a project and it works very well. 我最近在一个项目上使用它,它运行得很好。 It's also very responsive. 它也非常敏感。 When client applications close, the session on the server immediately ends. 客户端应用程序关闭时,服务器上的会话立即结束。

The second option, PollingDuplexBinding is included in the Silverlight SDK. 第二个选项PollingDuplexBinding包含在Silverlight SDK中。 It uses a client-initiated "long" HTTP request. 它使用客户端发起的“长”HTTP请求。 The request waits for messages that need to go out to the client and when they arrive, the client request returns. 请求等待需要发送到客户端的消息,当它们到达时,客户端请求返回。 The client then initiates a new HTTP request back to the server. 然后,客户端将新的HTTP请求发送回服务器。 In other words, the client always has a pending HTTP request. 换句话说,客户端始终具有挂起的HTTP请求。 This works well over firewalls and should be used when you're dealing with internet clients. 这适用于防火墙,应该在您处理Internet客户端时使用。 However, I've found this to be not as responsive as NetTcpBinding. 但是,我发现这不像NetTcpBinding那样响应。 I may have been doing something wrong but it seemed like attempts to send callbacks to abandoned client sessions took a while to "time out". 我可能一直在做错事,但似乎尝试将回调发送到废弃的客户端会话需要一段时间才能“超时”。


Here's an example of the configuration file from my recent project that used NetTcpBinding for duplex communication. 这是我最近项目中使用NetTcpBinding进行双工通信的配置文件的示例。 Note that other than some tweaks to service throttling I am pretty much using the defaults for this binding. 请注意,除了对服务限制的一些调整之外,我几乎使用此绑定的默认值。 But there's all kinds of things you can tweak such as receiveTimeout, inactivityTimeout, etc. 但是你可以调整各种各样的东西 ,比如receiveTimeout,inactivityTimeout等。

<configuration>
    <system.serviceModel>

        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

        <behaviors>
            <serviceBehaviors>
                <behavior name="">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="true" />
                    <serviceThrottling maxConcurrentCalls="65535" 
                                       maxConcurrentSessions="65535" 
                                       maxConcurrentInstances="65535" />
                </behavior>
            </serviceBehaviors>
        </behaviors>

        <bindings>
            <netTcpBinding>
                <binding maxConnections="65535">
                    <security mode="None" />
                </binding>
            </netTcpBinding>
        </bindings>

        <services>
            <service name="BroadcastService">
                <endpoint address="" binding="netTcpBinding" contract="BroadcastService" />
            </service>
        </services>

    </system.serviceModel>
</configuration>

[ServiceContract( CallbackContract = typeof( IBroadcastCallback ) )]
[ServiceBehavior( ConcurrencyMode = ConcurrencyMode.Multiple )]
public class BroadcastService : IDisposable
{

    [OperationContract(IsInitiating=true)]
    public long Subscribe( Guid clientID )
    {
        // clients call this to initiate the session
    }

    [OperationContract(IsOneWay = true)]
    public void Publish( BroadcastMessage message )
    {
        // client calls this to broadcast a message to
        // all other subscribed clients via callback
    }

}

[ServiceContract( Name = "BroadcastCallback" )]
public interface IBroadcastCallback
{

    [OperationContract( IsOneWay = true, AsyncPattern = true )]
    IAsyncResult BeginBroadcast(BroadcastMessage Message, AsyncCallback callback, object state);

    void EndBroadcast( IAsyncResult asyncResult );

}   // interface

I have done bit of research to select best approach for duplex communication. 我已经做了一些研究来选择双工通信的最佳方法。 i came up with following solutions. 我提出了以下解决方案。

WCF service with wsDualhttpbinding : 使用wsDualhttpbinding的WCF服务:
This approach is good if the server and client both has public IPs. 如果服务器和客户端都具有公共IP,则此方法很好。 Client and server use http to communicate so no firewall restriction. 客户端和服务器使用http进行通信,因此没有防火墙限制 But when initiating the duplex communication client has to make separate connection to server so that client also need to have public IP. 但是,当启动双工通信时,客户端必须与服务器分开连接,以便客户端也需要具有公共IP。

WCF service with NetTcpbinding : 使用NetTcpbinding的WCF服务:
This approach uses the TCP protocol for duplex communication and this is way to go if you are using WCF. 此方法使用TCP协议进行双工通信,如果您使用WCF,这是可行的方法。 Only disadvantage is some firewall blocks TCP port. 唯一不利的是一些防火墙阻塞TCP端口。 In this case you need to have firewall exception. 在这种情况下,您需要防火墙例外。

WCF NetHttpBinding with web socket : 使用Web套接字的WCF NetHttpBinding:
this approach use http protocol so no firewall restriction. 这种方法使用http协议,因此没有防火墙限制。 In this approach client initiate http connection and later upgrade to TCP connection for duplex communication. 在此方法中,客户端启动http连接,然后升级到TCP连接以进行双工通信。 At the moment only win 8 or win 2012 operating system support WCF web socket. 目前只赢8或赢得2012操作系统支持WCF网络套接字。 All server and clients are wind 8 or win 2013 this is the way to go. 所有服务器和客户都是风8或赢2013年这是要走的路。

Web socket using third party framework (signal R , Xsocket.net) : 使用第三方框架的Web套接字(信号R,Xsocket.net):
This is not a WCF but still you can implement rest web service or windows service application. 这不是WCF,但您仍然可以实现其他Web服务或Windows服务应用程序。 Use http protocol and later upgrade to TCP for duplex communication 使用http协议,然后升级到TCP进行双工通信

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

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