0

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.

1 Answer 1

0

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.

1
  • I want all my web methods to receive a list of objects because all of them call stored procedures with a lot parameters. My idea is to then iterate the list with a foreach and add all parameters to the stored procedure command effortlessly. I thought about an alternative, which is to have the List< object > on the web service side and then call a web method that adds a single object to the list. The problem is that when I call the web method, a new instance of the web service is created and I lose what was previously inside the list. Aug 3, 2012 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.