Introduction

No one has any doubt about the extensibility of Dotnet Core. That’s one of the reasons that right after the launch of Dotnet Core, developers stated moving from .Net to .Net Core, knowing that .Net Core is missing some of the great Features of .Net Framework.

Soap Web Services or WCF was one of that feature that was missing in .Net Core from his earlier release. It was one of the most requested & searched features of .Net Core Framework. So, after 3 months of Dotnet Core release, Mike from Microsoft wrote a blog post about implementing a middleware component for

handling SOAP requests & also provided a functional version of the blog’s sample code here: https://github.com/DigDes/SoapCore.

In this article, I’m going to Develop Soap Web Services using .Net Core. A NuGet package is also available for SoapCore. Using this we can take all the advantages of top features such as routing and filters.

Setting Up Project

Let’s start by creating an Empty Project.

I’m using dotnet cli to create my project using this command.

dotnet new web

Installing SoapCore

Now we need to install SoapCore from Nuget.

You can install it using Nuget Package Manager

Install-Package SoapCore

or using dotnet cli

dotnet add package SoapCore

Code

After successful installation of SoapCore, create a new Folder at the root of your Project for Models.

Inside this Models Folder, create a new File with the name as MyCustomModel.cs & add this Code.

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Models
{
    [DataContract]
    public class MyCustomModel
    {
       [DataMember]
       public int Id { get; set; }

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

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

Create another File Inside this Models Folder as ISampleService & add this Code.

using System.ServiceModel;
namespace Models
{
   [ServiceContract]
   public interface ISampleService
   {
      [OperationContract]
      string Test(string s);

      [OperationContract]
      void XmlMethod(System.Xml.Linq.XElement xml);

      [OperationContract]
      MyCustomModel TestCustomModel(MyCustomModel inputModel);
  }
}

Now create a new File at the root directory of your Project with the name SampleService.cs & Add this Code.

using Models;
using System;
using System.Xml.Linq;

public class SampleService : ISampleService
{
  public string Test(string s)
  {
    Console.WriteLine("Test Method Executed!");
    return s;
  }

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

  public MyCustomModel TestCustomModel(MyCustomModel customModel)
  {
    return customModel;
  }
}

In your Startup.cs File, Add the below code in your ConfigureServices Method.

services.TryAddSingleton<ISampleService, SampleService>();

& inside your Configure Method, Add this line of Code.

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

After that, my Startup.cs is looking like this.

using System.ServiceModel;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Models;
using SoapCore;

namespace DotNet_Core_Soap_Service_Example
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.TryAddSingleton<ISampleService, SampleService>();
            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
      app.UseSoapEndpoint<ISampleService>("/Service.asmx", new BasicHttpBinding(), SoapSerializer.XmlSerializer);
        }
    }
}

WSDL

Run your project & go to this Url => http://localhost:port/Service.asmx

You’ll see WSDL for your Soap Web Service. Like this.

WSDL
WSDL

Calling Web Service using Postman

Let’s Call the Test Method from our SampleService using Postman

Select the request type as Post & use this request Body

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <Test xmlns="http://tempuri.org/">
      <s>This is Test String</s>
    </Test>
  </soap:Body>
</soap:Envelope>

You can check the rest of the setting from this Image

dotnet soap request

You’ll get this Response

dotnet soap response

You can call the other Soap Web Services using the same way just by matching the request Body Parameters.

Thank you for reading. Please Comment to Let me know If you find any difficulty.

You can also download the complete source code of the above example from GitHub

Related Articles:

A Complete Guide to Secure your ASP.NET Web Application & API

Top Open Source ASP.NET Content Management System (CMS)