簡體   English   中英

退貨清單 <T> 使用WCF服務

[英]Returning List<T> with WCF service

我有一個Employee類,每個員工都有一個應用的葉子列表。 是否可以將列表AppliedLeave作為WCF中的[DataMember]

[DataContract]
public class Employee
{
    [DataMember]
    public string UserID { get; set; }

    [DataMember]
    public int EmployeeNumber { get; set; }

    [ForeignKey("EmployeeUserID")]
    [DataMember]
    public List<Leave> AppliedLeave
    {
        get { return _appliedLeaves; }
        set { _appliedLeaves = value; }
    }

    private List<Leave> _appliedLeaves = new List<Leave>();
    ...
 }

有沒有其他方法可以做到這一點?

感謝您對此事的考慮

我延伸我的問題

這是我的休假班:

[DataContract]
public class Leave
{

    [Key()]
    [DataMember]
    public Guid LeaveId { get; set; }

    [DataMember]
    public string LeaveType { get; set; }

    [DataMember]
    public DateTime StartDate { get; set; }

    [DataMember]
    public string EmployeeUserID { get; set; }

}

這顯示了ServiceContract ---->

[ServiceContract]
public interface IEmployeeService
{
    [OperationContract]
    Employee GetEmployeeByUserId(string userId);

    [OperationContract]
    void AssignSupervisor(string userId, string supervisorUserId);

    [OperationContract]
    void DeleteEmployeeByUserId(string userId);

....
}

在客戶端應用中,

EmployeeServiceClient employeeService = new EmployeeServiceClient();

員工employee = employeeService.GetEmployeeByUserId(id);

但是,當員工從服務中收集時,它會顯示Null for leaves,

在此輸入圖像描述

有人能幫助我嗎? 我在這做錯了什么?

是的,可以從WCF服務操作返回泛型。

但默認情況下,它們會在客戶端轉換為Array。 這可以在代理生成時自定義。

WCF:序列化和泛型

此外,您還必須使用KnownTypeAttribute使用可以解析泛型的所有類型來裝飾服務。

已知類型和通用解析器

您可以使用IList<T>而不是List<T>

我的解決方案的變化

我還發現我的服務器端列表總是作為空指針到達客戶端。 在瀏覽了很多這個問題之后,我覺得它幾乎總是被拒絕(“你的代碼應該工作”)

發現問題..我使用一個“WCF服務”項目和一個帶有生成服務引用的“Winforms app”項目配置了我的解決方案。 正如預期的那樣,Service1的接口和實現都在WCF服務項目中。 但是任何列表成員都返回null。

當我把我的IService1.cs = 接口=在一個單獨的類庫中時,引用兩側的類庫(使用)並再次生成服務引用,我的列表確實有效! 客戶端生成的代碼看起來更簡單。

我不需要任何特殊屬性,更改服務引用配置或接口引用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM