c# - 使用 WCF 服务返回 List<T>

标签 c# wcf

我有一个 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>();
    ...
 }

还有其他方法吗?

感谢您对此事的考虑

我扩展我的问题

这是我的休假类(class):

[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 employee = employeeService.GetEmployeeByUserId(id);

但是当 Employee 从服务中收集时,它的叶子显示为 Null,

enter image description here

有人可以帮助我吗?我在这里做错了什么?

最佳答案

是的,可以从 WCF 服务操作返回泛型。

但默认情况下,它们在客户端被转换为数组。这可以在代理生成时自定义。

WCF: Serialization and Generics

此外,您还必须使用 KnownTypeAttribute 使用泛型可以解析为的所有类型来装饰服务。

Known Types and the Generic Resolver

关于c# - 使用 WCF 服务返回 List<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10646978/

相关文章:

c# - 使用不带套接字的 TLS

c# - 访问参数的属性时使用 ArgumentNullException

c# - 在 C# 中使用 for 循环创建查询字符串

wcf - 你将只有自治服务

wcf - 带有 RIA WCF 项目的 Silverlight 无法添加常规 WCF 服务引用

c# - 正则表达式 c# 只有正数

wcf - WCF 服务中的 *Result 和 *ResultSpecified 参数?

wcf - 从 jQuery 调用启用 AJAX 的 WCF 服务

c# - WCF,用我的IP替换“localhost”还是应该使用NetTcpBinding?

c# - 使用 asp.net 将参数传递给 SSRS 报告过滤器下拉列表