C# SOAP 客户端导致异常

C# SOAP client causing exception

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

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

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

然后我将 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.

感谢任何帮助!

马克

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

    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>