Calling legacy ASMX services from .NET Core

Posted on : May 14 2024 by andreihetel
BlazorWeb Services

It's been a while since I didn't write or consumed the good old .NET Framework ASMX web services. The technology is most likely rarely used today; anyway, I had a requirement to utilize a couple of methods from inside a Blazor server application I'm working on. Because I cannot disclose too many details about the task, I will do a quick example project and publish the sources on GitHub. ASMX from .NET Core (.NET 8).

I searched for a simple and free ASMX web service to accelerate development. The calculator web service is perfect for the current exercise.

So, let's get to work. To make it simpler, from a Blazor Web App standard template, I removed the client web assembly project. Firstly, add a web reference to the service, like in the image below. Add web service reference

Press 'next' and 'finish' in the following two screens and accept default settings. At the end, see the newly created referenceĀ in the solution explorer pane. Connected services in solution explorer

We need two more things: a class (APIHelper.cs) and a razor component (TestCalls.razor). For simplicity, I will keep the class responsible for the API calls in the same project. Usually, in a production environment, it is placed in its class library (reusability in another type of project like Windows Form or WPF).

Except for the two files mentioned above, everything else is Blazor boilerplate. Happy coding!

                    public class APIHelper(string baseURL)
						{
							private readonly EndpointAddress? _endpointAddress = new(baseURL);

							public async Task AddNumbers(int A, int B )
							{        
								using WSCalculator.CalculatorSoapClient client = new(CalculatorSoapClient.EndpointConfiguration.CalculatorSoap, _endpointAddress);
								return await client.AddAsync(A,B);
							}
						}