[ ASP .NET Core, SOAP ]
SOAP (Simple Object Access Protocol) is a messaging protocol used in web services to exchange data between applications. In this article, we will explore the use of a connected SOAP service in a software development project. We will examine a sample app that uses a SOAP service to register a new customer and then sign in a user.
The code is written in C# and consists of two controllers: RegisterController and SignInController. These controllers. Each controller has corresponding views to register and sign in.
Before consuming the ICUTechService SOAP service it first must be represented by classes in our code, VS has a convenient way to scaffold those for us. Here are the steps:
- Right-click on the project in Solution Explorer, and select “Add” -> “Connected Service”.
- In the “Add Connected Service” dialog box, select “Microsoft WCF Web Service Reference Provider” and click on “Configure”.
- In the “Configure WCF Web Service Reference” dialog box, enter the URL of the ICUTechService WSDL file in the “Service URL” field and click on “Go”. This will load the WSDL and generate a preview of the service reference.
- In the “Configure WCF Web Service Reference” dialog box, you can specify the namespace for the service reference and the name of the service reference. You can also choose to enable or disable “Reuse types in referenced assemblies”.
- Click on “OK” to generate the service reference.
After that the wizard will create for us the ICUTechServiceReference class, which in turn has ICUTechClient() method, by calling that method we aquire access to the public methods of the service, the rest is easy:
public class RegisterController : Controller
{
[HttpPost]
public async Task<IActionResult> Index(string email, string password, string firstName, string lastName, string mobile)
{
ViewBag.IsDeveloperMode = true;
var client = new ICUTechClient();
var rawResponse = await client.RegisterNewCustomerAsync(email, password, firstName, lastName, mobile, 0, 0, "");
ViewBag.ResponseData = JsonConvert.DeserializeObject(rawResponse.@return);
return View();
}
public IActionResult Index()
{
return View();
}
}
It has two methods, Index and Index(HttpPost). The Index method returns a view for displaying the registration form to the user. The Index(HttpPost) method is called when the user submits the registration form. The method creates an instance of the ICUTechClient class and calls the RegisterNewCustomerAsync method to register a new customer with the ICUTechServiceReference service. The method then deserializes the raw response from the service using the Newtonsoft.Json library and sets the ViewBag.ResponseData property with the deserialized data.
public class SignInController : Controller
{
[HttpPost]
public async Task<IActionResult> Index(string username, string password)
{
ViewBag.IsDeveloperMode = false;
var client = new ICUTechServiceReference.ICUTechClient();
var rawResponse = await client.LoginAsync(username, password, "");
ViewBag.ResponseData = JsonConvert.DeserializeObject(rawResponse.@return);
return View();
}
public IActionResult Index()
{
return View();
}
}
The SignInController class is similar to the RegisterController class, but instead of registering a new customer, it signs in an existing user. The class has two methods, Index and Index(HttpPost). The Index method returns a view for displaying the sign-in form to the user. The Index(HttpPost) method is called when the user submits the sign-in form. The method creates an instance of the ICUTechClient class and calls the LoginAsync method to sign in the user with the ICUTechServiceReference service. The method then deserializes the raw response from the service using the Newtonsoft.Json library and sets the ViewBag.ResponseData property with the deserialized data.
Considering the simplicity of our business logic, this is basically it, add an HTML wrapper and you are good to go! Using SOAP services can be a powerful tool in software development, allowing you to easily exchange data between applications. With a little bit of knowledge and some basic programming skills, you can leverage the power of SOAP to build robust and scalable applications that meet the needs of your users.
Check out the complete code in my GitHub repo:
https://github.com/movsar/mk-task
Leave a reply