Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Solution to WSE839 error for WSE3 clients needing support both MTOM and non-MTOM responses

5.00/5 (3 votes)
6 Jun 2010CPOL 18.3K  
Sometimes you need a WSE3 client to send an MTOM-encoded request, but accept either an MTOM or non-MTOM response. While the WSE3 server classes support MTOM optional the client does not. Here's a quick solution to the problem.Assume you have generated your WSE3 web service proxy class....
Sometimes you need a WSE3 client to send an MTOM-encoded request, but accept either an MTOM or non-MTOM response. While the WSE3 server classes support "MTOM optional", the client does not. Here's a quick solution to the problem.

Assume you have generated your WSE3 web service proxy class. Normally you would set the RequireMtom property true on that object and make the call; but WSE3 then requires an MTOM-encoded response. With this trick, you can accept either.

Define a new class that inherits from your WSE3 proxy object with a single override method GetWebResponse:

 using System.Net;
class MyNewWebServiceProxy : MyWSE3GeneratedProxyClass
{
    protected override WebResponse GetWebResponse(WebRequest request)
    {
        WebResponse response = base.GetWebResponse(request);
        // if the response is non-MTOM like a SOAP fault, tell the proxy
        //   we don't really need MTOM
        if (response.Headers[HttpResponseHeader.ContentType].ToLower().StartsWith("text/xml"))
        {
            this.RequireMtom = false;
        }

        return response;
    }
}


Now instead of creating an instance of the WSE3 proxy, create an instance of this one and set the RequireMtom true before calling your MTOM web method.

The gotcha is you need to remember to reset the RequireMtom on the proxy back to true before making another call on the same proxy instance if the next method call also needs MTOM.

-- Darren DeLoach

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)