Andection
Andection

Reputation: 351

How to implement SOAP service on WebAPI

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.

Upvotes: 2

Views: 27748

Answers (2)

Michael Dennis
Michael Dennis

Reputation: 405

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.

Upvotes: 3

Jaime Mendes
Jaime Mendes

Reputation: 643

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();
   }
}

Upvotes: 3

Related Questions