以编程方式配置wcf服务

时间:2012-01-11 13:30:24

标签: .net wcf wcf-security wcf-binding wcf-data-services

我有一个远程wcf服务,我通过WSHttpBinding连接它。如果我使用空服务构造函数,这意味着它将从app.config中获取所有配置,一切正常,(我的意思是MyService s = new MyService())。 现在我想以编程方式配置wcf。这很简单,直到我到达身份验证问题,这很难做到。这是我使用的app.config,你可以看到我的安全配置。

<system.serviceModel>
    <bindings>
        <wsHttpBinding>
            <binding name="SecuredEndPoint" closeTimeout="00:01:00" openTimeout="00:01:00"
                receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false"
                transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                allowCookies="false">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <reliableSession ordered="true" inactivityTimeout="00:10:00"
                    enabled="true" />
                <security mode="Message">
                    <transport clientCredentialType="Windows" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="UserName" negotiateServiceCredential="true"
                        algorithmSuite="Default" />
                </security>
            </binding>               
        </wsHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://MyWcfService.svc"
            binding="wsHttpBinding" bindingConfiguration="SecuredEndPoint"
            contract="ServiceReference1.IMyService" name="SecuredEndPoint">
            <identity>
                <certificate encodedValue="*******************************************************************" />
            </identity>
        </endpoint>            
    </client>
</system.serviceModel>

1 个答案:

答案 0 :(得分:11)

我已经这样做了,您可能需要修改您在config

中的安全模式的代码
public virtual ChannelFactory<T> Proxy<T>(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<T> proxy = new ChannelFactory<T>(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<DataContractSerializerOperationBehavior>();
          //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;
 }

在这个proxy对象上,您需要.CreateChannel()才能使用它。

希望这有帮助。