C#导致异常的SOAP客户端

C#导致异常的SOAP客户端,c#,web-services,soap,wsdl,C#,Web Services,Soap,Wsdl,对SOAP相当陌生,但对C#或PHP并不陌生。我已经在http://basic.tip2tail.co.uk/?wsdl。这将公开一个方法submitSoap,该方法接受一个字符串参数并返回一个整数 我已经通过连接一个用另一种语言编写的简单客户端来测试这个SOAP服务,并确认它按预期工作 然而,在C#中我无法理解这一点。我添加了一个指向WSDL的服务引用。它显示为2个服务。该方法显示为BasicSOAP_PortType下的exposed。 然后,我使用SOAPTest.BasicSOAP添加

对SOAP相当陌生,但对C#或PHP并不陌生。我已经在
http://basic.tip2tail.co.uk/?wsdl
。这将公开一个方法
submitSoap
,该方法接受一个字符串参数并返回一个整数

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

然而,在C#中我无法理解这一点。我添加了一个指向WSDL的
服务引用。它显示为2个服务。该方法显示为BasicSOAP_PortType下的exposed。

然后,我使用SOAPTest.BasicSOAP添加了
到我的C#程序。但是,我无法确定如何编写对该方法的调用代码。我认为这类似于(在按钮点击事件中):

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

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>
使用(var client=new basicsoap\u ref.basicsoap\u PortTypeClient())
{
尝试
{
int结果=0;
string resultString=client.submitSoap(“Will”);
int.TryParse(结果字符串,输出结果);
控制台写入线(结果);
}
捕获(例外情况除外)
{
控制台。写入(例如消息);
}
控制台。写入(“完成”);
}
我的配置文件的服务模型部分如下所示:

    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>


但如何获取整数返回值?请参阅我的更新答案。它实际上返回一个字符串,您必须解析为int。您是否将此服务模型部分添加到配置文件中?对不起,我没有-我的错!那么这不是自动生成的吗?每次都必须手动编辑app.config XML吗?回答接受!这很奇怪,因为据我记忆所及,这是以前为我添加的,但这次没有。我不知道这是否与wsdl的设计或是什么有关,但很高兴你成功了。嗨,马克,你能发布你的客户端app.config吗?@DotNetHitMan
@DotNetHitMan这是我需要手动修改的吗?这不应该从WSDL自动创建吗?