locked
Can a web service return a List<T>? RRS feed

  • Question

  • I am pretty new to web services; so sorry if this is a stupid question; but I would like to return a List<T> of a custom object and in my client code when it calls the web service, I am getting an error stating: Cannot implicitly convert type 'WebServiceLayer.Class[]' to 'BusinessLayer.List<Class>'.  So it appears my web service is trying to convert my List to an Array.  I have been looking around online and getting conflicting reports on whether or not generics, like my List can actually be returned from a web service.  Some places so now; other places say yes; neither which provide good examples.

    My question is, can in fact generics such as the List<T> be returned from a web service?   If so, can anyone provide any tips, like, is there any special XML Serialization or stuff like that that I need to do? Right now I just have the Attribute [Serializable] declared above my class. Didn't know if I had to do anything else or not.

    Thanks,
    Flea#

    • Moved by nobugzModerator Saturday, August 1, 2009 8:15 PM (From:Visual C# General)
    Saturday, August 1, 2009 7:48 PM

Answers

  • Actually, web services never return objects of any kind. They only return XML.

    See Basics: How Web Services Work to to understand why List<T> on the server may appear to be TProxy[] on the client.

    Also, be sure to see Microsoft: ASMX Web Services are a “Legacy Technology” to understand why you should be starting off with WCF.

    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects
    • Proposed as answer by SantoshR Tuesday, August 4, 2009 7:17 AM
    • Marked as answer by Flea_ Tuesday, August 4, 2009 8:21 PM
    Saturday, August 1, 2009 8:49 PM
    Moderator

All replies

  • Actually, web services never return objects of any kind. They only return XML.

    See Basics: How Web Services Work to to understand why List<T> on the server may appear to be TProxy[] on the client.

    Also, be sure to see Microsoft: ASMX Web Services are a “Legacy Technology” to understand why you should be starting off with WCF.

    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects
    • Proposed as answer by SantoshR Tuesday, August 4, 2009 7:17 AM
    • Marked as answer by Flea_ Tuesday, August 4, 2009 8:21 PM
    Saturday, August 1, 2009 8:49 PM
    Moderator
  • You can do like this way.
    in Web service:

    [

    WebMethod]
    public MyCustomeClass[] GetList()
    {
    List<MyCustomeClass> dd = new List<MyCustomeClass>();
    dd.Add(obj1
    );
    dd.Add(
    obj2);
    return dd.ToAarray();
    }
    this obj1 and obj2 are object of MyCustomeClass

    in your application use like this way:

    MyService.

    Service objService = new MyService.Service();
    List<MyCustomeClass> mylist = new List<MyCustomeClass>(objService.GetList());

    Now you can use this 'mylist' List<T> object.

     

    Tuesday, August 4, 2009 7:23 AM
  • Why is this necessary? What does it accomplish?

    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects
    Tuesday, August 4, 2009 4:29 PM
    Moderator
  • Thanks John!
    Tuesday, August 4, 2009 8:22 PM