C#使用不同的终结点到同一WebService

时间:2019-01-08 15:16:53

标签: c# wcf

我们有一个带有开发测试和生产环境的设置。因此,在完成开发和测试后,每个服务器都具有相同的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和配置文件中?

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

2 个答案:

答案 0 :(得分:1)

您应该能够将此端点复制并粘贴到<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");

答案 1 :(得分:0)

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

 <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所写的那样使用配置名称来初始化客户端。