Home  >  Article  >  Backend Development  >  C# SOAP

C# SOAP

WBOY
WBOYOriginal
2024-09-03 15:18:261159browse

SOAP (Simple Access Object Protocol) is an XML-based protocol and provides a facility for applications written in 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 the 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.

Syntax

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 the 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 mechanisms. 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 an HTTP request, and this method of wrapping the information into a SOAP message is called Marshalling.

Now, the server takes the request from the client and unwraps 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 unwrapping the information is called Demarshalling.

C# SOAP

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 the Header should be qualified as 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:

Code:

<soap : Body>
<GetEmployeeDetails>
<EmployeeName>John Duffel</EmployeeName>
<EmployeeCode>EI66</EmployeeCode>
</GetEmployeeDetails>
</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:

  • : This element contains the code of the underlying fault, which helps us to identify the fault.
  • : This element contains a text message providing a detailed explanation of the fault.
  • : This is an optional element and tells us who is responsible for the fault.
  • : This is also an optional element and contains error messages specific to the application.

Please find below a diagram showing the SOAP message structure:

C# SOAP

The elements with the colored background are optional elements of a SOAP message.

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 of 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 the service and can execute it as shown in the example under the Example section.

Examples to Implement C# SOAP

Below are the examples mentioned :

Example #1

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebApplication4
{
[WebService(Name ="Sample Web Service")]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string Message()
{
return "Learning SOAP web service";
}
}
}

Output:

C# SOAP

After clicking on ‘Message’ (Web method), we will get the following output:

C# SOAP

Example #2

The SOAP request and response in the above snapshot are as follows:

Code:

POST /WebService1.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/Message"
<?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>
<Message xmlns="http://tempuri.org/" />
</soap:Body>
</soap:Envelope>

In the above message, the first element is the Envelope element. Then this message contains the Body element, which provides details of the SOAP message.

Code:

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 an Envelope element and then a Body element containing details of the response from the server. We can see a tag ‘MessageResult’ with a value string, indicating that our Web Method (Message) result will be of a type string.

After clicking on the ‘Invoke’ button in the second snapshot, we will get the final result as shown below:

Output:

C# SOAP

Conclusion

SOAP i.e. Simple Object Access Protocol, is a lightweight and loosely coupled protocol that can exchange data between applications written in 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.

The above is the detailed content of C# SOAP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:C# BaseNext article:C# Base