.net - WCF - 回调客户端(双工?)

标签 .net wcf .net-3.5 wcf-binding

我对选择什么解决方案有疑问..
我有一个正在运行的服务器,它正在运行一个可以从网站接收订单的服务。
几个客户端(远程计算机)以某种方式连接到该服务器。

我真的很想将 WCF 用于所有通信,但不确定是否可行。

我不想在他们的路由器中配置所有客户端防火墙设置,所以客户端必须连接到服务器。

但是当在服务器上收到订单时,它应该被转移到特定的客户端。

一种解决方案可能是让客户端使用双工绑定(bind)进行连接,但它必须以某种方式保持连接处于事件状态才能从服务器接收数据......这是一个好方法吗?

通常连接超时,可能有一个很好的理由......

任何人都对这个问题有洞察力。

非常感谢任何建议:-)

最好的祝福
索伦·穆勒

最佳答案

这正是双工绑定(bind)的设计目的。您拥有的两个最佳选择是NetTcpBindingPollingDuplexBinding .

前者使用的 TCP 协议(protocol)可能不适合您的客户端,如果它们不在您的网络上。但是,它确实允许通过客户端启动的套接字进行双向通信。所以客户端不需要能够接受传入的连接。我最近在一个项目中使用了它,效果很好。它也非常敏感。当客户端应用程序关闭时,服务器上的 session 立即结束。

第二个选项 PollingDuplexBinding 包含在 Silverlight SDK 中。它使用客户端发起的“长”HTTP 请求。请求等待需要发送给客户端的消息,当它们到达时,客户端请求返回。然后客户端向服务器发起一个新的 HTTP 请求。换句话说,客户端总是有一个未决的 HTTP 请求。这在防火墙上运行良好,应该在您与 Internet 客户端打交道时使用。但是,我发现它的响应不如 NetTcpBinding。我可能做错了什么,但似乎尝试向废弃的客户端 session 发送回调需要一段时间才能“超时”。

这是我最近项目中使用 NetTcpBinding 进行双工通信的配置文件示例。请注意,除了对服务限制进行一些调整外,我几乎使用此绑定(bind)的默认值。但是there's all kinds of things您可以调整诸如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

关于.net - WCF - 回调客户端(双工?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2735079/

相关文章:

c# - protobuf 和 List<object> - 如何序列化/反序列化?

.net - 最大化线程数以充分利用所有可用资源而不影响整体性能

c# - 以 HTML 格式发送电子邮件无效。为什么?

c# - x86 程序集的 null PublicKeyToken

windows-services - 为什么我的托管 WCF 服务的 Windows 服务不能在 LocalService 或 NetworkService 帐户下运行

wcf - 为 Windows Phone 创建 WCF

c# - .NET 中的混合语言项目

.net - Lucene.Net最佳实践

javascript - 使用 .NET 中的 JavaScript 确定 pageLoad() 中是否发生了部分回发以及发生了哪些部分回发

c# - wcf服务运行结果的良好实践