Building and Consuming SOAP APIs with .NET Core
๐งผ Building and Consuming SOAP APIs with .NET Core
SOAP (Simple Object Access Protocol) is a protocol for exchanging structured information in web services. While REST APIs are more common today, many enterprise systems still rely on SOAP. .NET Core (now .NET 6/7/8) supports both building and consuming SOAP services.
๐ฆ 1. Consuming a SOAP API in .NET Core
.NET Core doesn't include built-in WCF client tooling like .NET Framework, but you can still consume SOAP services using:
✅ Step-by-Step: Consuming a SOAP Web Service
๐ ️ Option 1: Use Connected Services
Right-click your project → Add → Connected Service
Choose Microsoft WCF Web Service Reference Provider
Enter the WSDL URL (e.g., http://example.com/service?wsdl)
Click Finish
It auto-generates a service proxy in .cs files.
๐ ️ Option 2: Use CLI Tool (dotnet-svcutil)
Install the tool:
bash
Copy
Edit
dotnet tool install --global dotnet-svcutil
Generate proxy classes:
bash
Copy
Edit
dotnet-svcutil http://example.com/service?wsdl
Add generated files to your project.
Use the client in your code:
csharp
Copy
Edit
var client = new MySoapClient(); // generated class
var result = await client.MyMethodAsync("param1");
✅ Ensure you're using a package like System.ServiceModel.Http if needed.
๐️ 2. Building a SOAP API in .NET Core
While .NET Core does not support full WCF server-side services, you have two main options:
✅ Option 1: Use CoreWCF (Open Source WCF for .NET Core)
CoreWCF is a community-driven port of WCF to .NET Core.
๐ง Setup Example:
Install packages:
bash
Copy
Edit
dotnet add package CoreWCF.Http
Create a service contract:
csharp
Copy
Edit
[ServiceContract]
public interface ICalculator
{
[OperationContract]
int Add(int a, int b);
}
Implement the service:
csharp
Copy
Edit
public class CalculatorService : ICalculator
{
public int Add(int a, int b) => a + b;
}
Configure in Program.cs:
csharp
Copy
Edit
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddServiceModelServices();
var app = builder.Build();
app.UseServiceModel(serviceBuilder =>
{
serviceBuilder.AddService<CalculatorService>();
serviceBuilder.AddServiceEndpoint<CalculatorService, ICalculator>(
new BasicHttpBinding(), "/CalculatorService");
});
app.Run();
Access WSDL:
Browse to http://localhost:5000/CalculatorService?wsdl
๐ Secure your service with transport or message-level security.
✅ Option 2: Host with ASP.NET Core MVC + XML Serialization (Non-SOAP)
If SOAP is not strictly required (i.e., only XML format is needed), use XML-formatted responses in a regular ASP.NET Core API:
csharp
Copy
Edit
services.AddControllers().AddXmlSerializerFormatters();
Then decorate your controller and actions to return XML, but this isn’t SOAP compliant.
๐งช 3. Testing SOAP Services
Use tools like SoapUI, Postman, or curl (with raw XML payloads).
Validate request structure against the WSDL.
๐ก️ 4. Security for SOAP APIs
Use BasicHttpBinding with SSL (HTTPS) for transport-level security.
Consider WS-Security headers if interacting with enterprise services.
Use authentication and authorization middleware in ASP.NET Core.
✅ Summary
Task Approach
Consume SOAP dotnet-svcutil or Connected Services
Build SOAP Use CoreWCF or external frameworks
Secure SOAP Use HTTPS, basic auth, or WS-Security
Test SOAP SoapUI, Postman, curl
Learn Full Stack Dot NET Training in Hyderabad
Read More
Caching Strategies for Full Stack .NET Applications
Message Queues and Asynchronous Messaging in .NET
Building Scalable Applications with .NET Core
Event-Driven Architecture in Full Stack .NET Development
Visit Our Quality Thought Training in Hyderabad
Comments
Post a Comment