2012-04-24 18 views
1

是否可以通过IEndpointBehavior实现更改ConnectionOrientedTransportBindingElement(例如,ConnectionBufferSize)的属性值?为扩展绑定实现IEndpointBehavior

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

回答

1

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

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