3

我们收到此错误:

System.ServiceModel.ServerTooBusyException:创建可靠会话的请求已被 RM 目标拒绝。服务器“net.tcp://localhost:50000/”太忙,无法处理此请求。稍后再试。无法打开通道。

据我了解,我需要在 ReliableSession 绑定中增加 MaxPendingChannels 的值。

但是我们在这样的代码中配置 WCF:

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

那么如何以编程方式设置 ReliableSession.MaxPendingChannels 呢?(我能找到的所有示例都使用配置文件)


在此网页上搜索 MaxPendingChannels 以获得一个选项,但它似乎过于复杂。

4

4 回答 4

6

这就是我所做的:

 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);
于 2010-03-10T16:14:45.233 回答
2

我还需要在代码中设置这个属性,在研究之后发现有两种方法可以通过编程来完成。

第一种方法是直接在代码中创建绑定元素,并从它们创建一个新的自定义绑定,如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);

我发现的第二种方法是从现有绑定中获取对绑定元素的引用,如此处所述如何:自定义系统提供的绑定。这是最重要的答案。

我试图在 NetTcpBinding 上设置 ReliableSession 属性,但它实际上并没有修改我所期望的 MaxPendingConnections 属性。在查看 NetTcpBinding 和 ReliableSession 的 .NET 源代码后,我发现像这样在绑定上设置此属性

NetTcpBinding netTcpBinding = new NetTcpBinding(SecurityMode.None, true);

netTcpBinding.ReliableSession = 
    new OptionalReliableSession(
        new ReliableSessionBindingElement()
            {
                MaxPendingChannels = 100
            });

实际上并未在绑定上设置 MaxPendingChannels 属性。

我最终直接从绑定元素创建绑定,因为我想精确控制绑定

于 2014-09-03T21:01:15.807 回答
0

或者您可以像这样使用ReliableSession属性的构造函数NetTcpBinding

// initialize ReliableSessionBindingElement object
var reliableSessionBindingElement = new ReliableSessionBindingElement
{
  MaxPendingChannels = 128
  // set other properties here
}; 

binding.ReliableSession = new ReliableSession(reliableSessionBindingElement)
{
  // set properties here
};

有关详细信息,请参阅: http: //msdn.microsoft.com/en-us/library/ms585454 (v=vs.110).aspx

于 2014-08-05T10:49:03.390 回答
0

如果您使用像 NetTcpRelayBinding 这样的中继绑定,您可以使用以下代码:

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;
    }
}

用法:

var binding = new NetTcpRelayBinding();
binding.SetReliableSessionMaxPendingChannels(128);
于 2016-08-24T11:00:14.660 回答