开发者

ASP.NET Web Service return arraylist of different object types

I got a problem when I tried ASP.NET WebService to return arraylist of several object types. Suppose I have a Book object and a Table object. I added Book and开发者_如何转开发 Table objects to an ArrayList. After that I return that arraylist in webservice. It does not allow me to do. How can I make it be able to return several object types?


Can you not define a complex object (DTO in this examle) that contains the other objects and return the populated DTO in you webmethod:

[OperationContract]
Dto GetBooksAndTables();


[DataContract]
public class Dto
{
    [DataMember]
    public Book[] Books { get; set; }

    [DataMember]
    public Table[] Tables { get; set; }
}

[DataContract]
public class Book 
{
   [DataMember]
   public string BookName {get; set; }
   //etc...
}

[DataContract]
public class Table 
{
   [DataMember]
   public string TableName {get; set; }
   //etc...
}

Have you looked at http://wcf.codeplex.com/wikipage?title=WCF%20HTTP - it makes building .NET services much easier. It's on NuGet.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜