【问题标题】:C# using different endpoints to the same WebServiceC# 对同一个 WebService 使用不同的端点
【发布时间】:2019-06-03 07:41:49
【问题描述】:

我们有一个设置,包括开发测试和生产环境。所以当开发和测试完成时,每台服务器都有相同的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";
}

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

如果您还没有弄清楚 :-),我想定义另外两个命名端点,但我不确定如何去做。有没有类似向导的方法,还是我必须复制/过去 app.config 和配置文件中的端点?

任何帮助将不胜感激。提前非常感谢。

【问题讨论】:

    标签: c# wcf


    【解决方案1】:

    您应该能够将此端点复制并粘贴到您的 &lt;client&gt; 节点中:

     <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");
    

    【讨论】:

    • 非常感谢,很抱歉没有尽快回复您。我实际上以为我已经尝试过你的解决方案,但我一定错过了一些东西,因为它就像一个魅力。再次非常感谢。
    【解决方案2】:

    如果你的服务有多个服务端点,应该是这样的

     <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 所写的那样。

    【讨论】:

    • 非常感谢您花时间写回复。我无法控制服务器端的事情,但事实证明这不是必需的,@Popo 的建议就足够了。不过还是非常感谢
    猜你喜欢
    • 1970-01-01
    • 2021-07-09
    • 2017-12-03
    • 1970-01-01
    • 2016-09-07
    • 2011-05-20
    • 2012-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多