In this article, we are going to learn how to consume the ASMX service in ASP.NET Core which we use to develop in old asp.net webforms days.
Nowadays we are using Web API everywhere whether it is web, windows or mobile but if you have any legacy application which has ASMX service which you need to use in the newly build asp.net core then you are at the right place. Here we are going to how to do that.

Table of Contents
- Creating Project
- Sample ASMX Service
- Adding Service Reference
- Consuming Calculator Service
- Debug Mode Snapshot
Useful Links
How to Upload Files and Save in Database in ASP.NET Core MVC
https://tutexchange.com/how-to-upload-files-and-save-in-database-in-asp-net-core-mvc/
File upload in ASP.NET Core MVC (Storing in Folder)
https://tutexchange.com/file-upload-in-asp-net-core-mvc-storing-in-folder/
Uploading Multiple Files in ASP.NET Core MVC (Storing in a Folder)
https://tutexchange.com/uploading-multiple-files-in-asp-net-core-mvc-storing-in-folder/
Creating Project
We are going to create a new application, as shown below.

Next, we are going to set Project Name WebConsumeDemo and location.
In the last part, we will choose the .Net Core framework and ASP.NET Core Version 6.0 as the framework for the application and a few advanced settings such as configuring HTTPS and enabling docker; we are not going to enable docker settings for this project.

Now finally, click on create button to create a project.

After creating the project next, we will view the ASMX service which we will consume in SOAPUI.
Sample ASMX Service
We are going to consume open-source API which is a basic calculator.
http://www.dneonline.com/calculator.asmx

The above service has 4 methods in it which we are going to consume for doing that in visual studio we need to add this service as a reference which we are going to see in the next step.
Adding Service Reference
For consuming the ASMX service we need to add it as a reference. For adding a reference, we just right-click on your web project “WebConsumeDemo” and then select Add inside that select Service Reference.

After clicking on Service Reference, a new dialogue will pop up inside that selects WCF Web Service.

After clicking on WCF Web Service, Click on the Next button a new dialogue will pop up. In this Dialog add URI “http://www.dneonline.com/calculator.asmx” and click on the “go” button along with that we are going to set the namespace as CalculatorService. Click on the next button.

After clicking on next a new dialogue will pop up in this, we will specify the datatype.

After choosing your custom options of data type next we are going to select the access level of the class that it is going to generate.
Here we are going to choose the public as the access level.

After choosing your Access level next, we are going to click on the Finish button.
After that, it will start its configuration process and successfully it will add service reference.

After the Service is added successfully you will see the newly added Connected Services Folder which contains the CalculatorService folder (this is the namespace name which we provided during configuration).
Now the main class to use in this bundle of the class is “CalculatorSoapClient”.

Let access CalculatorSoapClient class in Home Controller.
Consuming Calculator .asmx Service
Default Home Controller Which is added when we create the project. In this controller, we are going to access the CalculatorSoapClient service.

Home Controller Code Snippet
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using WebConsumeDemo.Models;
namespace WebConsumeDemo.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
return View();
}
public IActionResult Privacy()
{
return View();
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}
We are going to consume the ASMX service in the Index action method in an async way for that we are going add async and Task to IActionResult.
public async Task<IActionResult> Index()
{
return View();
}

Next to consume the service we need to create an object of CalculatorSoapClient class and pass Endpoint Configuration to it.
Now using the object of CalculatorSoapClient class we can access methods inside it like (Add, Subtract, Multiply, and Divide) as shown below.
public async Task<IActionResult> Index()
{
CalculatorSoapClient CalculatorSoapClient =
new CalculatorSoapClient(CalculatorSoapClient.EndpointConfiguration.CalculatorSoap);
var add = await CalculatorSoapClient.AddAsync(10, 50);
var subtract = await CalculatorSoapClient.SubtractAsync(100, 50);
var multiply = await CalculatorSoapClient.MultiplyAsync(10, 50);
var divide = await CalculatorSoapClient.DivideAsync(100, 10);
return View();
}
Debug Mode Snapshot

Conclusion
Wow, finally we are at the end of this article where we have learned how to consume ASMX services in ASP.NET Core in simple steps.
Hope you Liked this Article Please share it if possible.
References
https://www.facebook.com/Tutexchange/