这是传递给我的wcf服务时失败的行(我删除并交换了字母,因为这是一个实际的客户地址,但留下了有问题的字符)。
<StreetAddress>TU Dasdfasdn, asdfa für adsf, htras-Bähr-trf. 1</StreetAddress>
注意ü和ä字符。如果我从地址中删除这些字符并将XML发送到服务,它会很顺利。
我找到了一个有效的解决方案,涉及重新创建传入的消息,如下所示:
以前的代码
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
buffer = request.CreateBufferedCopy(Int32.MaxValue); // FAILS HERE!!!
var requestCopy = buffer.CreateMessage();
... do stuff ....
return null;
}
代码已修复
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;
buffer = request.CreateBufferedCopy(Int32.MaxValue); // NO LONGER FAILS HERE
var requestCopy = buffer.CreateMessage();
..... do stuff .......
return null
}
没有意义的是修复后的XML中的地址完全相同,包括ü和ä字符。但是这次我的代码运行正常且没有错误。
我唯一能想到的是对XElement.Parse的调用以某种方式解决了任何编码问题,而CreateBufferedCopy不会,但这只是一个猜测。
有没有'正确'的方法来解决这个问题?有没有人遇到过这个?我找到的'修复'实际上并没有做任何事情,因为之前和之后的XML是相同的(不知道它为什么会起作用),我宁愿更优雅的解决方案,或者至少解释正在发生的事情。< / p>
编辑:抛出的异常是“意外的文件结束。以下元素未关闭:信封。第105行,第3位。”
at System.Xml.XmlExceptionHelper.ThrowXmlException(XmlDictionaryReader reader, String res, String arg1, String arg2, String arg3)
at System.Xml.XmlBufferReader.EnsureBytes(Int32 count)
at System.Xml.XmlBufferReader.GetBuffer(Int32 count, Int32& offset)
at System.Xml.XmlUTF8TextReader.ReadEndElement()
at System.Xml.XmlUTF8TextReader.Read()
at System.Xml.XmlBaseReader.MoveToContent()
at System.Xml.XmlBaseReader.ReadEndElement()
at System.ServiceModel.Channels.Message.ReadFromBodyContentsToEnd(XmlDictionaryReader reader, EnvelopeVersion envelopeVersion)
at System.ServiceModel.Channels.ReceivedMessage.OnWriteBodyContents(XmlDictionaryWriter writer)
at System.ServiceModel.Channels.Message.OnWriteMessage(XmlDictionaryWriter writer)
at System.ServiceModel.Channels.Message.OnCreateBufferedCopy(Int32 maxBufferSize, XmlDictionaryReaderQuotas quotas)
at System.ServiceModel.Channels.Message.CreateBufferedCopy(Int32 maxBufferSize)
at Stella.ServiceLibrary.Security.MessageLogInspector.AfterReceiveRequest(Message& request, IClientChannel channel, InstanceContext instanceContext) in C:\Projects\SIHOT_CRES\WebServiceProjects\Stella.ServiceLibrary\Security\MessageLogInspector.cs:line 94
答案 0 :(得分:0)
问题似乎是我编码的字符串本身就是utf-16。即使我正在使用
byte[] content = Encoding.UTF8.GetBytes(myXDocument.ToString());
不知何故,当它到达我的服务时,它仍然是utf-16。相反,我必须使用带有内存流的XmlWriter并返回如下
XmlWriterSettings writerSettings = new XmlWriterSettings {Encoding = Encoding.UTF8};
MemoryStream buffer = new MemoryStream();
using (XmlWriter writer = XmlWriter.Create(buffer, writerSettings))
{
myXDocument.WriteTo(writer);
writer.Flush();
writer.Close();
result = buffer.ToArray();
}
return result;
结果现在是一个byte []编码为utf-8,现在可以使用我的服务,期望utf-8