SOAP API
Introduction
SOAP (Simple Object Access Protocol) is a protocol for exchanging structured information in web services. In this tutorial, we will cover how to create and consume SOAP APIs using .NET. We will create a SOAP service and a client to consume this service.
Prerequisites
- Visual Studio installed with .NET development workload
- Basic knowledge of .NET and C#
Creating a SOAP Service
First, create a new SOAP service project in Visual Studio:
- Open Visual Studio and click "Create a new project".
- Select "ASP.NET Web Application (.NET Framework)" and click "Next".
- Set the name and location for your project and click "Create".
- Choose "Empty" as the template and ensure "Web Forms" is unchecked. Click "Create".
Adding a WCF Service
Add a WCF Service to the project:
- Right-click on the project in Solution Explorer, select "Add" > "New Item".
- Choose "WCF Service" and name it
CalculatorService.svc
, then click "Add".
This will add two files to your project: CalculatorService.svc
and ICalculatorService.cs
.
Defining the Service Contract
Define the service contract in ICalculatorService.cs
:
using System.ServiceModel;
namespace MySoapService
{
[ServiceContract]
public interface ICalculatorService
{
[OperationContract]
int Add(int a, int b);
[OperationContract]
int Subtract(int a, int b);
}
}
Implementing the Service
Implement the service by modifying CalculatorService.svc.cs
:
using System;
namespace MySoapService
{
public class CalculatorService : ICalculatorService
{
public int Add(int a, int b)
{
return a + b;
}
public int Subtract(int a, int b)
{
return a - b;
}
}
}
Configuring the Service
Configure the service in web.config
:
<configuration>
<system.serviceModel>
<services>
<service name="MySoapService.CalculatorService">
<endpoint address="" binding="basicHttpBinding" contract="MySoapService.ICalculatorService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>
</configuration>
Running the Service
Run the service by pressing F5
or clicking the "Start" button in Visual Studio. The service will be hosted on http://localhost:port/CalculatorService.svc
.
Creating a SOAP Client
Now, create a new .NET Console App to consume the SOAP service:
- Open Visual Studio and click "Create a new project".
- Select "Console App (.NET Framework)" and click "Next".
- Set the name and location for your project and click "Create".
- Right-click on the project in Solution Explorer and select "Add" > "Service Reference".
- Enter the URL of the WSDL document for your service (e.g.,
http://localhost:port/CalculatorService.svc?wsdl
) and click "Go". - Set the namespace to
CalculatorServiceReference
and click "OK".
Consuming the SOAP Service
Modify the Program.cs
file to call the SOAP service:
using System;
using MySoapClient.CalculatorServiceReference;
namespace MySoapClient
{
class Program
{
static void Main(string[] args)
{
var client = new CalculatorServiceClient();
int resultAdd = client.Add(5, 3);
Console.WriteLine("5 + 3 = " + resultAdd);
int resultSubtract = client.Subtract(5, 3);
Console.WriteLine("5 - 3 = " + resultSubtract);
client.Close();
}
}
}
Running the Client
Run the client application by pressing F5
or clicking the "Start" button in Visual Studio. You should see the results from the SOAP service.
Conclusion
In this tutorial, we covered how to create and consume SOAP APIs with .NET. You learned how to define a WCF service, implement the service in .NET, and consume the service with a .NET client application. SOAP provides a robust protocol for exchanging structured information in web services, making it suitable for enterprise-level applications. Happy coding!