Sunday, April 3, 2011

Web service cast exception why?!

Error Cannot implicitly convert type 'string[]' to 'System.Collections.Generic.List<string>'

The above error is caused when I call a method to a web service

List<string> bob = myService.GetAllList();

Where: GetAllList =

[WebMethod]
        public List<string> GetAllList()
        {

            List<string> list ....
            return list;
        }

I have rebuilt the whole solution, updated the service references and still I get a cast exception any ideas?

From stackoverflow
  • you need to do this:

    List<string> bob = new List<string>(myService.GetAllList());
    

    An overload for the constructor of a generic list takes an IEnumerable of the specified type to initialize the array. You can not, like the exception states, implicitly cast it staright to that type.

    Andrew

  • The SOAP protocol doesn't support generic collections.

    Try this instead:

    List<string> bob = new List<string>(myService.GetAllList());
    

0 comments:

Post a Comment