Comparing ASP.NET SOAP Services and Core APIs
By FoxLearn 2/17/2025 9:15:13 AM 37
This article compares two key approaches for implementing SOAP services in the .NET ecosystem: ASMX and CoreWCF for ASP.NET Core SOAP APIs. The aim is to guide developers in selecting the most suitable SOAP implementation for their applications by offering practical examples of both approaches.
In this article, you will learn:
- The maintenance and enhancement of legacy SOAP services using ASMX.
- The creation of scalable, cross-platform SOAP APIs with CoreWCF in ASP.NET Core.
- How to choose the appropriate framework for your specific application needs.
- How to integrate modern SOAP solutions with REST, gRPC, or transition to contemporary SOAP solutions.
The ASP.NET SOAP Web Services (ASMX)
ASMX, part of the classic ASP.NET Framework, provides an easy way to expose methods as SOAP-based services for clients to consume over HTTP. These services were once the standard for SOAP communication in early ASP.NET applications.
ASMX Features
- Built-in SOAP support: ASMX automatically generates SOAP envelopes and allows easy method exposure using the
[WebMethod]
attribute. - Auto-generated WSDL: A WSDL file is automatically generated for clients to interact with the service.
- Platform limitations: ASMX services require IIS (Internet Information Services) for hosting, making them less flexible for cross-platform environments.
ASMX Web Service
To create an ASMX web service in the .NET Framework, follow these steps:
- Create a new ASP.NET Web Forms project in Visual Studio.
- Add an ASMX file to your project (e.g.,
CalculatorService.asmx
). - Implement the service using the
[WebMethod]
attribute.
For example, basic calculator service:
using System.Web.Services; namespace DemoWCFService { [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] public class CalculatorService : System.Web.Services.WebService { [WebMethod] public int Add(int a, int b) => a + b; [WebMethod] public int Subtract(int a, int b) => a - b; } }
Run the project and visit the .asmx
file in the browser to view the auto-generated WSDL.
How Do ASP.NET Core SOAP APIs Work?
In ASP.NET Core, SOAP services aren’t built-in, but CoreWCF enables developers to implement SOAP-based APIs. CoreWCF brings WCF-like functionality to .NET Core, allowing developers to build modern, scalable, and cross-platform SOAP APIs.
CoreWCF SOAP APIs for ASP.NET Core
- Requires CoreWCF: Unlike ASMX, ASP.NET Core doesn’t natively support SOAP but can do so via CoreWCF.
- Cross-platform support: CoreWCF can run on Windows, Linux, and macOS, making it ideal for cloud-native applications.
- Modern integration: CoreWCF benefits from ASP.NET Core’s middleware, dependency injection, and performance scalability.
ASP.NET Core SOAP API
To create a SOAP API in ASP.NET Core, follow these steps:
Install necessary CoreWCF packages:
dotnet add package CoreWCF dotnet add package CoreWCF.Http
Define the service contract using [ServiceContract]
and [OperationContract]
:
using CoreWCF; namespace DemoWCFService [ServiceContract] public interface ICalculatorService { [OperationContract] double Add(double a, double b); [OperationContract] double Subtract(double a, double b); [OperationContract] double Multiply(double a, double b); [OperationContract] double Divide(double a, double b); }
Implement the service:
using DemoWCFService; namespace DemoWCFService; public class CalculatorService : ICalculatorService { public double Add(double a, double b) => a + b; public double Subtract(double a, double b) => a - b; public double Multiply(double a, double b) => a * b; public double Divide(double a, double b) => b != 0 ? a / b : throw new DivideByZeroException("Cannot divide by zero."); }
Configure CoreWCF in Program.cs
:
using CoreWCF; using CoreWCF.Configuration; using DemoWCFService; var builder = WebApplication.CreateBuilder(args); builder.Services.AddServiceModelServices(); builder.Services.AddServiceModelMetadata(); builder.Services.AddSingleton<CalculatorService>(); builder.Services.AddOpenApi(); var app = builder.Build(); app.UseServiceModel(builder => { builder.AddService<CalculatorService>(); builder.AddServiceEndpoint<CalculatorService, ICalculatorService>( new BasicHttpBinding(), "/CalculatorService"); }); app.MapGet("/calculate/add/{a}/{b}", (double a, double b, CalculatorService service) => { return Results.Ok(new { Result = service.Add(a, b) }); }); app.UseHttpsRedirection(); app.Run();
Differentiating ASMX and ASP.NET Core
Feature | ASP.NET SOAP Web Services (ASMX) | ASP.NET Core SOAP APIs (CoreWCF) |
---|---|---|
Framework | .NET Framework | ASP.NET Core |
Cross-Platform Support | No | Yes |
Middleware and DI Support | No | Yes |
Performance | Moderate | High |
SOAP Support | Built-In | Requires CoreWCF |
Ideal Use Case | Legacy applications | Modern, scalable systems |
When to Choose Which?
- Use ASMX if:
- You are maintaining legacy systems.
- Migrating to ASP.NET Core is not cost-effective.
- Use CoreWCF if:
- You are building new SOAP-based services.
- You need cross-platform and scalable solutions.
- You want to integrate modern technologies like REST, gRPC, or message queues.
While ASMX can still be useful for maintaining legacy applications, CoreWCF provides a modern, scalable, and cross-platform solution for building SOAP APIs in ASP.NET Core. Adopting CoreWCF allows developers to take advantage of modern development practices, performance improvements, and seamless integration with other technologies like REST and gRPC. By choosing CoreWCF, developers can future-proof their SOAP services for enterprise applications.
- Basic Authentication in ASP.NET Core
- How to Implement Stripe Payment Gateway in ASP.NET Core
- How to fix System.InvalidOperationException: Scheme already exists: Identity.Application
- Two-Factor Authentication with Google Authenticator in ASP.NET Core
- Implementing Passkeys to ASP.NET Core Application
- How to Implement Passkey Authentication in ASP.NET Core
- Implementing Google Authentication in the Blazor WebAssembly App
- How to Add a custom InputFormatter in ASP.NET Core