2010-08-31 70 views
0

2問題真的,我不知道我是否正確地做這件事...WebService:如何返回一個複雜類型的數組?

我想發回一些我創建的實體對象的數組。我真的不知道如何標記它發送(需要什麼屬性或什麼),我真的不知道如何發回(IList,List,Collection,ICollection)。

理想情況下,我很想能夠發回一個ObservableCollection,但是如果想要使用該服務的客戶端不在.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? 
[Serializable()] 
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 
} 
+0

我建議用[DataContract]替換[Serializable]。 – 2010-08-31 17:40:04

回答

1

DataContract是你在找什麼。有關更多詳細信息,請檢查此link

On Observable collection front,不知道爲什麼發送數組對於您來說會有任何不同。你能詳細說明一下,以幫助我們理解這一點。

+0

您是否必須在父抽象類和接口上標記此DataContract? – 2010-08-31 18:11:36

+1

在派生類Personentity上應該有足夠的屬性。因爲這是通過頻道交換的唯一類型 – 2010-08-31 18:29:51

0

最可互操作的方法將返回一個複雜類型的數組,無論您決定如何。我個人的偏好是使用專用的DTO對象通過線路傳輸,而不是直接通過服務暴露內部實體。

關於ObservableCollection,我建議你將你的數組包裝在ObservableCollection客戶端,而不是試着返回ObservableCollection

請注意,ObservableCollection不會將更改發送回服務;你將需要手動完成這些工作。

+0

我只是看到,如果我可以跳過Array-To-ObservableCollection的步驟,因爲客戶端將是WPF。 – 2010-08-31 18:18:48

+0

@myermian:你可能會 - 'OC'畢竟是可序列化的 - 但我不會。首先,你不會得到任何好處;例如,如果服務有新數據,則服務返回的「OC」不會更新,並且對它的更新不會傳播回服務。其次,它會不必要地破壞你可能獲得的任何互操作性。這對你來說可能也可能不是問題,但對我來說這永遠都是一個問題 - 我喜歡讓我的選擇保持開放。 – Randolpho 2010-08-31 18:32:08

1

當涉及通過線路傳遞對象時,您必須考慮數據而不是行爲。因此,就這一點而言,List或IEnumerable或T []不會有區別,我的偏好是所有語言都支持的數組。服務器不應該知道或者對客戶端(WPF,Windows窗體,......)進行任何毫不含糊的假設,並且你傳遞的所有數據都是一組數據,因此最不規範的是最好的:T [](Array of T)。

正如myermian所說,通過線路發送的ObservableCollection不會作爲客戶端上的ObservableCollection工作。

此外,您正在使用MessageContract,同時不使用Body/Header的任何好處。所以你必須使用DataContract。