连接没有配置文件的WCF客户端(但通过代码)?

时间:2017-12-12 14:00:30

标签: c# web-services wcf soap basichttpbinding

我知道这个问题已经被问到(以某种形式),但我是新手,并且无法使其发挥作用。 问题:需要创建一个.NET dll(c#),我需要从PowerBuilder(通过COM)调用 - > dll将处理对Web服务的SOAP客户端调用。

在.NET中没有多少经验,但设法与(例如)公共Web服务“交谈”。但问题是我不能使用配置文件(否则找不到端点的错误)。

拜托,有没有人可以帮我解决这个问题(可能是这个代码)?

这是配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="GlobalWeatherSoap" />
      </basicHttpBinding>
      <customBinding>
        <binding name="GlobalWeatherSoap12">
          <textMessageEncoding messageVersion="Soap12" />
          <httpTransport />
        </binding>
      </customBinding>
    </bindings>
    <client>
      <endpoint address="http://www.webservicex.com/globalweather.asmx"
          binding="basicHttpBinding" bindingConfiguration="GlobalWeatherSoap"
          contract="SrvRefWeather.GlobalWeatherSoap" name="GlobalWeatherSoap" />
      <endpoint address="http://www.webservicex.com/globalweather.asmx"
          binding="customBinding" bindingConfiguration="GlobalWeatherSoap12"
          contract="SrvRefWeather.GlobalWeatherSoap" name="GlobalWeatherSoap12" />
    </client>
  </system.serviceModel>
</configuration>

我通过WSDL文件添加了“服务引用”并像这样调用web服务(例如):

SrvRefWeather.GlobalWeatherSoapClient client = new SrvRefWeather.GlobalWeatherSoapClient("GlobalWeatherSoap");
string strCities = client.GetCitiesByCountry("Belgium");

任何人都可以告诉我,我如何能够实现与我所做的相同但没有配置文件?

我想我需要通过'BasicHttpBinding()',但不是如何在我的例子中引用'SrvRefWeather'对象。

任何帮助将不胜感激!真的...

谢谢, 马克。

2 个答案:

答案 0 :(得分:0)

我设法让这个(简单的)示例工作:

BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress("http://www.webservicex.com/globalweather.asmx");

SrvRefWeather.GlobalWeatherSoapClient client = new SrvRefWeather.GlobalWeatherSoapClient(binding, address);
string strCities = client.GetCitiesByCountry("Belgium");

任何人都可以向我解释一下“CONFIG文件中的合同标签”的含义吗? example here

我没有在我的例子中使用它,但它有效...... 所以我想知道它在哪里(以及如果使用)?

感谢。

答案 1 :(得分:0)

没有回答你的问题,但我个人总是会使用XML配置,因为它比在代码中创建所有内容更容易阅读和理解。

如果您不想/不能拥有外部配置文件,则可以在程序集中包含配置XML,例如:作为字符串常量或作为嵌入资源。然后,您可以使用ConfigurationChannelFactory类从此配置数据创建客户端通道。

如果您想探索此方法并且不了解如何使用$(OBJ_D)\ecb3_enc.obj $(OBJ_D)\cfb64enc.obj $(OBJ_D)\cfb64ede.obj \ 来实现此目的,请发表评论或其他问题。