1

是否可以通过 IEndpointBehavior 实现更改 ConnectionOrientedTransportBindingElement 的属性值(例如 ConnectionBufferSize

var host = new ServiceHost(typoef(ISomeService), new Uri(service));
var endpoint = host.AddServiceEndpoint(typeof (ISomeService), new NetTcpBinding(), string.Empty);
endpoint.Behaviors.Add(new MyCustomEndpointBehavior());
// ...

class MyCustomEndpointBehavior : IEndpointBehavior {
    // ....
    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) {
         // what to do here?
    }
}
4

1 回答 1

2

您不能使用行为来修改绑定内部。您需要通过配置或代码构建自定义绑定

 <customBinding>
  <binding name="MyCustomBinding">
   <binaryMessageEncoding />
   <tcpTransport connectionBufferSize="256192" maxOutputDelay="00:00:30" transferMode="Streamed">
   </tcpTransport>
  </binding>
 </customBinding>

或代码

var host = new ServiceHost(typeof(Service1), new Uri("net.tcp://someservice"));

            //binding stack - order matters!
            var myCustomNetTcpBindingStack = new List<BindingElement>();

            //session - if reliable
            var session = new ReliableSessionBindingElement();
            myCustomNetTcpBindingStack.Add(session);

            //transaction flow
            myCustomNetTcpBindingStack.Add(new TransactionFlowBindingElement(TransactionProtocol.OleTransactions));


            //encoding
            myCustomNetTcpBindingStack.Add(new BinaryMessageEncodingBindingElement());

            //security
            //var security = SecurityBindingElement.CreateUserNameOverTransportBindingElement();
            //myCustomNetTcpBindingStack.Add(security);

            //transport
            var transport = new TcpTransportBindingElement();
            transport.ConnectionBufferSize = 64 * 1024;
            myCustomNetTcpBindingStack.Add(transport);


            var myCustomNetTcpBinding = new CustomBinding(myCustomNetTcpBindingStack);

            host.AddServiceEndpoint(typeof(IService1), myCustomNetTcpBinding, string.Empty);

            host.Open();

关于 ConnectionBufferSize 的好帖子在这里

于 2012-04-26T03:46:46.867 回答