我们收到此错误:
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? (我可以找到的所有示例都使用配置文件)
在web page上搜索一个选项的MaxPendingChannels,但它似乎过于复杂。
答案 0 :(得分: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);
答案 1 :(得分: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);
我找到的第二种方法是从现有绑定中获取对绑定元素的引用,如How to: Customize a System-Provided Binding所述。这就是最佳答案。
我尝试在NetTcpBinding上设置ReliableSession属性,但实际上并没有修改我期望的MaxPendingConnections属性。在查看NetTcpBinding和ReliableSession的.NET源代码后,我发现在绑定上设置此属性就像这样
NetTcpBinding netTcpBinding = new NetTcpBinding(SecurityMode.None, true);
netTcpBinding.ReliableSession =
new OptionalReliableSession(
new ReliableSessionBindingElement()
{
MaxPendingChannels = 100
});
实际上并未在绑定上设置MaxPendingChannels属性。
我最终直接从绑定元素创建绑定,因为我想精确控制绑定
答案 2 :(得分: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
答案 3 :(得分: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);