【问题标题】:WCF ChannelFactory IDuplexSessionChannelWCF ChannelFactory IDuplexSessionChannel
【发布时间】:2012-10-25 14:56:54
【问题描述】:

我正在使用ChannelFactory 通过发送原始soap 请求与外部服务进行通信。我有一个使用 IDuplexSessionChannel 的 NetTcp 端点。我尝试使用binding.BuildChannelFactory<T>() 创建IRequestChannel/IRequestSessionChannel,但这不起作用(抛出异常)。从我在线阅读的内容来看,双工通道可以像请求通道一样使用,您可以在其中调用服务方法并立即获得响应。我想知道下面的代码是否实现了这一点。我到处搜索以查看ChannelFactoryIDuplexSessionChannel 的示例,但找不到任何东西。最后,如果这是一个 IDuplexChannel 而不是一个 IDuplexSessionChannel,那么实现会有什么不同,因为一个是会话的,另一个不是?

IChannelFactory factory;
IChannel        channel;

Binding         binding = GetBindingFromConfig( bindingName );
EndpointAddress address = new EndpointAddress( endpointAddress );

if( binding.CanBuildChannelFactory<IDuplexSessionChannel>() )
{
    factory = binding.BuildChannelFactory<IDuplexSessionChannel>();
    factory.Open();
    channel = ( (IChannelFactory<IDuplexSessionChannel>)factory ).CreateChannel( address );
    channel.Open();

    ( (IDuplexSessionChannel)channel ).Send( requestMessage );
    ( (IDuplexSessionChannel)channel ).TryReceive( Timespan.MaxValue, out responseMessage );
}

这是我的配置文件:

<netTcpBinding>
    <binding
        name="xyz"
        closeTimeout="00:01:00"
        openTimeout="00:01:00"
        receiveTimeout="00:10:00"
        sendTimeout="00:01:00"
        transactionFlow="false"
        transferMode="Buffered"
        transactionProtocol="OleTransactions"
        hostNameComparisonMode="StrongWildcard"
        listenBacklog="10"
        maxBufferPoolSize="524288"
        maxBufferSize="10485760"
        maxConnections="10"
        maxReceivedMessageSize="10485760">

        <readerQuotas
            maxDepth="32"
            maxStringContentLength="8192"
            maxArrayLength="10485760"
            maxBytesPerRead="4096"
            maxNameTableCharCount="16384" />

        <reliableSession
            ordered="true"
            inactivityTimeout="00:10:00"
            enabled="false" />

        <security mode="Transport" />
    </binding>
</netTcpBinding>

【问题讨论】:

  • 你说它抛出了一个异常——它抛出了什么以及在哪里?
  • 在 binding.BuildChannelFactory() 处抛出。 ArgumentException:此通道管理器不支持指定的通道类型 System.ServiceModel.Channels.IRequestChannel。参数名称:TChannel.

标签: wcf channel channelfactory


【解决方案1】:

我找到了解决问题的方法,这里是:

ChannelFactory<IRequestChannel> factory;
IRequestChannel                 channel;

Binding         binding = GetBindingFromConfig( bindingName );
EndpointAddress address = new EndpointAddress( endpointAddress );

factory = new ChannelFactory<IRequestChannel>( binding, address );

// Since this endpoint uses sessions, we have to allow sessions to prevent an exception.
factory.Endpoint.Contract.SessionMode = SessionMode.Allowed;

factory.Open();
channel = factory.CreateChannel( address );
channel.Open();

responseMessage = channel.Request( requestMessage );

致谢:http://blogs.msdn.com/b/drnick/archive/2007/06/25/changing-the-channelfactory-contract.aspx

【讨论】:

    猜你喜欢
    • 2011-01-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-12
    • 1970-01-01
    相关资源
    最近更新 更多