top of page
Writer's pictureThe Tech Platform

Create SOAP Web Service in DOTNET Core

In this article, I’m going to demonstrate development of SOAP WebService using .Net Core. We will utilize a NutGet package available for SoapCore. Pros of using this we will reuse features like filter and routing.


We will create an Empty Project you can use your preferred Visual Studio IDE. In this case I’m using dotnet cli to create an empty project using this command.

dotnet new web

Install SoapCore Nuget package with the following command

dotnet add package SoapCore

On successful installation of Nuget package, create a new Folder at the root of the project for Models.


Create a new File in the Models folder with the name as MachineModel.cs and add the below code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading.Tasks;namespace TempWSDL.Models
{   [DataContract]
    public class MachineModel
    {
        [DataMember]
        public int Id { get; set; }
        
       [DataMember]
        public string MachineName { get; set; }       

        [DataMember]
        public string HorsePower { get; set; }
    }
}

Create another folder Services as the name, inside this create an Interface IAccelerationService.cs and add the below code

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Threading.Tasks;namespace TempWSDL.Models
{
    [ServiceContract]
    public interface IAccelerationService
    {
        [OperationContract]
        string Speed(string s);        

        [OperationContract]
        void XmlMethod(System.Xml.Linq.XElement xml);
        [OperationContract]
        MachineModel TestMachine(MachineModel machine);    
    }
}

Create AccelerationService.cs class under the Services folder and add the below code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
using TempWSDL.Models;namespace TempWSDL.Services
{
    public class AccelerationService : IAccelerationService
    {
        public string Speed(string s)
        {
            Console.WriteLine(s+" MPH");
            return s;
        }        public MachineModel TestMachine(MachineModel machine)
        {
            return machine;
        }        

        public void XmlMethod(XElement xml)
        {             
                Console.WriteLine(xml.ToString());
        }
    }
}

Access your ‘Startup.cs’ file and add the below logic the ConfigureServices Method this will load the Interface and Service class as Singleton pattern

services.TryAddSingleton<IAccelerationService, AccelerationService>();

services.AddMvc();

Inside the Configure Method still under the ‘Startup.cs’ add the below code. The purpose of this is to expose the soap endpoint and instruct the application to route ‘/Service.asmx’

app.UseSoapEndpoint<IAccelerationService>("/Service.asmx", new BasicHttpBinding(), SoapSerializer.XmlSerializer);

The complete project layout

Project Layout


Test WSDL

Execute the application and access the development url https://localhost:44373/Service.asmx


WSDL metadata


Call Webservice

There are many tools to use such as Apache JMeter, Postman, Wizdler extension just to mention a few. In this case, I have used Wizdler chrome extension. Select Speed method

Select request type as Post and use the below payload. Then click Go button to submit

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <Speed xmlns="http://tempuri.org/">
            <s>160</s>
        </Speed>
    </Body>
</Envelope>

Response received is as below

Response



On the console output below was captured

Console Output


For the remaining SOAP web service methods you can match the request body params and call them as described on the latter steps.



Source: Medium - Duncan N.


The Tech Platform

0 comments

Comments


bottom of page