0

我正在使用 IIS Express 在 Windows 10 上运行 WCF 服务。我正在从嵌入式设备发送消息,并在嵌入式代码中手动形成 POST 请求和 XML。我已经能够发送一个简单的“单部分”消息并获得成功的响应。

现在,我正在尝试发送 MIME 多部分消息。我能够使用 Windows 客户端成功地做到这一点,并且我使用 Wireshark 来捕获消息文本。我正在尝试从嵌入式设备发送相同的文本。看起来这应该可以工作,但是服务器返回一个错误:

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

我能够启用 WCF 跟踪并确定原因是消息被视为“格式错误”,如以下消息日志记录所示:

<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>

我进一步挖掘并在 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>

Unexpected end of file error文本很突出,所以我想也许我的长度设置不正确。它是计算出来的,但我尝试在没有帮助的情况下添加许多额外的 \n。

我不确定的一件事是预期的线端类型。我假设只有 0xA (LF) 而不是 0xD (CR),但我想知道行尾是否导致计数不正确。

关于为什么此消息被拒绝的任何想法?

4

1 回答 1

2

是的,问题与行尾有关。Windows WCF 服务器在每一行的末尾都需要 CR+LF。我的嵌入式客户端只发送 LF。不幸的是,这在消息转储或错误消息中并不明显。比较 Wireshark二进制捕获以查看差异。

于 2016-02-15T16:19:35.507 回答