【问题标题】:How can I set the address attribute of a System.ServiceModel.Client.Endpoint object in code?如何在代码中设置 System.ServiceModel.Client.Endpoint 对象的地址属性?
【发布时间】:2018-08-16 21:20:41
【问题描述】:

首先,我不是网络开发人员,所以请放轻松..

我继承了一个使用 Web 服务客户端的项目。我希望能够在多个环境中部署相同的 .config 文件,而无需直接对其进行编辑。为此,我不确定如何处理的唯一属性是<system.serviceModel> 部分中的地址:

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="CA_ServiceSoap" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="1000000" maxBufferPoolSize="524288" maxReceivedMessageSize="1000000" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
      <readerQuotas maxDepth="32" maxStringContentLength="16384" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
      <security mode="Transport">
        <transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
        <message clientCredentialType="UserName" algorithmSuite="Default"/>
      </security>
    </binding>
    <binding name="CA_ServiceSoap1" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
      <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
        <message clientCredentialType="UserName" algorithmSuite="Default"/>
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="https://myserver.contoso.com/CA_Service.asmx" binding="basicHttpBinding" bindingConfiguration="CA_ServiceSoap" contract="WebServices.CA_ServiceSoap" name="CA_ServiceSoap"/>
</client>

有没有办法可以在 .config 的 &lt;endpoint&gt; 标记中为“地址”属性输入一个虚拟值,然后在运行时通过代码正确定义它?

【问题讨论】:

    标签: c# .net web-services wcf


    【解决方案1】:

    创建客户端时,可以在代码中用basicHttpBinding更改端点的地址。

    根据您提供的配置,我猜您创建客户端的代码可能类似于以下内容:

    //Specify the binding to be used for the client.
    BasicHttpBinding binding = new BasicHttpBinding() { Namespace = "WebServices.CA_ServiceSoap" };
    
    //Specify the address to be used for the client.
    EndpointAddress address =
         new EndpointAddress("https://myserver.contoso.com/CA_Service.asmx");
    
    // Create a client that is configured with this address and binding.
    CA_ServiceSoap client = new CA_ServiceSoap(binding, address);
    

    【讨论】:

      【解决方案2】:

      感谢@S.Dav 为我指明了正确的方向。我最终所做的是将我发布的 XML 中的所有内容(它本身只是一个序列化对象)直接包含在我的代码中。唯一可变的属性是 URL:

      BasicHttpBinding binding = new BasicHttpBinding()
      {
          Namespace = "WebServices.CA_ServiceSoap",
          CloseTimeout = new TimeSpan(0, 1, 0),
          OpenTimeout = new TimeSpan(0,1,0),
          ReceiveTimeout = new TimeSpan(0,10,0),
          SendTimeout = new TimeSpan(0,1,0),
          AllowCookies = false,
          BypassProxyOnLocal = false,
          HostNameComparisonMode = HostNameComparisonMode.StrongWildcard,
          MaxBufferSize = 1000000,
          MaxBufferPoolSize = 524288,
          MaxReceivedMessageSize = 1000000,
          MessageEncoding = WSMessageEncoding.Text,
          TextEncoding = Encoding.UTF8,
          TransferMode = TransferMode.Buffered,
          UseDefaultWebProxy = true,
          ReaderQuotas = new XmlDictionaryReaderQuotas()
          {
              MaxDepth = 32,
              MaxStringContentLength = 16384,
              MaxArrayLength = 16384,
              MaxBytesPerRead = 4096,
              MaxNameTableCharCount = 16384
          }
      };
      binding.Security.Mode = BasicHttpSecurityMode.Transport;
      binding.Security.Transport = new HttpTransportSecurity()
      {
          ClientCredentialType = HttpClientCredentialType.None,
          ProxyCredentialType = HttpProxyCredentialType.None,
          Realm = ""
      };
      binding.Security.Message = new BasicHttpMessageSecurity()
      {
          ClientCredentialType = BasicHttpMessageCredentialType.UserName,
          AlgorithmSuite = SecurityAlgorithmSuite.Default
      };
      client = new CA_ServiceSoapClient(binding, new EndpointAddress(Config.WebServiceURL));
      

      作为未来的工作,我计划查看是否可能需要调整这些绑定属性以进行故障排除/性能并将它们添加为 appsettings,然后我将在 BasicHttpBinding 实例化中引用。

      【讨论】:

        猜你喜欢
        • 2021-10-07
        • 1970-01-01
        • 2014-06-23
        • 2015-11-03
        • 1970-01-01
        • 2017-03-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-25
        相关资源
        最近更新 更多