在代码中创建 NetTcpBinding 时如何设置 ReliableSession.MaxPendingChannels?

发布于 2024-08-24 09:33:03 字数 788 浏览 12 评论 0原文

我们收到此错误:

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 网页 一个选项,但似乎过于复杂。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

裂开嘴轻声笑有多痛 2024-08-31 09:33:04

这就是我所做的:

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

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);
水溶 2024-08-31 09:33:04

我还需要在代码中设置此属性,经过研究发现有两种方法可以以编程方式完成此操作。

第一种方法是直接在代码中创建绑定元素,并根据它们创建新的自定义绑定,如 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 属性。

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

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

The first way is to create the binding elements directly in code, and create a new Custom Binding from them as described on the 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 way I found is to grab a reference to the binding elements from an existing binding as described here How to: Customize a System-Provided Binding. This is what the top answer is.

I tried to set the ReliableSession property on the NetTcpBinding, but it wasn't actually modifying the MaxPendingConnections property that I was expecting. After looking into the .NET source for NetTcpBinding and ReliableSession, I found that setting this property on the binding like this

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

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

Doesn't actually set the MaxPendingChannels property on the binding.

I ended up creating my binding from the binding elements directly since I wanted to precisely control the binding

我们的影子 2024-08-31 09:33:04

或者,您可以使用 NetTcpBinding 中的 ReliableSession 属性的构造函数,如下所示:

// 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

Or you could use the constructor of the ReliableSession property from the NetTcpBinding like this:

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

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

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

明媚如初 2024-08-31 09:33:04

如果您使用像 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);

If you use the relay bindings like NetTcpRelayBinding, you can use 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;
    }
}

Usage:

var binding = new NetTcpRelayBinding();
binding.SetReliableSessionMaxPendingChannels(128);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文