Search code examples
c#web-servicessoapwsdl

C# SOAP client causing exception


Fairly new to SOAP however not new to C# or PHP. I have created a basic SOAP service at http://basic.tip2tail.co.uk/?wsdl. This should expose a method submitSoap which takes a string parameter and returns an integer.

I have tested this SOAP service by connecting a simple client I have written in another language and confirmed it works as expected.

However, I cannot get my head round this in C#. I have added a Service Reference pointing at my WSDL. It shows as 2 services. The method shows as exposed under BasicSOAP_PortType. WSDL Ref

I have then added using SOAPTest.BasicSOAP; to my C# program. However I cannot work out how to then code a call to this method. I thoughts it would be similar to (in a button click event):

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

However this does not work and generates the following exception:

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.

Any help appreciated!

Mark


Solution

  • I got it to work. After adding the service ref, my code looks like this:

        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");
        }
    

    And the service model portion of my config file looks like this:

    <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>