ReadAsDataContract <String []> - Unexpected end of file
I have a program that calls a rest service and returns an array of strings. When I try to read a string array, I get an Unexpected end of file exception. If I have a service, return a string containing only the first element of the array, it works. What am I missing in ReadAsDataContract<>()
causing this exception?
var returnElement = response.Content.ReadAsDataContract<string[]>();
The exception is XMLException: Unexpected end of file.
Stack trace -
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()
+3
source to share
1 answer
I solved it very simply that I still don't understand why it worked. I just did
string[] returnElement = response.Content.ReadAsDataContract<string[]>();
instead
var returnElement = response.Content.ReadAsDataContract<string[]>();
and it worked fine. If anyone can tell me why this worked, I will give them credit for answering this question.
0
source to share