C#: WS Service Tutorial (WCF) with 2 SOAP Accessible Methods

Create Project

  • Add New Project > WCF Service Application
ProtoService

Project Layout

Add WebService

  • Add New Item > Web Service ( ASMX )
TemperatureConverter.asmx
  • Update [WebService(Namespace = "https:www.yourhostname.com/")]
  • A Class file of the same name is created

Project Layout

TemperatureConverter.asmx.cs

  • Create two methods to convert Faherenheit to Celsius and F to C
  • Update the WebService Namespace

WebService(Namespace ="")] Annotation

WebMethod Annotation

  • To Expose the methods publicly, add [WebMethod] annotation above the method names

Class File After Updates

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace ProtoService
{
    /// <summary>
    /// Summary description for TemperatureConverter
    /// </summary>
    [WebService(Namespace = "http://www.unityconstruct.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    // [System.Web.Script.Services.ScriptService]
    public class TemperatureConverter : System.Web.Services.WebService
    {
        // annotation for EXPOSING method to the outside world
        [WebMethod]
        public double ConvertFahernheitToCelsius(double Farhenheit)
        {
            double Celsius = (((Farhenheit - 32) * 5) / 9);
            return Celsius;
        }
        [WebMethod]
        public double ConvertCelsiusToFahernheit(double Celsius)
        {
            double Farhenheit = (32 + ((9 * Celsius) / 5));
            return Farhenheit;
        }
    }
}

Run Service

  • FireFox ( or Chrome ) will start up
  • Click 'Service Description' link for the wsdl
  • The two WebService methods will be shown
    • ConvertCelsiusToFahernheit
    • ConvertFahernheitToCelsius


ConvertCelsiusToFahernheit

Test

To test the operation using the HTTP POST protocol, click the 'Invoke' button.

Parameter Value
Celsius:
   

SOAP 1.1

The following is a sample SOAP 1.1 request and response. The placeholders shown need to be replaced with actual values.

POST /TemperatureConverter.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.unityconstruct.org/ConvertCelsiusToFahernheit"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <ConvertCelsiusToFahernheit xmlns="http://www.unityconstruct.org/">
      <Celsius>double</Celsius>
    </ConvertCelsiusToFahernheit>
  </soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <ConvertCelsiusToFahernheitResponse xmlns="http://www.unityconstruct.org/">
      <ConvertCelsiusToFahernheitResult>double</ConvertCelsiusToFahernheitResult>
    </ConvertCelsiusToFahernheitResponse>
  </soap:Body>
</soap:Envelope>

SOAP 1.2

The following is a sample SOAP 1.2 request and response. The placeholders shown need to be replaced with actual values.

POST /TemperatureConverter.asmx HTTP/1.1
Host: localhost
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <ConvertCelsiusToFahernheit xmlns="http://www.unityconstruct.org/">
      <Celsius>double</Celsius>
    </ConvertCelsiusToFahernheit>
  </soap12:Body>
</soap12:Envelope>
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <ConvertCelsiusToFahernheitResponse xmlns="http://www.unityconstruct.org/">
      <ConvertCelsiusToFahernheitResult>double</ConvertCelsiusToFahernheitResult>
    </ConvertCelsiusToFahernheitResponse>
  </soap12:Body>
</soap12:Envelope>

HTTP POST

The following is a sample HTTP POST request and response. The placeholders shown need to be replaced with actual values.

POST /TemperatureConverter.asmx/ConvertCelsiusToFahernheit HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: length

Celsius=string
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<double xmlns="http://www.unityconstruct.org/">double</double>

ConvertFahernheitToCelsius

Test

To test the operation using the HTTP POST protocol, click the 'Invoke' button.

Parameter Value
Farhenheit:
   

SOAP 1.1

The following is a sample SOAP 1.1 request and response. The placeholders shown need to be replaced with actual values.

POST /TemperatureConverter.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.unityconstruct.org/ConvertFahernheitToCelsius"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <ConvertFahernheitToCelsius xmlns="http://www.unityconstruct.org/">
      <Farhenheit>double</Farhenheit>
    </ConvertFahernheitToCelsius>
  </soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <ConvertFahernheitToCelsiusResponse xmlns="http://www.unityconstruct.org/">
      <ConvertFahernheitToCelsiusResult>double</ConvertFahernheitToCelsiusResult>
    </ConvertFahernheitToCelsiusResponse>
  </soap:Body>
</soap:Envelope>

SOAP 1.2

The following is a sample SOAP 1.2 request and response. The placeholders shown need to be replaced with actual values.

POST /TemperatureConverter.asmx HTTP/1.1
Host: localhost
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <ConvertFahernheitToCelsius xmlns="http://www.unityconstruct.org/">
      <Farhenheit>double</Farhenheit>
    </ConvertFahernheitToCelsius>
  </soap12:Body>
</soap12:Envelope>
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <ConvertFahernheitToCelsiusResponse xmlns="http://www.unityconstruct.org/">
      <ConvertFahernheitToCelsiusResult>double</ConvertFahernheitToCelsiusResult>
    </ConvertFahernheitToCelsiusResponse>
  </soap12:Body>
</soap12:Envelope>

HTTP POST

The following is a sample HTTP POST request and response. The placeholders shown need to be replaced with actual values.

POST /TemperatureConverter.asmx/ConvertFahernheitToCelsius HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: length

Farhenheit=string
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<double xmlns="http://www.unityconstruct.org/">double</double>

Change the url/port of the webservice endpoint

  • Open Project > Properties > Web  Project Url
http://localhost:49291

To Allow External Connections to the WebService

Quickly: Was getting HTTP400 and/or HTTP503 errors when attempting to connect remotely.

Turn off Windows Firewall to simplify troubleshooting

  • if this is the issue NO response will be received & it will timeout
  • if firewall is fine, then the HTTP 400(Invalid Hostname) and/or HTTP503 (Service not available) might be encountered

Adding urlacl using netsh resolved the HTTP400 error ( WITH firewall disabled )

netsh http add urlacl url=http://*:49294/ user=everyone
  • this will add the host(s) to the access list so the OS will allow the connection

Updating applicationhost.config to include BOTH resolved the 503 ( WITH firewall disabled )

<binding protocol="http" bindingInformation="*:49294:localhost" /> 
<binding protocol="http" bindingInformation="*:49294:*" /> 
  • This will add the host(s) to the IIS WEBSITE whitelist
  • There were several site "name" elements & only ONE required the 'localhost' machine name, presumably for the automatic firing & connection of a browser when starting the IIS 'website/service'
  • THIS is the binding that needs the additional "*" entry

 

More Detail for Allowing External Connections to the WebService

Turn off Windows Firewall (or Linux firewall)

  • if working, then just add a New Rule for the inbound port#
  • then Enable firewall & retry

Update applicationhost.config to allow remote ip address access

modify the config here:
$(solutionDir)\.vs\config\applicationhost.config

or here:

%userprofile%\Documents\IISExpress\config\applicationhost.config

from

<binding protocol="http" bindingInformation="*:49294:localhost" />  

to

<binding protocol="http" bindingInformation="*:49294:*" /> 

Alternatively, one can also add an additional binding for a specific machine

<binding protocol="http" bindingInformation="*:49294:192.168.0.101" />

or

<binding protocol="http" bindingInformation="*:49294:MyComputerHostName" />

 

Using netsh as LAST RECOURSE

netsh command

  • change 49291/49294 to whatever IIS is using

Add

netsh http add urlacl url=http://*:49291/ user=everyone

netsh http add urlacl url=http://*:49294/ user=everyone

Delete

netsh http delete urlacl url=http://*:49291/

netsh http delete urlacl url=http://*:49294/

Import WSDL into SoapUI

  • Save WDSL provided by the webservice
  • Import into SoapUI

  • Generating Test Suites

 

Soap Project Tree View

  • Setup Endpoints

  • Changing hostname/port

  • Assign endpoint to "All Requests and TestRequests"

  • Send Request
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:unit="http://www.unityconstruct.org/">
   <soap:Header/>
   <soap:Body>
      <unit:ConvertCelsiusToFahernheit>
         <unit:Celsius>110</unit:Celsius>
      </unit:ConvertCelsiusToFahernheit>
   </soap:Body>
</soap:Envelope>
  • Receive Response
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <ConvertCelsiusToFahernheitResponse xmlns="http://www.unityconstruct.org/">
         <ConvertCelsiusToFahernheitResult>230</ConvertCelsiusToFahernheitResult>
      </ConvertCelsiusToFahernheitResponse>
   </soap:Body>
</soap:Envelope>

WSDL Created by WebService

<wsdl:definitions targetNamespace="http://www.unityconstruct.org/">
    <wsdl:types>
        <s:schema elementFormDefault="qualified" targetNamespace="http://www.unityconstruct.org/">
            <s:element name="ConvertFahernheitToCelsius">
                <s:complexType>
                    <s:sequence>
                        <s:element minOccurs="1" maxOccurs="1" name="Farhenheit" type="s:double"/>
                    </s:sequence>
                </s:complexType>
            </s:element>
            <s:element name="ConvertFahernheitToCelsiusResponse">
                <s:complexType>
                    <s:sequence>
                        <s:element minOccurs="1" maxOccurs="1" name="ConvertFahernheitToCelsiusResult" type="s:double"/>
                    </s:sequence>
                </s:complexType>
            </s:element>
            <s:element name="ConvertCelsiusToFahernheit">
                <s:complexType>
                    <s:sequence>
                        <s:element minOccurs="1" maxOccurs="1" name="Celsius" type="s:double"/>
                    </s:sequence>
                </s:complexType>
            </s:element>
            <s:element name="ConvertCelsiusToFahernheitResponse">
                <s:complexType>
                    <s:sequence>
                        <s:element minOccurs="1" maxOccurs="1" name="ConvertCelsiusToFahernheitResult" type="s:double"/>
                    </s:sequence>
                </s:complexType>
            </s:element>
        </s:schema>
    </wsdl:types>
    <wsdl:message name="ConvertFahernheitToCelsiusSoapIn">
        <wsdl:part name="parameters" element="tns:ConvertFahernheitToCelsius"/>
    </wsdl:message>
    <wsdl:message name="ConvertFahernheitToCelsiusSoapOut">
        <wsdl:part name="parameters" element="tns:ConvertFahernheitToCelsiusResponse"/>
    </wsdl:message>
    <wsdl:message name="ConvertCelsiusToFahernheitSoapIn">
        <wsdl:part name="parameters" element="tns:ConvertCelsiusToFahernheit"/>
    </wsdl:message>
    <wsdl:message name="ConvertCelsiusToFahernheitSoapOut">
        <wsdl:part name="parameters" element="tns:ConvertCelsiusToFahernheitResponse"/>
    </wsdl:message>
    <wsdl:portType name="TemperatureConverterSoap">
        <wsdl:operation name="ConvertFahernheitToCelsius">
            <wsdl:input message="tns:ConvertFahernheitToCelsiusSoapIn"/>
            <wsdl:output message="tns:ConvertFahernheitToCelsiusSoapOut"/>
        </wsdl:operation>
        <wsdl:operation name="ConvertCelsiusToFahernheit">
            <wsdl:input message="tns:ConvertCelsiusToFahernheitSoapIn"/>
            <wsdl:output message="tns:ConvertCelsiusToFahernheitSoapOut"/>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="TemperatureConverterSoap" type="tns:TemperatureConverterSoap">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="ConvertFahernheitToCelsius">
            <soap:operation soapAction="http://www.unityconstruct.org/ConvertFahernheitToCelsius" style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="ConvertCelsiusToFahernheit">
            <soap:operation soapAction="http://www.unityconstruct.org/ConvertCelsiusToFahernheit" style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:binding name="TemperatureConverterSoap12" type="tns:TemperatureConverterSoap">
        <soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="ConvertFahernheitToCelsius">
            <soap12:operation soapAction="http://www.unityconstruct.org/ConvertFahernheitToCelsius" style="document"/>
            <wsdl:input>
                <soap12:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap12:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="ConvertCelsiusToFahernheit">
            <soap12:operation soapAction="http://www.unityconstruct.org/ConvertCelsiusToFahernheit" style="document"/>
            <wsdl:input>
                <soap12:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap12:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="TemperatureConverter">
        <wsdl:port name="TemperatureConverterSoap" binding="tns:TemperatureConverterSoap">
            <soap:address location="http://localhost:49294/TemperatureConverter.asmx"/>
        </wsdl:port>
        <wsdl:port name="TemperatureConverterSoap12" binding="tns:TemperatureConverterSoap12">
            <soap12:address location="http://localhost:49294/TemperatureConverter.asmx"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>