Search code examples
.netwcfmimemalformed

WCF MIME message from embedded device is "Malformed" - why?


I am running a WCF Service on Windows 10 using IIS Express. I am sending messages from an embedded devices and forming the POST request and XML by hand in the embedded code. I have been able to send a simple "single part" message and get a successful response.

Now, I am trying to send a MIME multipart message. I was able to successfully do this with a Windows client, and I used Wireshark to capture the message text. I am trying to send the same text from the embedded device. It seems like this should work, however the server returns an error:

HTTP/1.1 400 Bad Request
Cache-Control: private
Server: Microsoft-IIS/10.0
MIME-Version: 1.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Sun, 14 Feb 2016 01:45:36 GMT
Connection: close
Content-Length: 0

I am able to enable WCF tracing and determine the cause is the message being considered "Malformed", as shown in the following message log record:

<MessageLogTraceRecord Time="2016-02-13T19:45:36.7215337-06:00" Source="Malformed" xmlns="http://schemas.microsoft.com/2004/06/ServiceModel/Management/MessageTrace"><![CDATA[--uuid:34450500-f156-44c4-9029-39b25c2794d5+id=2
Content-ID: <http://tempuri.org/0>
Content-Transfer-Encoding: 8bit
Content-Type: application/xop+xml;charset=utf-8;type="application/soap+xml"

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing"><s:Header><a:Action s:mustUnderstand="1">http://tempuri.org/IService1/StoreData</a:Action><a:MessageID>urn:uuid:c6cb5104-adfd-4040-a2eb-a2cb659e8f2a</a:MessageID><a:ReplyTo><a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address></a:ReplyTo><a:To s:mustUnderstand="1">http://192.168.0.10:8080/Service1.svc</a:To></s:Header><s:Body><StoreData xmlns="http://tempuri.org/"><device>22222</device><time>2016-01-10T14:34:54.2151851-06:00</time><data>AwIB</data></StoreData></s:Body></s:Envelope>
--uuid:34450500-f156-44c4-9029-39b25c2794d5+id=2--]]></MessageLogTraceRecord>

I dug in further and found the following stack trace in the svclog:

<Exception><ExceptionType>System.ServiceModel.CommunicationException, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Error creating a reader for the MTOM message</Message><StackTrace>   at System.ServiceModel.Channels.MtomMessageEncoder.MtomBufferedMessageData.TakeXmlReader()
at System.ServiceModel.Channels.BufferedMessageData.GetMessageReader()
at ...
at System.ServiceModel.Channels.MtomMessageEncoder.MtomBufferedMessageData.TakeXmlReader()
--- End of inner exception stack trace ---</ExceptionString><InnerException><ExceptionType>System.FormatException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Unexpected end of file.</Message><StackTrace>   at System.Xml.MimeReader.ReadNextPart()
at System.Xml.XmlMtomReader.ReadMimePart(String uri)
at ...
at System.ServiceModel.Channels.MtomMessageEncoder.MtomBufferedMessageData.TakeXmlReader()</ExceptionString></InnerException></Exception>

The text Unexpected end of file error stood out, so I thought maybe my length was not set correctly. It is computed, but I tried adding many extra \n with no help.

One thing I am not sure about is what type of line ends are expected. I assume only 0xA (LF) and not 0xD (CR), but I wonder if line ends are causing the count to be incorrect.

Any ideas for why this message is being rejected?


Solution

  • Yes, the problem has to do with line endings. The Windows WCF server expects CR+LF at the end of every line. My embedded client was only sending LF. Unfortunately, this is not obvious in the message dumps or the error messages. Compare the Wireshark binary capture to see the difference.