Service-Oriented Architecture (SOA): Complete Guide with Examples

What is SOA?

Service-Oriented Architecture (SOA) is a design approach where software is composed of independent, loosely-coupled services that communicate via standard protocols.

Key Characteristics of SOA

  • Loose Coupling
  • Reusability
  • Interoperability
  • Autonomy
  • Standardized Communication
  • Discoverability

Example Scenario: Online Shopping System

We'll build 3 independent services using ASP.NET Core Web API.

  • User Service: Manages users
  • Product Service: Manages products
  • Order Service: Places orders by calling other services

1. User Service

Dotnet new webapi -n UserService

// UserService/Controllers/UserController.cs
[Route("api/users")]
[ApiController]
public class UserController : ControllerBase
{
    [HttpGet("{id}")]
    public IActionResult GetUser(int id)
    {
        var user = new 
        { 
            Id = id, 
            Name = "John Doe", 
            Email = "[email protected]" 
        };
        
        return Ok(user);
    }
}

2. Product Service

Dotnet new webapi -n ProductService

// ProductService/Controllers/ProductController.cs
[Route("api/products")]
[ApiController]
public class ProductController : ControllerBase
{
    [HttpGet("{id}")]
    public IActionResult GetProduct(int id)
    {
        var product = new 
        { 
            Id = id, 
            Name = "Laptop", 
            Price = 1200.99 
        };
        
        return Ok(product);
    }
}

3. Order Service

Dotnet new webapi -n OrderService

// OrderService/Controllers/OrderController.cs
[Route("api/orders")]
[ApiController]
public class OrderController : ControllerBase
{
    private readonly HttpClient _httpClient;

    public OrderController(HttpClient httpClient)
    {
        _httpClient = httpClient;
    }

    [HttpPost]
    public async Task CreateOrder(int userId, int productId)
    {
        var userResponse = await _httpClient.GetStringAsync($"http://localhost:5001/api/users/{userId}");
        var user = JsonSerializer.Deserialize<dynamic>(userResponse);

        var productResponse = await _httpClient.GetStringAsync($"http://localhost:5002/api/products/{productId}");
        var product = JsonSerializer.Deserialize<dynamic>(productResponse);

        var order = new
        {
            OrderId = 101,
            User = user,
            Product = product,
            Status = "Order Placed"
        };

        return Ok(order);
    }
}

Running the Services

  • Update launchSettings.json with different ports: 5001, 5002, 5003
  • Run each service using dotnet run --project <ServiceName>

Calling the Order API

POST http://localhost:5003/api/orders?userId=1&productId=101

{
  "OrderId": 101,
  "User": {
    "Id": 1,
    "Name": "John Doe",
    "Email": "[email protected]"
  },
  "Product": {
    "Id": 101,
    "Name": "Laptop",
    "Price": 1200.99
  },
  "Status": "Order Placed"
}

Benefits of SOA in .NET

  • Independent Deployment
  • Scalable Services
  • Reusable APIs
  • Cross-Platform Integration

Conclusion

SOA in .NET, especially with ASP.NET Core Web API, provides a modular, scalable, and loosely-coupled architecture. It’s ideal for enterprise-level systems with complex, evolving requirements.

Up Next
    Ebook Download
    View all
    Learn
    View all