Web service cast exception why?
-
03-07-2019 - |
Question
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?
Solution
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
OTHER TIPS
The SOAP protocol doesn't support generic collections.
Try this instead:
List<string> bob = new List<string>(myService.GetAllList());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow