返回列表< T>使用WCF服务 [英] Returning List<T> with WCF service

查看:46
本文介绍了返回列表< T>使用WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Employee 类,每个雇员都有一个已应用的叶子列表.在WCF中是否可以将列表 AppliedLeave 作为 [DataMember] ?

I got an Employee class and each employee has a list of applied leaves. Is it possible to have the list AppliedLeave as a [DataMember] in WCF?

[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 ---->

this shows ServiceContract ---->

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

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

    [OperationContract]
    void DeleteEmployeeByUserId(string userId);

....
}

在客户端应用程序中,

EmployeeServiceClient employeeService =新的EmployeeServiceClient();

EmployeeServiceClient employeeService = new EmployeeServiceClient();

Employee employee = employeeService.GetEmployeeByUserId(id);

Employee employee = employeeService.GetEmployeeByUserId(id);

但是当Employee从服务中收集时,它显示Null for leaves,

But when Employee gathered from the service its shows Null for leaves,

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

Can somebody help me? what have I done wrong here?

推荐答案

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

Yes, it is possible to return generics from WCF service operations.

但是默认情况下,它们被强制转换为客户端的Array.可以在生成代理时进行自定义.

But by default they are casted to Array on client side. This can be customized while proxy generation.

WCF:序列化和泛型

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

Also you have to decorate the service with all the types to which generics can be resolved, using KnownTypeAttribute.

已知类型和通用解析器

这篇关于返回列表&lt; T&gt;使用WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆