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

We get this error:

System.ServiceModel.ServerTooBusyException: Request to create a trusted session was denied by RM Destination. the server 'net.tcp: // localhost: 50000 /' is also busy processing this request. Try again later. The channel could not be opened.

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

However, we configure WCF in code as follows:

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

So how can I programmatically configure ReliableSession.MaxPendingChannels? (All the examples I can find use configuration files)


Find MaxPendingChannels on this web page for one parameter, but it seems complicated.

+3
source share
4 answers

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); 
+5
source

I also needed to set this property in the code, and after researching, he found that there are two ways to do this programmatically.

The first way is to create anchor elements directly in the code and create a new custom anchor from them, as described in CustomBinding MSDN

 // Create a custom binding that contains two binding elements. ReliableSessionBindingElement reliableSession = new ReliableSessionBindingElement(); reliableSession.MaxPendingChannels = 100; TcpTransportBindingElement tcpTransport = new TcpTransportBindingElement(); CustomBinding binding = new CustomBinding(reliableSession, tcpTransport); 

the second method I found is to grab the link to the binding elements from an existing binding, as described here How to configure binding to the system . This is the best answer.

I tried to set the ReliableSession property to NetTcpBinding, but actually it did not change the MaxPendingConnections property that I was expecting. After examining the .NET source for NetTcpBinding and ReliableSession, I found that setting this property in a binding like this

 NetTcpBinding netTcpBinding = new NetTcpBinding(SecurityMode.None, true); netTcpBinding.ReliableSession = new OptionalReliableSession( new ReliableSessionBindingElement() { MaxPendingChannels = 100 }); 

Actually, the MaxPendingChannels property for binding is not set.

In the end, I created the binding to the binding elements, since I wanted to precisely control the binding

+1
source

Or you can use the constructor of the ReliableSession property from NetTcpBinding as follows:

 // initialize ReliableSessionBindingElement object var reliableSessionBindingElement = new ReliableSessionBindingElement { MaxPendingChannels = 128 // set other properties here }; binding.ReliableSession = new ReliableSession(reliableSessionBindingElement) { // set properties here }; 

For more information see: http://msdn.microsoft.com/en-us/library/ms585454(v=vs.110).aspx

0
source

If you use relay bindings such as NetTcpRelayBinding, you can use the following code:

 public static class NetTcpRelayBindingExtension { public static void SetReliableSessionMaxPendingChannels(this NetTcpRelayBinding baseBinding, Int32 MaxPendingChannels) { var bindingType = baseBinding.GetType(); var bindingProperty = bindingType.GetField("session", BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic); var reliableSession = (ReliableSessionBindingElement)bindingProperty.GetValue(baseBinding); reliableSession.MaxPendingChannels = MaxPendingChannels; } } 

Using:

 var binding = new NetTcpRelayBinding(); binding.SetReliableSessionMaxPendingChannels(128); 
0
source

Source: https://habr.com/ru/post/887341/


All Articles