开发者

WebService: How to return an array of a complex type?

2 Questions really, and I'm not sure if I'm doing this right or not...

I want to send back an array of some sort for an Entity object I created. I'm not really sure how to mark it for sending (what attributes are needed or whatever), and I'm not really sure how to send it back (IList, List, Collection, ICollection).

Ideally I'd love to be able to send back an ObservableCollection, but would that work if a client that wants to consume the service isn't in .NET?

[ServiceContract(Namespace = "http://www.tempuri.com/MyService",
                 ConfigurationName = "IMyService")]
public interface IMyServie
{
    [OperationContract(Action = "http://www.tempuri.com/MyService/GetUsers",
                       ReplyAction = "*")]
    [XmlSerializerFormat(SupportFaults = true)]
    GetUsersResponse GetUsers(GetUsersRequest request);
}

[MessageContract(IsWrapped = false)]
public sealed class GetUsersRequest
{
    public GetUsersRequest() { }
    public GetUsersRequest(Int32 record = -1)
    {
        Record = record;
    }

    [MessageBodyMember(Namespace = "", Order = 0)]
    public Int32 Record { get; private set; }

}

[MessageContract(IsWrapped = false)]
//[ServiceKnownType(??)]
public sealed class GetUsersResponse
{
    public GetUsersResponse() { }
    public GetUsersResponse(PersonEntity[] entities)
    {
        Entities = entities;
    }

    [MessageBodyMember(Namespace = "", Order = 1)]
    //[XmlElement(DataType = "??")]
    public PersonEntity[] Entities { get; private set; }
    //Should this have been some other type of array-type (Collection, List, etc?)
}

//Does this need any attributes besides Serializable?
[Serializabl开发者_开发百科e()]
public PersonEntity : AbstractEntity
{
    public PersonEntity() { }
    public PersonEntity(Int32 id = 0, String fname = "", String lname = "")
    {
        ID        = id;
        FirstName = fname;
        LastName  = lname;
    }

    public String FirstName { get; set; }
    public String LastName { get; set; }

    //Functionality (Clone, Clear, Default, Equals, etc) Removed...
}

[Serializable()]
public abstract class AbstractEntity : IEntity
{
    public Int32 ID { get; set; }

    //Abstracts or Implements some functionality...
}

public interface IEntity
{
    //Defines functionality requirements
}


DataContract is what you are looking for. For more details check this link

On Observable collection front, not sure why sending an array would be any different for you. Can you elaborate a bit more to help us understand this.


When it comes to passing objects over the wire, you must think of data rather than behaviour. So as far as this is concerned, a List or IEnumerable or T[] would not differ and my preference would be array which is supported in all languages. Server should not know or make any unncessary assumption about the client (WPF, Windows forms, ...) and all you are passing is a collection of data so least prescriptive is the best: T[] (Array of T).

As myermian said, your ObservableCollection sent over the wire would not be working as an ObservableCollection on the client.

Also you are using MessageContract while not using any of its benefits of Body/Header. So you must use DataContract.


The most inter-operable means would be to return an array of the complex type, whatever you decide that to be. My personal preference is to use a dedicated DTO object for transfer across the wire, rather than expose internal entities directly via the service.

Regarding ObservableCollection, I suggest you wrap your array in an ObservableCollection client-side rather than try to return ObservableCollection.

Keep in mind that ObservableCollection will not transmit changes back to the service; you're going to h ave to do those manually.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜