How do I set ReliableSession.MaxPendingChannels when I create a NetTcpBinding in code?

Ian Ringrose picture Ian Ringrose · Mar 10, 2010 · Viewed 7.6k times · Source

We are getting this error:

System.ServiceModel.ServerTooBusyException: The request to create a reliable session has been refused by the RM Destination. Server 'net.tcp://localhost:50000/' is too busy to process this request. Try again later. The channel could not be opened.

As I understand it, I need to increase the value of MaxPendingChannels in the ReliableSession binding.

However we configure WCF in code like this:

serviceHost = new ServiceHost(typeof(MyServiceClass));
ServiceEndpoint endPoint = serviceHost.AddServiceEndpoint(
    typeof(IMyService),
    new NetTcpBinding(SecurityMode.None, true),
    endPointAddress);

So how do I set ReliableSession.MaxPendingChannels programmatically? (All the examples I can find use config files)


Search for MaxPendingChannels on this web page for one option, but it seems over complex.

Answer

Ian Ringrose picture Ian Ringrose · Mar 10, 2010

This is what I did:

 private Binding CreateBindingWith_MaxPendingChannels_Set(Binding baseBinding)
 {
     BindingElementCollection elements = baseBinding.CreateBindingElements();
     ReliableSessionBindingElement reliableSessionElement = 
                elements.Find<ReliableSessionBindingElement>();
     if (reliableSessionElement != null)
     {
         reliableSessionElement.MaxPendingChannels = 128;

         CustomBinding newBinding = new CustomBinding(elements);

         newBinding.CloseTimeout = baseBinding.CloseTimeout;
         newBinding.OpenTimeout = baseBinding.OpenTimeout;
         newBinding.ReceiveTimeout = baseBinding.ReceiveTimeout;
         newBinding.SendTimeout = baseBinding.SendTimeout;
         newBinding.Name = baseBinding.Name;
         newBinding.Namespace = baseBinding.Namespace;
         return newBinding;
     }
     else
     {
         throw new Exception("the base binding does not " +
                             "have ReliableSessionBindingElement");
     }
 }

.....

 Binding customBinding = CreateBindingWith_MaxPendingChannels_Set(
      new NetTcpBinding(SecurityMode.None, true));

 serviceHost = new ServiceHost(typeof(MyService));
 ServiceEndpoint endPoint = serviceHost.AddServiceEndpoint(
                    typeof(IMyService),
                    customBinding,
                    endPointAddress);