Paulo Morgado

.NET Development & Architecture

This Blog

Syndication

Search

Sponsored By

Tags

News

Unit Test Today! Get Typemock Isolator!

Books

 

Visitors

Visitor Locations

Community

Email Notifications

Archives

Profile

Disclaimer

The opinions and viewpoints expressed in this site are mine and do not necessarily reflect those of Microsoft, my employer or any community that I belong to. Any code or opinions are offered as is. Products or services mentioned are purchased by me, made available to me by my employer or the manufacturer/vendor which doesn't influence my opinion in any way.

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'll use

Paulo Morgado

# XML SOAP Service: Encoding-Probleme - .NET Windows Forms @ tutorials.de: Forum, Tutorial, Anleitung, Schulung & 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

# re: WCF: Text Message Encoding and ISO-8859-1 Encoding@ Friday, August 28, 2009 5:04 PM

Paul,

In the WSD Scan Spec, to retrieve a scan you send a standard utf-8 text message and recieve a mime type with enbedded MTOM in it. Since a basic wcf message can only send and recieve one type, could you use this to trap the content type mismatch? if so, without a custom message encoder can you use a IsContentTypeSupported overide and trap the mismatch?

Thanks in advance,

Eric

Eric Schlosser

# re: WCF: Text Message Encoding and ISO-8859-1 Encoding@ Saturday, August 29, 2009 6:27 AM

Hi Eric,

I'm not that good on WCF, but I think it could be done.

IsContentTypeSupported is a member of the MessageEncoder class. So you need one to override it.

Paulo Morgado

# re: WCF: Text Message Encoding and ISO-8859-1 Encoding@ Wednesday, October 28, 2009 6:44 PM

I got this error too when using .NET 3.5/VS 2008.  I added a "Service Reference" and imported the WSDL for a site driven by PHP.  It built fine, but got the ISO-8859-1 encoding error.

I tried again after changing the project properties to uses .NET 2.0.  This time, I was not offered the option of adding a "Service Reference", but I could add a "Web Reference".  When I did that, everything worked fine.

Finally, I tried using .NET 3.5 again.  I used "Add Service Reference" again, but this time I hit the "Advanced..." button.  The next dialog box had a compatibility option where a button allowed you to add a "Web Reference" based on .NET 2.0 Web Services.  This worked the same as when using .NET 2.0, but the rest of your app can use .NET 3.5.

There must be advantages in using the "Service Reference", but until WCF supports ISO-8859-1, using a "Web Reference" beats having to override IsContentSupported.

Jim Rolph

# re: WCF: Text Message Encoding and ISO-8859-1 Encoding@ Friday, October 30, 2009 4:25 AM

I agree that WCF should support other encodings, but if you need something more than just calling a web service (security, other bindings (TCP, Pipes, etc.), logging, tracing, etc.) nothing beats WCF.

Paulo Morgado

Leave a Comment

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