Introduction to C# SOAP
SOAP (Simple Access Object Protocol) is an XML based protocol and provides facility for applications written on different languages and running on different platforms to interact with each other. It works over HTTP. SOAP is a lightweight protocol as it is based on XML which is a lightweight language. C# SOAP is independent of platform and operating system on which it is working which makes it easier for it to exchange data between different applications working on different platforms. It is a loosely coupled protocol because it doesn’t require communicating applications to be in the same language.
The syntax for defining a SOAP message is as follows:
<SOAP : Envelope xmlns : SOAP = “https://www.educba.com/”>
<SOAP : Header>
</SOAP : Header>
<SOAP : Body>
<SOAP : Fault>
</SOAP : Fault>
</SOAP : Body>
</SOAP : Envelope>
The syntax rules for defining a SOAP message are as follows:
Encoding of a SOAP message should be done using XML language. It should use SOAP Envelope namespace. It should not consist of DTD reference and XML processing instructions.
How does SOAP work in C#?
SOAP works on Marshalling and Demarshalling mechanism. It uses HTTP protocol to send XML based messages called SOAP messages to the server for processing. These SOAP messages contain information for processing. We can call this as an HTTP request and this method of wrapping the information into SOAP message is called Marshalling.
Now, the server takes the request from the client and unwrap the SOAP message sent by the client. The server then processes the request and sends the appropriate response to the client in the form of a SOAP message. This method of un-wrapping the information is called Demarshalling.
Elements of SOAP Message
A Soap message consists of the following elements:
1. SOAP Envelope element: This element is the root element of the SOAP message. It tells that the specific XML document is a SOAP message. It contains details of the SOAP message. Header element: The SOAP header element is an optional element of the SOAP message. But if the SOAP message contains this element then it should be the first child element of the root Envelope element and the child elements of Header should be qualified of a namespace. This element contains information like payment information, authentication credentials etc. SOAP Body element: This element contains the actual information to be exchanged between the two endpoints. It contains request and response information.
Please find below an example of a SOAP Body element from a SOAP response message containing the employee details:
<soap : Body>
2. SOAP Fault element: When a SOAP message is sent to the server then the response returned by the server can contain either the information required in the request on successful processing of the request or it can contain an error message. Thus, this element contains error-related information. If a SOAP message contains this element, then it should be a child element of the Body element.
The sub-elements of the Fault element are as follows:
- <faultCode>: This element contains the code of the underlying fault which helps us to identify the fault.
- <faultString>: This element contains a text message providing a detailed explanation of the fault.
- <faultActor>: This is an optional element and tells us who is responsible for the fault.
- <detail>: This is also an optional element and contains error messages specific to the application.
Please find below a diagram showing the SOAP message structure:
The elements with the coloured background are optional elements of a SOAP message.
Now, let us see the steps required to create a SOAP web service in C#. The steps are as follows:
- In visual studio go to File -> New -> Project to create a new project for the web service.
- Select C# and Web template, under that select ASP.NET Web Application.
- Give the name and location for the solution.
- Now, this project will appear in the Solution Explorer.
- Right-click on the project in solution explorer then select Add -> Web Service (ASMX)
In this service file, you can add your code for service and can execute it as shown in the example under Example section.
Examples to Implement C# SOAP
Below are the examples mentioned :
[WebService(Name ="Sample Web Service")] public class WebService1 : System.Web.Services.WebService
[WebMethod] public string Message()
return "Learning SOAP web service";
After clicking on ‘Message’ (Web method), we will get the following output:
The SOAP request and response in the above snapshot are as follows:
POST /WebService1.asmx HTTP/1.1
Content-Type: text/xml; charset=utf-8
<?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/">
<Message xmlns="http://tempuri.org/" />
In the above message, the first element is the Envelope element. Then this message contains Body element which provides details of the SOAP message. Inside Body element, we can see the name of our Web Method i.e. ‘Message’ which is required to get executed for the result.
HTTP/1.1 200 OKContent-Type: text/xml; charset=utf-8Content-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> <MessageResponse xmlns="http://tempuri.org/"> <MessageResult>string</MessageResult> </MessageResponse> </soap:Body></soap:Envelope>
The first line of this message contains code ‘200’ which indicates a successful response from the server. This message contains Envelope element then Body element containing details of response from the server. We can see a tag ‘MessageResult’ with a value string in it which indicates that the result of our Web Method (Message) will be of type string.
After clicking on the ‘Invoke’ button in the second snapshot, we will get the final result as shown below:
SOAP i.e. Simple Object Access Protocol is a lightweight and loosely coupled protocol which can exchange data between applications written on different programming languages and working on different platforms. It exchanges data in the form of SOAP messages in XML language and works over HTTP protocol.
This is a guide to C# SOAP. Here we discuss an introduction to C# SOAP, syntax, how does it work, with query examples for better understanding. You can also go through our other related articles to learn more –