WCF REST - 如何将流读取为文本
我有一个 WCF REST 服务。每个传入消息的 XML 正文都被反序列化为我的对象,如下所示:
private static Message MyMethod(Stream stream)
{
try
{
var serializer = new XmlSerializer(typeof(MyObject));
var myObject = (MyObject)serializer.Deserialize(stream);
//do stuff
}
catch (InvalidOperationException invEx)
{
//write stream (xml) to error log
}
//etc
}
我希望能够在反序列化失败时写入要记录的 XML。我尝试过的所有结果都是空字符串。这可能吗?
谢谢!
I have a WCF REST Service. The XML body of each incoming message is deserialized into my objects as follows:
private static Message MyMethod(Stream stream)
{
try
{
var serializer = new XmlSerializer(typeof(MyObject));
var myObject = (MyObject)serializer.Deserialize(stream);
//do stuff
}
catch (InvalidOperationException invEx)
{
//write stream (xml) to error log
}
//etc
}
I would like to be able to write the XML to log when the Deserialization fails. Everything I have tried results in an Empty String. Is this even possible?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以轻松地将整个内容作为
string
而不是Stream
引入,并从中加载/反序列化。Stream
有什么特殊原因吗?或者(更好,IMO),您可以将要反序列化的对象指定为
DataContract
并在您的操作合约中要求 XML,并让 WCF 框架完成工作你。You could easily bring the whole thing in as a
string
rather than aStream
, and load/deserialize from that. Is there a particular reason for theStream
?Alternatively (better, IMO), you can specify the object you want de-serialized as a
DataContract
and require XML in your operation contract and let the WCF framework do the work for you.您是否尝试在写入错误日志之前重新定位流?
流.Position = 0;
Have you tried to reposition the stream before writing to the error log?
stream.Position = 0;