以编程方式配置 wcf 服务

发布于 2024-12-26 02:24:59 字数 2034 浏览 1 评论 0原文

我有一个远程 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>

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 , everything is ok, (I mean MyService s = new MyService()).
Now I want to configure the wcf programmatically . it's simple till I arrive to the authentication issue , it was so hard to do that . Here is the app.config which I use , you can see there my security configurations .

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一曲爱恨情仇 2025-01-02 02:24:59

我已经完成了此操作,您可能需要修改配置中的安全模式代码。

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() 才能使用它。

希望这有帮助。

I have done this, you might have to modify your code for security mode you have in 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;
 }

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

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文