Simple Object Access Protocol (SOAP)

Simple Object Access Protocol (SOAP)

SOAP (Simple Object Access Protocol), is a protocol specification for exchanging structured information in the implementation of Web Services in computer networks. It relies on XML Information Set for its message format, and usually relies on other Application Layer protocols, most notably Hypertext Transfer Protocol (HTTP) or Simple Mail Transfer Protocol (SMTP), for message negotiation and transmission.

SOAP can form the foundation layer of a web services protocol stack, providing a basic messaging framework upon which web services can be built. This XML-based protocol consists of three parts: an envelope, which defines what is in the message and how to process it, a set of encoding rules for expressing instances of application-defined datatypes, and a convention for representing procedure calls and responses. SOAP has three major characteristics: extensibility (security and WS-routing are among the extensions under development), neutrality (SOAP can be used over any transport protocol such as HTTP, SMTP, TCP, or JMS), and independence (SOAP allows for any programming model).

The SOAP architecture consists of several layers of specifications for: message format, Message Exchange Patterns (MEP), underlying transport protocol bindings, message processing models, and protocol extensibility. SOAP is the successor of XML-RPC, though it borrows its transport and interaction neutrality and the envelope/header/body from elsewhere (probably from WDDX).

SOAP Messaging Framework

The SOAP specification defines the messaging framework which consists of:

• The SOAP processing model defining the rules for processing a SOAP message
• The SOAP extensibility model defining the concepts of SOAP features and SOAP modules
• The SOAP underlying protocol binding framework describing the rules for defining a binding to an underlying protocol that can be used for exchanging SOAP messages between SOAP nodes
• The SOAP message construct defining the structure of a SOAP message

Both SMTP and HTTP are valid application layer protocols used as transport for SOAP, but HTTP has gained wider acceptance as it works well with today's internet infrastructure; specifically, HTTP works well with network firewalls. SOAP may also be used over HTTPS (which is the same protocol as HTTP at the application level, but uses an encrypted transport protocol underneath) with either simple or mutual authentication; this is the advocated WS-I method to provide web service security as stated in the WS-I Basic Profile 1.1.

Why SOAP Is Used?

SOAP is used when systems need:

• Strong security requirements
• Formal communication contracts
• Reliable and structured messaging
• Enterprise-grade interoperability

Typical use cases:

• Banking systems
• Payment gateways
• Telecom systems
• Government services

Key Features of SOAP

1. XML-Based Messaging

All data is formatted in XML, making it highly structured but verbose.

2. Strict Contract (WSDL)

Services are defined using WSDL (Web Services Description Language):

• Describes operations
• Defines request/response formats

3. Transport Independent

SOAP can work over:

• HTTP/HTTPS (most common)
• SMTP
• TCP, etc.

4. Built-in Error Handling

Uses SOAP Faults to report errors in a structured way.

5. Extensibility

Supports additional standards like:

• WS-Security (encryption, authentication)
• WS-ReliableMessaging

C# SOAP Examples

1. Modern .NET SOAP Client (Recommended)

For .NET Core / .NET 5+ / .NET 6+ / .NET 8.

The most common approach is using:

• dotnet-svcutil
• Connected Services
• generated proxy classes

Example SOAP Service

Assume this WSDL:

https:/www.howcsharp.com/CalculatorService.svc?wsdl

Install SOAP tooling

dotnet tool install --global dotnet-svcutil

Generate client:

dotnet-svcutil https://www.howcsharp.com/CalculatorService.svc?wsdl

This creates proxy classes.

Example Client Code

using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        var client = new CalculatorServiceClient(
            CalculatorServiceClient.EndpointConfiguration.BasicHttpBinding_ICalculatorService);

        int result = await client.AddAsync(5, 3);

        Console.WriteLine($"Result: {result}");

        await client.CloseAsync();
    }
}

2. SOAP Request with Raw XML + HttpClient

Useful when:

• you don't want generated proxies
• the SOAP service is old/broken
• you need full control

Example

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        string soap = @"<?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>
    <Add xmlns=""http://tempuri.org/"">
      <a>5</a>
      <b>3</b>
    </Add>
  </soap:Body>
</soap:Envelope>";

        using var client = new HttpClient();

        var content = new StringContent(
            soap,
            Encoding.UTF8,
            "text/xml");

        content.Headers.Add("SOAPAction", "\"http://tempuri.org/Add\"");

        var response = await client.PostAsync(
            "https://www.howcsharp.com/Calculator.asmx",
            content);

        string xml = await response.Content.ReadAsStringAsync();

        Console.WriteLine(xml);
    }
}

3. ASP.NET Core SOAP Service Example

SOAP services in modern .NET usually use the library: SoapCore

Install:

dotnet add package SoapCore

Service Contract

using System.ServiceModel;

[ServiceContract]
public interface ICalculatorService
{
    [OperationContract]
    int Add(int a, int b);
}

Implementation

public class CalculatorService : ICalculatorService
{
    public int Add(int a, int b)
    {
        return a + b;
    }
}

Program.cs

using SoapCore;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSoapCore();
builder.Services.AddSingleton<ICalculatorService, CalculatorService>();

var app = builder.Build();

app.UseRouting();

app.UseEndpoints(endpoints =>
{
    endpoints.UseSoapEndpoint<ICalculatorService>(
        "/Service.asmx",
        new SoapEncoderOptions(),
        SoapSerializer.XmlSerializer);
});

app.Run();

4. Legacy .NET Framework ASMX Example

Older enterprise systems still use ASMX.

Service

using System.Web.Services;

[WebService(Namespace = "http://tempuri.org/")]
public class CalculatorService : WebService
{
    [WebMethod]
    public int Add(int a, int b)
    {
        return a + b;
    }
}

5. WCF SOAP Example (.NET Framework)

Classic enterprise SOAP stack.

Service Contract

[ServiceContract]
public interface ICalculator
{
    [OperationContract]
    int Add(int a, int b);
}

Implementation

public class CalculatorService : ICalculator
{
    public int Add(int a, int b)
    {
        return a + b;
    }
}

web.config

<system.serviceModel>
  <services>
    <service name="CalculatorService">
      <endpoint
        address=""
        binding="basicHttpBinding"
        contract="ICalculator" />
    </service>
  </services>
</system.serviceModel>

6. Parsing SOAP XML Response

Sometimes you only need the response value.

using System.Xml.Linq;

XDocument doc = XDocument.Parse(xml);

XNamespace ns = "http://tempuri.org/";

var result = doc.Descendants(ns + "AddResult")
       .FirstOrDefault()?.Value;

Console.WriteLine(result);

Advantages of SOAP

• Very secure and reliable
• Strong formal contract (WSDL)
• Platform and language independent
• Good for enterprise integration
• Built-in error handling

Disadvantages of SOAP

• Heavy and verbose (XML overhead)
• Slower compared to REST
• Complex to implement and debug
• Not ideal for lightweight or mobile applications

Contents related to 'Simple Object Access Protocol (SOAP)'

Extensible Markup Language (XML)
Extensible Markup Language (XML)
Element Management System (EMS)
Element Management System (EMS)
Axis
Axis
Stomp
Stomp