none
WCF and encoding. Fails on german characters RRS feed

  • Question

  • This is the line that fails when passed into my wcf service (I've removed and swapped letters as this was an actual customers address, but left the offending characters in place).

    <StreetAddress>TU Dasdfasdn, asdfa für adsf, htras-Bähr-trf. 1</StreetAddress>

    Note the ü and ä characters. If I remove these characters from the address and send the XML to the service, it goes through fine.

    I found one solution which works and involves recreating the incoming message as shown below:

    Code Before

    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
       MEssageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue); // FAILS HERE!!!
       var requestCopy = buffer.CreateMessage();       
       ... do stuff ....
       return null;
    }

    Code With Fix

    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        var dictionaryReader = request.GetReaderAtBodyContents();
        var outerXml = dictionaryReader.ReadOuterXml();
        var parsedOuterXml = XElement.Parse(outerXml);
        var replacedRequest = Message.CreateMessage(request.Version, request.Headers.Action, parsedOuterXml);
        replacedRequest.Headers.CopyHeadersFrom(request.Headers);
        replacedRequest.Properties.CopyProperties(request.Properties);
        request = replacedRequest;
    
    
       MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue); // NO LONGER FAILS HERE
       var requestCopy = buffer.CreateMessage();
        ..... do stuff .......
        return null
    }

    What doesn't make sense is that the address in the XML after the fix is EXACTLY the same, including the ü and ä characters. But this time my code runs fine and there are no errors.

    The only thing I can think is that the call to XElement.Parse somehow fixes any encoding issues whereas CreateBufferedCopy won't, but thats just a guess.

    Is there a 'proper' way to fix this? Has anyone come across this before? The 'fix' I found doesn't actually do anything as the XML before and after is the same (don't know why it works) and I would rather a more elegant solution, or at least an explanation of what is happening.

    Exception thrown is "Unexpected end of file. Following elements are not closed: Envelope. Line 105, position 3."



    • Edited by GarethB83 Wednesday, July 10, 2013 12:11 AM code fix
    Tuesday, July 9, 2013 11:44 PM

Answers

All replies