Paulo Morgado

.NET Development & Architecture

Test Content

<p>Test</p>

This Blog

Syndication

Search

Sponsored By

Tags

Visitor Locations

<p><a href="http://www4.clustrmaps.com/counter/maps.php?url=http://PauloMorgado.NET/Blogs/EN" title="Visitor Locations"><img src="http://www4.clustrmaps.com/counter/index2.php?url=http://PauloMorgado.NET/Blogs/EN" alt="Visitor Locations" /></a></p>

News

Unit Test Today! Get Typemock Isolator!

Visitor Locations

Community

Email Notifications

Archives

WCF: Text Message Encoding and ISO-8859-1 Encoding

I'm a newbie in WFC and, so far, only have done client code to call POX web services.

I've been using a textMessageEncoding binding extension with a message version of None and a write encoding of utf-8 and all has been running fine.

Well, until I needed to call a service in iso-8859-1 encoding. Then I started getting a ProtocolException:

The content type text/xml;charset=iso-8859-1 of the response message does not match the content type of the binding (text/xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly.

Bruno directed me to a sample that looked promising. The sample works fine because it's using the same binding extension in the server and in the client.

When I tried to use the customTextMessageBinding from the sample, I got this nice ProtocolException:

The content type text/xml;charset=iso-8859-1 of the response message does not match the content type of the binding (text/xml; charset=iso-8859-1). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly.

Looks kind of funny, doesn't it?

It comes down to the fact that the base MessageEncoder class doing a string comparison on the content type.

To solve this, I've overriden the IsContentTypeSupported method and added an additional validadion when the base validation fails. This new validation only checks for the media type of the response and lets the XmlReader handle the encoding.

public override bool IsContentTypeSupported(string contentType)

{

    if (base.IsContentTypeSupported(contentType))

    {

        return true;

    }

    if (contentType.Length == this.MediaType.Length)

    {

        return contentType.Equals(this.MediaType, StringComparison.OrdinalIgnoreCase);

    }

    else

    {

        if (contentType.StartsWith(this.MediaType, StringComparison.OrdinalIgnoreCase)

            && (contentType[this.MediaType.Length] == ';'))

        {

            return true;

        }

    }

    return false;

}

Since this seems to be happening in Orcas too, I've added a comment to the sample documentation and opened an issue. Vote on them.

Published Wed, Apr 25 2007 21:34 by Paulo Morgado

Comments

# WCF: Text Message Encoding and ISO-8859-1 Encoding@ Wednesday, April 25, 2007 4:23 PM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetKicks.com

# Optimizing reading for the CustomTextMessageEncoder@ Monday, May 07, 2007 3:45 PM

Continuing the improvement of the CustomTextMessageEncoder (see this and this ), this time I&#39;ll use

Paulo Morgado

# XML SOAP Service: Encoding-Probleme - .NET Windows Forms @ tutorials.de: Forum, Tutorial, Anleitung, Schulung &amp; Hilfe@ Friday, November 16, 2007 8:01 AM

Pingback from  XML SOAP Service: Encoding-Probleme - .NET Windows Forms @ tutorials.de: Forum, Tutorial, Anleitung, Schulung & Hilfe

XML SOAP Service: Encoding-Probleme - .NET Windows Forms @ tutorials.de: Forum, Tutorial, Anleitung, Schulung & Hilfe

# re: WCF: Text Message Encoding and ISO-8859-1 Encoding@ Friday, February 08, 2008 1:23 AM

where do we override this method, in the application. is it in the client side or in the service side.

swapneel

# re: WCF: Text Message Encoding and ISO-8859-1 Encoding@ Friday, February 08, 2008 7:05 PM

It can be on either side. Just put it where you need it to be.

Paulo Morgado

# re: WCF: Text Message Encoding and ISO-8859-1 Encoding@ Monday, June 09, 2008 5:49 PM

So, this all makes sense to me, but for one thing:  How do you configure your app to use your custom encoder containing the override?

Adam

# re: WCF: Text Message Encoding and ISO-8859-1 Encoding@ Tuesday, June 10, 2008 11:26 AM

See this article:

msdn.microsoft.com/.../ms751486.aspx

Paulo Morgado

# re: WCF: Text Message Encoding and ISO-8859-1 Encoding@ Wednesday, August 06, 2008 4:36 AM

Read the article, but still don't understand how easy I can implement this message encoder while calling such a webservice. Anyone any help?

Erik

# re: WCF: Text Message Encoding and ISO-8859-1 Encoding@ Wednesday, August 06, 2008 6:05 AM

Can you elaborate a bit more on where you need help here?

Paulo Morgado

# re: WCF: Text Message Encoding and ISO-8859-1 Encoding@ Wednesday, August 06, 2008 6:32 AM

It's probably since I'm not yet familiar enough with .Net/WCF ;-)

But if I look at the artice I would have to write code for a custom MessageEncoder (where the IsContentTyepSupported is overridden), a custom

MessageEncoderFactory and a custom TextMessageEncodingBindingElement?

And I still cannot see where the custom MessageEncoder is actually hooked in when the call to the webservice is made.

Erik

# re: WCF: Text Message Encoding and ISO-8859-1 Encoding@ Wednesday, August 06, 2008 6:39 PM

You have to do all that in order to WCF use your message encoder.

The message encoder will be called in by WCF while WCF runs the request/response through its pipeline.

Paulo Morgado

# re: WCF: Text Message Encoding and ISO-8859-1 Encoding@ Thursday, August 07, 2008 8:40 AM

Thought WCF was there to make our lifes easier ;-)

For the moment fixed the problem at the other side: changed the PHP webservice so it returns UTF-8 and now it works.

I'll try to look at writing my own custom message encoder later.

Thanx for your reponse, made things clearer for me!

Erik

# re: WCF: Text Message Encoding and ISO-8859-1 Encoding@ Thursday, August 07, 2008 9:45 AM

You are better of that way, but, at the time, I wasn't allowed that luxury.

Based on the sample, I wrote my onw message encoder and registered it and it without any change in both caller and called applications.

<?xml version="1.0"?>
<configuration>
 <system.serviceModel>
   <bindings>
     <customBinding>
       <binding name="MyCustomBinding">
         <customTextMessageEncoding messageVersion="None" writeEncoding="ISO-8859-1" mediaType="text/xml" />
         <httpTransport manualAddressing="false" keepAliveEnabled="false" />
       </binding>
     </customBinding>
   </bindings>
   <client>
     <endpoint name="MyServiceEndpoint"
         address=http://myserver/MyPoxEndpoint
                 contract="IMyService"
                 binding="customBinding"
                 bindingConfiguration="MyCustomBinding" />
   </client>
 </system.serviceModel>
</configuration>

Paulo Morgado

# SOAP and DotNet 3.0 / 3.5 &laquo; Shaw Innes@ Wednesday, April 08, 2009 10:22 PM

Pingback from  SOAP and DotNet 3.0 / 3.5 «  Shaw Innes

SOAP and DotNet 3.0 / 3.5 « Shaw Innes

Leave a Comment

(required) 
(required) 
(optional)
(required)