Search code examples
wcfapp-configwcf-binding

How to use c# code to add bindingConfiguration attribute (WCF)


I have an endpoint in the app.config:

<endpoint address=""

              binding="netTcpBinding"

              bindingConfiguration="financeBinding"

              name="RoutingServiceEndpoint"
              contract="System.ServiceModel.Routing.IRequestReplyRouter" />

The bindingConfiguration in app.config

    <bindings>
<netTcpBinding>
    <binding name="financeBinding" closeTimeout="00:10:10" openTimeout="00:10:10" sendTimeout="00:10:10" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647">
      <!--transactionFlow="true"-->
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
      <security mode="None" />
    </binding>
  </netTcpBinding>
</bindings>

Question is: how could i config this in the code? I need read the config for the database and bind it. :)


Solution

  • With this you can configure your settings in the code:

    using System.ServiceModel;           
    
    EndpointAddress endpoint = new EndpointAddress("http://yourAdress.svc");
    NetTcpBinding binding = new NetTcpBinding();
    binding.Name = "financeBinding";
    binding.MaxBufferPoolSize = 2147483647;
    
    binding.ReaderQuotas.MaxDepth = 2147483647;
    binding.ReaderQuotas.MaxStringContentLength = 2147483647;
    binding.ReaderQuotas.MaxBytesPerRead = 2147483647;
    binding.ReaderQuotas.MaxNameTableCharCount = 2147483647;
    
    binding.OpenTimeout = new TimeSpan(0, 10, 10);
    binding.CloseTimeout = new TimeSpan(0, 10, 10);
    binding.SendTimeout = new TimeSpan(0, 10, 10);
    
    binding.Security.Mode = SecurityMode.None;
    yourClient client = new yourClient(binding, endpoint);