WCF,NetTcpBinding,流传输模式可以双工吗?

时间:2012-08-25 08:16:27

标签: c# .net wcf

我使用NetTcpBinding和Streamed TransferMode。现在我试图以双工方式实现回调,但是我收到了错误消息。可以将NetTcpBinding与Streamed TransferMode一起使用并使用(双工)回调服务合同吗? 的背景: - 我使用NetTcpBinding,因为它很快,并且没有nat问题 - 我使用流模式,因为我也传输大文件。

配置:

 <netTcpBinding>
    <binding name="DuplexBinding" transferMode="Streamed"
                closeTimeout="00:10:00"  openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"  transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="104857600" maxReceivedMessageSize="104857600"
             >
      <readerQuotas maxDepth="104857600" maxStringContentLength="104857600" maxArrayLength="104857600" maxBytesPerRead="104857600" maxNameTableCharCount="104857600"/>
      <reliableSession enabled="true" ordered="true" inactivityTimeout="00:10:00"/>
      <security mode="None" />
    </binding>
  </netTcpBinding>

合同:

IMyDataService.cs

   [ServiceContract(CallbackContract = typeof(INotifyCallback))]
    public interface IMyDataService
    {
        [OperationContract(ProtectionLevel = System.Net.Security.ProtectionLevel.None)]
        [FaultContract(typeof(MyFaultException))]
        [FaultContract(typeof(MyUserAlreadyLoggedInFaultException))]
        [FaultContract(typeof(AuthorizationFaultException))]
        Guid Authenticate(Guid clientID, string userName, string password, bool forceLogin);
    }


INotifyCallback.cs

    public interface INotifyCallback
    {
        [OperationContract(IsOneWay = true)]
        void ShowMessageBox(string message);
    }

设置transferMode =&#34; Streamed&#34;

时出错
  

合同要求双工,但绑定&#39; NetTcpBinding&#39;不支持   它或者没有正确配置以支持它。

每个人都可以建议谢谢

1 个答案:

答案 0 :(得分:1)

在您的客户端代码中,请确保您使用DuplexChannelFactory创建到服务器的频道:

INotifyCallback callbackObject = new NotifyCallbackImpl(); //your concrete callback class
var channelFactory = new DuplexChannelFactory<IMyDataServce>(callbackObject); //pick your favourite constructor!
IMyDataService channel = channelFactory.CreateChannel();
try {
    var guid = channel.Authenticate(....);
    //... use guid...
} finally {
    try {
        channel.Close();
    } catch (Exception) {
        channel.Abort();
    }
}

[编辑]自动生成的服务引用的代理应该扩展DuplexClientBase