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.
精彩评论