locked
System.InvalidOperationException: The type System.Object[] may not be used in this context. RRS feed

  • Question

  • User1112784522 posted

    I've got a webservice serialization issue that is causing quite a headache and am hoping to get a little assistance before I end up pulling my hair out or something equally drastic.

    I've got several webmethods that all return an object array that contains another object array. All but one work fine.

    An example of the webmethod that works is (de-cluttered for brevity):


    <WebMethod()> _
        Public Function WDGetDocumentInfoObject() As Object
    
            Dim objarrdocument() As Object = Nothing
    
            Try
                document = GetDocument(pDocumentKey)
    
                ReDim objarrdocument(0)
    
                Dim docstuff As Object = New Object() {document.DocumentKey, document.DocumentTitle, document.UserDefined1, document.UserDefined2, document.UserDefined3, document.UserDefined4,  document.UserDefined5}
    
                objarrdocument(0) = docstuff
            Catch ex As Exception
                Me.LastError = ex.ToString
                objarrdocument = Nothing
            End Try
            Return objarrdocument
        End Function
    


    This returns the following XML (shortened to one result):


    <?xml version="1.0" encoding="utf-8"?>
    <anyType d1p1:type="ArrayOfAnyType" xmlns:d1p1="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org">
      <anyType d1p1:type="ArrayOfAnyType">
        <anyType xmlns:q1="http://www.w3.org/2001/XMLSchema" d1p1:type="q1:string">000000A4-A866-4516-A448-D35D990C67B8                            </anyType>
        <anyType xmlns:q2="http://www.w3.org/2001/XMLSchema" d1p1:type="q2:string">My title</anyType>
        <anyType xmlns:q3="http://www.w3.org/2001/XMLSchema" d1p1:type="q3:string" />
        <anyType xmlns:q4="http://www.w3.org/2001/XMLSchema" d1p1:type="q4:string" />
        <anyType xmlns:q5="http://www.w3.org/2001/XMLSchema" d1p1:type="q5:string" />
        <anyType xmlns:q6="http://www.w3.org/2001/XMLSchema" d1p1:type="q6:string" />
        <anyType xmlns:q7="http://www.w3.org/2001/XMLSchema" d1p1:type="q7:string" />
      </anyType>
    </anyType>
    


    Again, that one works fine. The problem is with the next webmethod that was added:


    <WebMethod()> _
        Public Function WDDocumentSearch() As Object
            Dim objarrdocs() As Object = Nothing
            Dim rs As Object
    
            Try
                rs = GetDocuments(<search criteria>)
    
                ReDim objarrdocs(rs.RecordCount - 1)
                Dim docstuff As Object
                Dim i As Integer = 0
                Do Until rs.EOF
                    docstuff = New Object() {rs.Fields("DocumentKey").Value, rs.Fields("DocTypeKey").Value, Format(rs.Fields("InsertDate").Value, "MM/dd/yyyy hh:mm:ss"), rs.Fields("DocumentTitle").Value, _                                        rs.Fields("UserDefined1").Value, rs.Fields("UserDefined2").Value, rs.Fields("UserDefined3").Value, rs.Fields("UserDefined4").Value, rs.Fields("UserDefined5").Value, rs.Fields("FileName").Value}
    
                    objarrdocs(i) = docstuff
                    i = i + 1
                    rs.MoveNext()
                Loop
                Return objarrdocs
            Catch ex As Exception
                Me.LastError = ex.ToString
                Return Nothing
            End Try
        End Function
    


    Leaving this code even in the webservice and trying to run any of the other webmethods results in the following error message (regardless of which webmethod is actually run):

    System.InvalidOperationException: The type System.Object[] may not be used in this context.


    This webmethod was copied from another webservice project. When run from THAT project, the webmethod executes perfectly as intended but not in this new project. Copying the entire webmethod code to the new project instantly breaks the entire new service (not just this webmethod) despite being configured exactly the same and running on and against the same server.


    What on earth is going on?

    Thursday, March 11, 2010 1:11 PM

Answers

  • User-952121411 posted

    Correct. I can literally copy and paste the exact code from one service to the other and the new one immediately breaks the service. It somehow affects all of the other webmethods as well, because they ALL break as soon as this code is added.
     

     

    Well I am a bit stumped at this moment because the code you have provided seems to be working and nothing syntactically incorrect, so there is nothing more to go on from here.  That really leaves one thing out, and possibly the culprit: configuration.  If the code works perfectly, then it may be possible that the (2) web services are configured differently. 

    I have been in this same situation many times.  One version works, here but not over there, etc.  The good news is you have a working copy.  The only advice I can give further is to look line by line, configuration by configuration setting of each service to see the differences.  Hopefully you will find what you need.

    As I posted previously, there are a ton of links on that exact error message you are receiving.  You may want to investigate them further for some more information than what I provided.

    Lastly, since this doesn't seem to be strictly a 'VB.NET' coding issue, you might try reposting this in the .ASMX forum located here (I think I saw your post as well on the MSDN forum):

    XML Web Services

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 15, 2010 10:59 AM

All replies

  • User-952121411 posted

    It is difficult to see any issues just based on the code you have posted.  There are only subtle differences between the working and non-working method, like 'Return Nothing' in the catch block, and the main Return statement being inside or outside of the Try-Catch block.

    Is this exception being caught as an issue within the method and defining 'Me.LastError'?  Is Me.LastError what you are reading to get the exception you posted?  Have you looked at the stack trace and any inner exceptions to see more detail possibly?  Can you run this code in a test harness on its own (not in a web service) and have it work correctly?  I typically yank a method out of a web service and test it independently to help narrow down issues.

    Also, you have another service where this method works, correct? Then that is the good news; this method can work, it is just a matter of playing detective and match setting for setting, line for line, and configuration settings between the 2 services to see any differences that may be causing the issue.

    As a side note, I really hope you are trying to support some legacy code with these methods as I see them using classic ADO recordsets, which is quite ancient in terms of current data access methods.  One possibility although not the easiest would be to use at least ADO.NET and possibly generics instead of plain 'Object()' types to possibly circumvent this issue altogether.

    Lastly, if this service is being used to communicate with any legacy code and hence the use of classic ADO, then you may want to post on a forum more appropriate like VBForums where VB6 code is supported.  Even though you are using a web service, that code is older and will be more difficult to get support on this forum, especially since it is not 'VB.NET' specific.  I also see you have this same question on the MSDN forum (http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/thread/2df81b32-c56d-4ba6-92fa-c109ad5a5b97) in the .ASMX forum, which hopefully will get some eyes on it as well.

    VB Forums:

    http://www.vbforums.com/

     

    Thursday, March 11, 2010 2:09 PM
  • User1112784522 posted

    Is this exception being caught as an issue within the method and defining 'Me.LastError'?

    Actually no...the webmethod itself is executing completely (and just before the final Return i can view the contents of the array and it's got all of the correct data in it. It's after (or during) the actual Return that the exception occurs and spits out the error message. The full stack trace is as follows:


    System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: 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)
       at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType)
       at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write17_anyType(Object o)
       at Microsoft.Xml.Serialization.GeneratedAssembly.ObjectSerializer6.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
       at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
       --- End of inner exception stack trace ---
       at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
       at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, 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()
    




    Also, you have another service where this method works, correct? Then that is the good news; this method can work, it is just a matter of playing detective and match setting for setting, line for line, and configuration settings between the 2 services to see any differences that may be causing the issue.


    Correct. I can literally copy and paste the exact code from one service to the other and the new one immediately breaks the service. It somehow affects all of the other webmethods as well, because they ALL break as soon as this code is added.

    As a side note, I really hope you are trying to support some legacy code with these methods as I see them using classic ADO recordsets, which is quite ancient in terms of current data access methods.

    Unfortunately, the API DLL that provides the business logic/data access layer returns a classic RecordSet. I've tried simply injecting the object array with simple Strings instead of using the RecordSet though and there's no change, so it isn't an issue with the use of classic RecordSets.


    Thursday, March 11, 2010 2:49 PM
  • User1112784522 posted

    Anyone have ideas? Bueller? Bueller?

    Monday, March 15, 2010 10:13 AM
  • User-952121411 posted

    Correct. I can literally copy and paste the exact code from one service to the other and the new one immediately breaks the service. It somehow affects all of the other webmethods as well, because they ALL break as soon as this code is added.
     

     

    Well I am a bit stumped at this moment because the code you have provided seems to be working and nothing syntactically incorrect, so there is nothing more to go on from here.  That really leaves one thing out, and possibly the culprit: configuration.  If the code works perfectly, then it may be possible that the (2) web services are configured differently. 

    I have been in this same situation many times.  One version works, here but not over there, etc.  The good news is you have a working copy.  The only advice I can give further is to look line by line, configuration by configuration setting of each service to see the differences.  Hopefully you will find what you need.

    As I posted previously, there are a ton of links on that exact error message you are receiving.  You may want to investigate them further for some more information than what I provided.

    Lastly, since this doesn't seem to be strictly a 'VB.NET' coding issue, you might try reposting this in the .ASMX forum located here (I think I saw your post as well on the MSDN forum):

    XML Web Services

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 15, 2010 10:59 AM
  • User-1738924320 posted

    I'm sorry I may be a little late in getting to this...

     

    Here's the solution. Good luck!!

     

    <WebMethod()> _

    <XmlInclude(GetType(Object()))>
    Public Function WDGetDocumentInfoObject() As Object

            Dim objarrdocument() As Object = Nothing

            Try
                document = GetDocument(pDocumentKey)

                ReDim objarrdocument(0)

                Dim docstuff As Object = New Object() {document.DocumentKey, document.DocumentTitle, document.UserDefined1, document.UserDefined2, document.UserDefined3, document.UserDefined4,  document.UserDefined5}

                objarrdocument(0) = docstuff
            Catch ex As Exception
                Me.LastError = ex.ToString
                objarrdocument = Nothing
            End Try
            Return objarrdocument
        End Function

    Tuesday, June 21, 2011 12:04 AM