5

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.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Christian Findlay
  • 6,770
  • 5
  • 51
  • 103

2 Answers2

11

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

KyleMit
  • 30,350
  • 66
  • 462
  • 664
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
  • Hmmm.... This looks like WCF. I can already serve up WCF. Perhaps I'm missing something. Could I perhaps just serve up a WCF service with BassicHttpBinding? – Christian Findlay Nov 08 '17 at 04:03
  • Is it possible to get a complex deeply nested XML document as the output from an incoming request using SoapCore? For example, let say we only want a few elements out of a complex SOAP request and want the entire request body (no envelope) for parsing via Xpath. Can your function Ping receive a XElement of the SOAP body? I've been playing with SoapCore and it seems like you have to create a hierarchy of models that match the XML hierarchy. – mtpultz Nov 17 '20 at 23:11
  • @mtpultz I am not sure off the top of my head. Sorry. – CodingYoshi Nov 19 '20 at 18:01
  • Thanks @CodingYoshi I ended up adding a Tuner service and grabbing the raw request and parsing it using Xpath. It's clumsy since it have to parse out the soap envelope and body tags, but it works well enough and doesn't require deserializing into a set of models. Thanks for the response – mtpultz Nov 19 '20 at 21:38
1

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();
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Chris Rosete
  • 1,240
  • 15
  • 13