Mauricio Ramalho
Mauricio Ramalho

Reputation: 869

How to send a List<object> to a Web Method?

I am developing a web application using MVC 3 and ASMX Web Services.

I am trying to send a List< object > to a Web Method, but I get the following error:

" cannot convert from 'System.Collections.Generic.List' to 'WebServiceClass.ArrayOfAnyType' "

This is my Web Service definition:

public class WebServiceClass : System.Web.Services.WebService
{
    [WebMethod]
    public bool MyWebMethod(List<object> ParameterValues)
    {
        //do stuff..
    }
}

And this is the block of code where I call the Web Method:

        List<object> ParameterValues = new List<object>();

        WebServiceClass.WebServiceClassSoapClient MyWebService = new WebServiceClass.WebServiceClassSoapClient();

        //I use actual objects here, this is just for an example
        ParameterValues.Add(new DateTime(2012,5,2));
        ParameterValues.Add(23);
        ParameterValues.Add("some string");

        MyWebService.MyWebMethod(ParameterValues);

My idea was to save time and pass Lists of objects to all Web Methods instead of defining WebMethod(DateTime date, int someint, string somestring).

Is there a solution for this?

Best regards.

Upvotes: 0

Views: 652

Answers (1)

Jakub Konecki
Jakub Konecki

Reputation: 46008

If your method expects 3 parameters of type DateTime, int, and string than define a method with this arguments. Otherwise you will have to case down and your solution is not type-safe.

Are all your methods in code taking a list of objects as a parameter? Probably not. And the same should apply to web methods.

Upvotes: 0

Related Questions