Error Returning Certain Types of Data

One of the errors we ran into was simple because we were not paying attention to the details of the code. The code compiled fine—we’re not sure whether it was a compiler issue or, as a software developer would say, it is a problem “as designed,” meaning that this is the way it is supposed to be. Take a look at the following error message and see if you can identify why we received this message:

System.Exception: There was an error generating the XML document. –-> System.Exception:
 The type System.Object[] may not be used in this context.. 
   at System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(String 
   name, String ns, Object o, Boolean xsiType) 
   atn2499d7d93ffa468fbd8861780677ee41.XmlSerializationWriter1.Write1_Object(String n,
 String ns, Object o, Boolean isNullable, Boolean needType) 
   atn2499d7d93ffa468fbd8861780677ee41.XmlSerializationWriter1.Write3_Object(Object o) 
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o,
 XmlSerializerNamespaces namespaces) 
   at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o) 
   at System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse response, Stream
 outputStream, Object returnValue) 
   at System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(Object[] returnValues,
 Stream outputStream) 
   at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues) 
   at System.Web.Services.Protocols.WebServiceHandler.Invoke() 
   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest() 

This message contains a lot of repetitive information here, but it also has some very specific bits that narrow down where the problem is coming from.You’ll notice that Object and Serialize are used several times. Now you know from the error message at the beginning that the problem originates with the System.Object class. Passing simple types back and forth is very simple to do. Now you are taking the next step to passing arrays in Web Services.When working with any sort of array, you must make sure to define your WebMethod as returning an array, not just a single value or object.

Listings 13.1813.21 give some examples of problems that you might run into when returning array’s of objects.

Listing 13.18. Code with Error (C#)
[WebMethod] 
public object ErrorStringCatch()// Missing [] from object 

Listing 13.19. Correct Syntax (C#)
[WebMethod] 
public object[] ErrorStringCatch() 

Listing 13.20. Code with Error (Visual Basic .NET)
<WebMethod()> Public Function ErrorStringCatch() As Object 

Listing 13.21. Correct Syntax (Visual Basic .NET)
<WebMethod()> Public Function ErrorStringCatch() As Object() 

This might seem like a trivial item, but don’t forget that most errors that you will run into probably are simple errors that you overlooked.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset