C# 对同一个 WebService 使用不同的端点

C# using different endpoints to the same WebService

我们有一个设置,具有开发测试和生产环境。所以当开发和测试完成后,每个服务器都有相同的WebServices。 这是我第一次这样做,但在开发中。环境 我已经使用 Visual Studio (2017) “添加服务引用”功能编写了一个 WebService 和一个 C# 客户端。所以我有一个这样的 app.config 文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BizTalkInterfaceServiceSoapBinding">
          <security mode="Transport">
            <transport clientCredentialType="Basic" proxyCredentialType="Basic" realm="" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="https://xxx.xxx.xxx.xxx:xxxxx/and/so/on"
          binding="basicHttpBinding" bindingConfiguration="BizTalkInterfaceServiceSoapBinding"
          contract="ServiceReference.BizTalkInterface" name="BizTalkInterfacePort" />
    </client>
  </system.serviceModel>
</configuration>

还有一个 Connected Services->ServiceReference 结构,带有 .wsdl、configuration.svcinfo、configuration91.svcinfo 和 Reference.svcmap 文件。不知道把这些文件的内容展示出来有没有意义?

我这样初始化客户端:

protected BizTalkInterfaceClient client;

protected ServiceBase()
{
    client = new BizTalkInterfaceClient("BizTalkInterfacePort");
    client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
    client.ClientCredentials.UserName.UserName = "xxx@xxxdomain";
    client.ClientCredentials.UserName.Password = "xxxxxx";
}

无论如何 - 这一切都很好,而且工作正常。

如果你还没有弄明白:-),我想定义另外两个命名端点,但我不确定如何去做。是否有类似向导的方法,或者我是否必须 copy/past app.config 和配置文件中的端点?

如有任何帮助,我们将不胜感激。非常感谢。

您应该能够将此端点复制并粘贴到您的 <client> 节点中:

 <endpoint address="https://xxx.xxx.xxx.xxx:xxxxx/and/so/on"
      binding="basicHttpBinding" bindingConfiguration="BizTalkInterfaceServiceSoapBinding"
      contract="ServiceReference.BizTalkInterface" name="BizTalkInterfacePort" />

只是给它一个不同的名字。

此外,当您初始化客户端时,您将在此处使用相应的名称:

client = new BizTalkInterfaceClient("BizTalkInterfacePort");

示例:

<endpoint address="https://xxx.xxx.xxx.xxx:xxxxx/and/so/on"
      binding="basicHttpBinding" bindingConfiguration="BizTalkInterfaceServiceSoapBinding"
      contract="ServiceReference.BizTalkInterface" name="BizTalkInterfacePortProd" />

client = new BizTalkInterfaceClient("BizTalkInterfacePortProd");

如果您的服务有多个服务端点,它应该像

 <service name="Service.CalculatorService"  >
    <endpoint address="http://localhost:3721/calculator"  binding="basicHttpBinding"  bindingConfiguration="ECMSBindingConfig" contract="ServiceInterface.ICalculatorService"></endpoint>
     <endpoint address="http://localhost:4000/calculator"  binding="wsHttpBinding"   contract="ServiceInterface.ICalculatorService"></endpoint>
  </service>

然后您可以使用 wsdl 地址添加对该服务的引用。 添加引用后,您的客户端中应该有两个具有端点名称的端点,例如

<endpoint address="http://localhost:3721/calculator" binding="basicHttpBinding"
  bindingConfiguration="BasicHttpBinding_ICalculatorService" contract="Calculator.ICalculatorService"
  name="BasicHttpBinding_ICalculatorService" />
<endpoint address="http://localhost:4000/calculator" binding="wsHttpBinding"
  bindingConfiguration="WSHttpBinding_ICalculatorService" contract="Calculator.ICalculatorService"
  name="WSHttpBinding_ICalculatorService">

然后在您的客户端中,您可以使用您的配置名称初始化您的客户端,就像 Popo 所写的那样。