Configure wcf service programmatically

前端 未结 1 1140
执笔经年
执笔经年 2020-12-07 03:59

I have a remote wcf service, I connect it by WSHttpBinding. If I use the empty service constructor which mean it will take all the configurations from the app.config , every

1条回答
  •  無奈伤痛
    2020-12-07 04:31

    I have done this, you might have to modify your code for security mode you have in config

    public virtual ChannelFactory Proxy(string address) {
          //Validate Address
          if (string.IsNullOrEmpty(address)) throw new ArgumentNullException("Address can not be null or empty.");
          //Address
          EndpointAddress endpointAddress = new EndpointAddress(address);
    
          //Binding
          WSHttpBinding wsHttpBinding = new WSHttpBinding(SecurityMode.None, false);
          wsHttpBinding.OpenTimeout = wsHttpBinding.CloseTimeout = new TimeSpan(0, 1, 0);
          wsHttpBinding.ReceiveTimeout = wsHttpBinding.SendTimeout = new TimeSpan(0, 10, 0);
          wsHttpBinding.MaxReceivedMessageSize = wsHttpBinding.MaxBufferPoolSize = 2147483647;
          wsHttpBinding.BypassProxyOnLocal = wsHttpBinding.AllowCookies = wsHttpBinding.TransactionFlow = false;
          wsHttpBinding.MessageEncoding = WSMessageEncoding.Text;
          wsHttpBinding.TextEncoding = Encoding.UTF8;
          wsHttpBinding.UseDefaultWebProxy = true;
          wsHttpBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
          wsHttpBinding.ReaderQuotas = new XmlDictionaryReaderQuotas(); //ReaderQuotas, setting to Max
          wsHttpBinding.ReaderQuotas.MaxArrayLength = wsHttpBinding.ReaderQuotas.MaxBytesPerRead = 2147483647;
          wsHttpBinding.ReaderQuotas.MaxStringContentLength = wsHttpBinding.ReaderQuotas.MaxNameTableCharCount = 2147483647;
          wsHttpBinding.ReaderQuotas.MaxDepth = 2147483647;
    
          //Create the Proxy
          ChannelFactory proxy = new ChannelFactory(wsHttpBinding, endpointAddress);
    
          //Sets the MaxItemsInObjectGraph, so that client can receive large objects
          foreach (var operation in proxy.Endpoint.Contract.Operations) {
              DataContractSerializerOperationBehavior operationBehavior = operation.Behaviors.Find();
              //If DataContractSerializerOperationBehavior is not present in the Behavior, then add
              if (operationBehavior == null) {
                  operationBehavior = new DataContractSerializerOperationBehavior(operation);
                  operation.Behaviors.Add(operationBehavior);
              }
              //IMPORTANT: As 'operationBehavior' is a reference, changing anything here will automatically update the value in list, so no need to add this behavior to behaviorlist
              operationBehavior.MaxItemsInObjectGraph = 2147483647;
          }
          return proxy;
     }
    

    On this proxy object you will need to do .CreateChannel() to use it.

    Hope this helps.

    0 讨论(0)
提交回复
热议问题