2012-04-24 76 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