代码之家  ›  专栏  ›  技术社区  ›  Mike Bruno

如何在代码中设置System.ServiceModel.Client.Endpoint对象的address属性?

  •  1
  • Mike Bruno  · 技术社区  · 6 年前

    首先,我不是一个网络开发人员,所以对我放轻松。。

    我继承了一个使用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>
    

    <endpoint> 在.config中标记,然后在运行时通过代码正确定义它?

    2 回复  |  直到 6 年前
        1
  •  1
  •   S.Dav    6 年前

    可以使用 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
  •  0
  •   Mike Bruno    6 年前

    感谢@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实例化中引用它们。