我有这段代码,它从数据库获取肥皂信封(varchar(max))并从中创建一个XmlDictionaryReader,目标是创建Message(System.ServiceModel.Channels.Message)。
这是代码
public static Stream GenerateStreamFromString(string s)
{
var stream = new MemoryStream(Encoding.UTF8.GetBytes(s));
return stream;
}
public static XmlDictionaryReader GenerateXmlDictionaryReader(string s, XmlDictionaryReaderQuotas quota)
{
using (var stream = GenerateStreamFromString(s))
{
return XmlDictionaryReader.CreateTextReader(stream, quota);
}
}
我在这里调用上面的方法
var reader = GenerateXmlDictionaryReader(soapenv, XmlDictionaryReaderQuotas.Max);
message = Message.CreateMessage(reader, int.MaxValue, MessageVersion.Soap11);
我收到此错误
System.NotSupportedException: Stream does not support reading.
at System.IO.__Error.ReadNotSupported()
at System.IO.BufferedStream.Read(Byte[] array, Int32 offset, Int32 count)
at System.Xml.EncodingStreamWrapper.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.Xml.XmlBufferReader.TryEnsureBytes(Int32 count)
at System.Xml.XmlUTF8TextReader.BufferElement()
at System.Xml.XmlUTF8TextReader.ReadStartElement()
at System.Xml.XmlUTF8TextReader.Read()
at System.Xml.XmlBaseReader.MoveToContent()
at System.ServiceModel.Channels.StreamedMessage..ctor(XmlDictionaryReader reader, Int32 maxSizeOfHeaders, MessageVersion desiredVersion)
at System.ServiceModel.Channels.Message.CreateMessage(XmlDictionaryReader envelopeReader, Int32 maxSizeOfHeaders, MessageVersion version)
我还验证了soapenv不为空并且代码中的xml是有效的。我不知道如何解决这个问题。请帮忙。
using (var stream = GenerateStreamFromString(s))
{
return XmlDictionaryReader.CreateTextReader(stream, quota);
}
当您退出
using
中的方法时,此 return
会处置(/关闭)流 - 您的读者位于死流之上;这就是它不起作用的原因。删除using
。