我使用ChannelFactory通过发送原始soap请求与外部服务通信。我有一个使用IDuplexSessionChannel的NetTcp端点。我尝试使用binding.BuildChannelFactory<T>()创建一个IRequestChannel/IRequestSessionChannel,但不起作用(抛出异常)。根据我在网上读到的内容,双工通道可以像请求通道一样使用,在请求通道中,调用服务方法并立即获得响应。我想知道下面的代码是否能做到这一点。我到处寻找ChannelFactory和IDuplexSessionChannel的例子,但是什么也找不到。最后,如果这是一个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>发布于 2012-11-07 00:14:36
我找到了一种解决问题的方法,如下所示:
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
https://stackoverflow.com/questions/13237766
复制相似问题