How to implement SOAP service on WebAPI

24,198

Solution 1

I think this is already answered here ASP.NET WebAPI + Soap

If what you are asking for is how to create REST wrappers that call into the SOAP implementations then library's like ServiceStack do this for you but if you want to do it yourself with WebApi it's pretty easy. Just make a separate project that has your SOAP service references in it wrapped in some sort of abstraction and then reference that in your WebApi project and call into it from your REST endpoints.

If what you are asking is how to host the SOAP interfaces in WebApi I think you are just making more work for yourself. Use the WCF scaffolding that MS has provided. WebApi for REST services, WCF for SOAP.

Solution 2

I wouldn't recommend to mix the technologies. Have one project for SOAP Apis and another one for the WebApi, sharing the same logic.

You have then one url for soap, the other one to webapi.

Edit: I wouldn't do the SOAP Parser at all. That was the power of WCF and would keep on using it.

Since proxing is not an option (Which could be done in web.config and easily deployed), I would create a WebAPI endpoint which would redirect to SOAP API.

[HttpGet]
public IHttpActionResult Service1()
{
   return Redirect("http://service.com/soap/services/service1");
}

Later, when migrating the logic, use the service itself.

[HttpGet]
public IHttpActionResult Service1()
{
   var result = new ServiceLogin1().Execute();
   if(result == null)
   {
          return StatusCode(HttpStatusCode.NoContent);
   }
   else
   {
          return Ok();
   }
}
Share:
24,198
Andection
Author by

Andection

Updated on July 20, 2022

Comments

  • Andection
    Andection almost 2 years

    We have a server which has several types of api (custom XML API based on httplistener, SOAP API based on WCF and REST API based on WEB API). We want to move all API's to WEB API (there are many reasons) and it should be backward compatible.

    One of the reason to support url structure: services/service1. services/service2. And in this case it should be on one port. It is intranet application which is distributed to multiple customers and it should be easy to deploy, install. So, we can not have a long configuration on customer side (proxing and otherts).

    Are there easy way for implementation SOAP service on web api? At first look should be easy way to parse httprequest to typed soap envelope (based on existed contract) and serialize a answer. Of course, there many actions and data types in contract.

    PS: I do not want to look into servicestack:)

    Update:

    The problem I described above can be fixed by proxing http request to soap service (It can work only with basichttpbinding without security. If WCF service require NTLM authentication it won't work):

    [HttpPost]
    public async Task<IHttpActionResult> SoapAction()
    {
        var httpClient = new HttpClient();
    
        var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "http://localhost:8111/soap")
        {
            Content = this.Request.Content
        };
    
        foreach (var header in this.Request.Headers)
        {
            httpRequestMessage.Headers.Add(header.Key, header.Value);
        }
    
        var responseMessage= await httpClient.SendAsync(httpRequestMessage).ConfigureAwait(false);
    
        return ResponseMessage(responseMessage);
    }
    

    But I still want to know are there any SOAP parser in C# because my server supports NTLM authentication.

  • Andection
    Andection over 7 years
    We want to have same url structure: services/service1. services/service2. And in this case it should be on one port. It is intranet application which is distributed to multiple customers and it should be easy to deploy, install. So, we can not have a long configuration on customer side (proxing and otherts). We can create a inside proxy, of course. But normal soap parser looks better. PS: I can not edit that comment.
  • Jaime Mendes
    Jaime Mendes over 7 years
    @Andection Update the answer. I'm really not fan of the parsers. For me, it's twice the work and trouble.
  • Andection
    Andection over 7 years
    Thank you for redirect suggestion. But looks like it does not work. client receives Bad Request: System.ServiceModel.ProtocolException: The remote server returned an unexpected response: (400) Bad Request. ---> System.Net.WebException: The remote server returned an error: (400) Bad Request. at System.Net.HttpWebRequest.GetResponse() at System.ServiceModel.Channels.HttpChannelFactory`1.HttpReques‌​tChannel.HttpChannel‌​Request.WaitForReply‌​(TimeSpan timeout)
  • Andection
    Andection over 7 years
    First option (I udated it in my question) is not suit to me. I should support NTLM authentication which require direct TCP/IP connection.