这正是双工绑定设计用于任何意见。你有两个最好的选择是NetTcpBinding或PollingDuplexBinding。
前者使用TCP协议,如果它们不在您的网络上,可能不适合您的客户端。但是,它确实允许通过客户端启动的套接字进行双向通信。所以客户端不需要能够接受传入的连接。我最近在一个项目中使用了它,它工作得很好。它也非常敏感。当客户端应用程序关闭时,服务器上的会话立即结束。
第二个选项PollingDuplexBinding包含在Silverlight SDK中。它使用客户端发起的“长”HTTP请求。请求等待消息发送到客户端,当客户端到达时,客户端请求返回。客户端然后发起一个新的HTTP请求回到服务器。换句话说,客户端总是有一个挂起的HTTP请求。这适用于防火墙,应在与互联网客户端打交道时使用。但是,我发现这不像NetTcpBinding那样敏感。我可能一直在做一些错误的事情,但似乎尝试将回调传递给已放弃的客户端会话需要一段时间才能“超时”。
下面是我最近使用NetTcpBinding进行双工通信的项目中的配置文件示例。请注意,除了一些调整服务调节,我几乎使用默认的这个绑定。但是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
请小心保持场中的会话,以及重启等。 – 2010-04-29 06:34:18