C#SOAP客户端导致异常

时间:2015-09-16 12:01:20

标签: c# web-services soap wsdl

相当新的SOAP,但不是C#或PHP的新手。我在http://basic.tip2tail.co.uk/?wsdl创建了一个基本的SOAP服务。这应该公开一个方法submitSoap,它接受​​一个字符串参数并返回一个整数。

我已经通过连接一个我用另一种语言编写的简单客户端测试了这个SOAP服务,并确认它按预期工作。

但是,我无法用C#来解决这个问题。我添加了Service Reference指向我的WSDL。它显示为2个服务。该方法在BasicSOAP_PortType下显示为公开。 WSDL Ref

然后我将using SOAPTest.BasicSOAP;添加到我的C#程序中。但是我无法弄清楚如何编写对此方法的调用。我认为它类似于(在按钮点击事件中):

BasicSOAP_PortType oSoap = new BasicSOAP_PortType();
int iReturn = oSoap.submitSoap("this is the string sent");

但是这不起作用并产生以下异常:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.ServiceModel.dll

Additional information: Could not find default endpoint element that references contract 'BasicSOAP.BasicSOAP_PortType' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.

任何帮助表示赞赏!

标记

1 个答案:

答案 0 :(得分:1)

我得到了它的工作。添加服务引用后,我的代码如下所示:

    using (var client = new basicsoap_ref.BasicSOAP_PortTypeClient())
    {
        try
        {
            int result = 0;
            string resultString = client.submitSoap("<root><test>Will</test></root>");
            int.TryParse(resultString, out result);
            Console.WriteLine(result);
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
        }
        Console.Write("Done");
    }

我的配置文件的服务模型部分如下所示:

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicSOAP_Binding" />
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://basic.tip2tail.co.uk/"
          binding="basicHttpBinding" bindingConfiguration="BasicSOAP_Binding"
          contract="basicsoap_ref.BasicSOAP_PortType" name="MyServicesSoap" />
    </client>
</system.serviceModel>