我有一个调用休息服务的程序并返回一个字符串数组。当我尝试读取字符串数组时,我得到一个异常意外的文件结尾。如果我有服务返回一个字符串,只包含它工作的数组的第一项。关于导致此异常的ReadAsDataContract<>()
我错过了什么?
var returnElement = response.Content.ReadAsDataContract<string[]>();
异常是XMLException:意外的文件结束。
堆栈追踪 -
at System.Xml.EncodingStreamWrapper.ReadBOMEncoding(Boolean notOutOfBand)
at System.Xml.EncodingStreamWrapper..ctor(Stream stream, Encoding encoding)
at System.Xml.XmlUTF8TextReader.SetInput(Stream stream, Encoding encoding, XmlDictionaryReaderQuotas quotas, OnXmlDictionaryReaderClose onClose)
at System.Runtime.Serialization.XmlObjectSerializer.ReadObject(Stream stream)
at System.Runtime.Serialization.DataContractContentExtensions.ReadAsDataContract[T](HttpContent content, DataContractSerializer serializer)
at System.Runtime.Serialization.DataContractContentExtensions.ReadAsDataContract[T](HttpContent content)
at RestConsumption.Program.GetDevices() in **********\documents\visual studio 2010\Projects\RestPractice\RestConsumption\Program.cs:line 55
at RestConsumption.Program.Main(String[] args) in *********\documents\visual studio 2010\Projects\RestPractice\RestConsumption\Program.cs:line 26
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
答案 0 :(得分:0)
我以一种非常简单的方式解决了这个问题,但我仍然不明白为什么会这样。我只是做了
string[] returnElement = response.Content.ReadAsDataContract<string[]>();
而不是
var returnElement = response.Content.ReadAsDataContract<string[]>();
它工作正常。如果有人告诉我为什么这有效,我会给他们回答这个问题。