Christian Findlay
Christian Findlay

Reputation: 7660

Serve SOAP from ASP.NET Core web method

In classic ASP.NET, we would simply mark a method with the WebMethod attribute to create a SOAP service method that could be called from an external app. How do we achieve the same with ASP.NET Core?

It must be XML SOAP based. It must be compatible with a client that worked when ASP.NET class was the back end.

Upvotes: 6

Views: 12371

Answers (2)

CodingYoshi
CodingYoshi

Reputation: 26989

You can use the SOAPCore NuGet package to achieve that. Imagine you have a contract like below:

[ServiceContract]
public interface IPingService
{
    [OperationContract]
    string Ping(string msg);
}

And the implementation:

public class SampleService : IPingService
{
    public string Ping(string msg)
    {
        return string.Join(string.Empty, msg.Reverse());
    }
}

And then registration of the service:

public void ConfigureServices(IServiceCollection services)
{
     services.AddSingleton(new PingService());
     services.AddMvc();
     //rest goes here
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env,  ILoggerFactory loggerFactory)
{
    app.UseSoapEndpoint(path: "/PingService.svc", binding: new BasicHttpBinding());
    app.UseMvc();
    //rest goes here
}

Further Reading

Upvotes: 12

Chris Rosete
Chris Rosete

Reputation: 1258

Extending the Solution proposed by @CodingYoshi which is great.

When I install SoapCore i had to install all the dependencies first here is the list:

Install-Package Microsoft.Extensions.Primitives -Version 2.2.0 
Install-Package Microsoft.AspNetCore.Http.Features -Version 2.2.0 
Install-Package Microsoft.AspNetCore.Http.Abstractions -Version 2.2.0 
Install-Package Microsoft.Net.Http.Headers -Version 2.2.0 
Install-Package Microsoft.AspNetCore.WebUtilities -Version 2.2.0 
Install-Package Microsoft.Extensions.DependencyInjection.Abstractions -version 2.2.0 
Install-Package Microsoft.Extensions.ObjectPool -Version 2.2.0 
Install-Package Microsoft.Extensions.Options -Version 2.2.0 
Install-Package Microsoft.AspNetCore.Http -Version 2.2.2  
Install-Package Microsoft.Extensions.Logging.Abstractions -Version 2.2.0
Install-Package SoapCore -Version 0.9.9.5

Then on my Startup.cs I added this:

app.UseSoapEndpoint<IMyService>("/IMyService.svc", new BasicHttpBinding(), SoapSerializer.XmlSerializer);
app.UseSoapEndpoint<IMyService>("/IMyService.svc", new BasicHttpBinding(), SoapSerializer.XmlSerializer);
app.UseSoapEndpoint<IMyService>("/V3/IMyService.svc", new BasicHttpBinding(), SoapSerializer.XmlSerializer);

app.UseMvc();

Upvotes: 1

Related Questions