Scor Pio
Scor Pio

Reputation: 49

Why Exception occured when method in webservice return Arraylist in c#?

i write method in web service returning array List and when i get that method it generate exception. My method is below

   //Webservice Method
    [WebMethod]
    public ArrayList ReceiveMessage(string strUser)
    {
        ArrayList arrList = new ArrayList();

        for (int i = 0; i < arrMessage.Count; i++)
        {
            object[] objArr = (object[])arrMessage[i];
            if (objArr[1].ToString() == strUser)
            {
                arrList.Add(new object[] {objArr[0],objArr[2], objArr[3], objArr[4] });


                arrMessage.RemoveAt(i);
                break;
            }
        }
        return arrList;
    }

    //Method from where i am calling webservice method  
    private void button2_Click(object sender, EventArgs e)
    {

        ArrayList obj = new ArrayList(objService1.ReceiveMessage("ABC"));
    }

Detail of Exception generate when call webservice method is below....

System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> 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.Write6_ReceiveMessageResponse(Object[] p) 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(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle)
at System.Web.Services.Protocols.SoapServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream) at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues) at System.Web.Services.Protocols.WebServiceHandler.Invoke() --- End of inner exception stack trace ---

Upvotes: 1

Views: 3781

Answers (1)

krystan honour
krystan honour

Reputation: 6793

The serializer doesn't know what to do with the underlying Object array inside the ArrayList.

You can fix this by returning a different type, say a Generic List or by decorating your method with:

[System.Xml.Serialization.XmlInclude(typeof(Object[]))]

There is a good explanation here

however for the benefit of this post you are basically telling the proxy generator the type of classes to generate otherwise it simply does not know what to generate and you get that exception.

based on this your webmethod code can be rewritten as:

[WebMethod]
[System.Xml.Serialization.XmlInclude(typeof(Object[]))]
public ArrayList ReceiveMessage(string strUser)
{
    ArrayList arrList = new ArrayList();

    for (int i = 0; i < arrMessage.Count; i++)
    {
        object[] objArr = (object[])arrMessage[i];
        if (objArr[1].ToString() == strUser)
        {
            arrList.Add(new object[] { objArr[0], objArr[2], objArr[3], objArr[4] });

            arrMessage.RemoveAt(i);
            break;
        }
    }
    return arrList;
}

If you can update your code its best not to use ArrayList anymore you could rewrite your code like this:

[WebMethod]
public List<object> ReceiveMessage(string strUser)
{
    List<object> list = new List<object>();

    for (int i = 0; i < arrMessage.Count; i++)
    {
        object[] objArr = (object[])arrMessage[i];
        if (objArr[1].ToString() == strUser)
        {
            list.Add(new object[] { objArr[0], objArr[2], objArr[3], objArr[4] });
            arrMessage.RemoveAt(i);
            break;
        }
    }
    return list;
}

Hope this helps.

Upvotes: 1

Related Questions