I am using a ChannelFactory
to communicate with external services by sending raw soap requests. I have a NetTcp endpoint that uses the IDuplexSessionChannel
. I tried creating a IRequestChannel/IRequestSessionChannel
using binding.BuildChannelFactory<T>()
, but that did not work (threw exception). From what I read online, duplex channels can be used like request channels, where you call a service method and get an immediate response. I want to know if the below code accomplishes that. I searched everywhere to see an example of ChannelFactory
and IDuplexSessionChannel
, but could not find anything. Lastly, if this were an IDuplexChannel instead of an IDuplexSessionChannel, would the implementation be any different since one is sessionful and the other isn't?
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 );
}
Here is my configuration file:
<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>
I found a way to fix the issue, here it is:
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 );
Credits: http://blogs.msdn.com/b/drnick/archive/2007/06/25/changing-the-channelfactory-contract.aspx