简体   繁体   中英

Returning List<T> with WCF service

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>();
    ...
 }

Is there any other way to do this?

thank you for your consideration of this matter

I extend my Question

This is my 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; }

}

this shows ServiceContract ---->

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

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

    [OperationContract]
    void DeleteEmployeeByUserId(string userId);

....
}

In Client application,

EmployeeServiceClient employeeService = new EmployeeServiceClient();

Employee employee = employeeService.GetEmployeeByUserId(id);

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

在此输入图像描述

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

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

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

WCF: Serialization and Generics

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

Known Types and the Generic Resolver

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

我的解决方案的变化

I also found my server side list would always arrive at the client as a null pointer. After browsing around a lot for this problem it strikes me it is nearly always denied at first ("your code should work")

Found the issue.. I had configured my solution using one "WCF Service" project and one "Winforms app" project with a generated service reference. Both interface and implementation of Service1 were in the WCF service project, as expected. But any list member returned null.

When I put my IService1.cs = the interface only = in a separate class library instead, reference the class library on both sides (using) and generate the service reference again, my list does work ! The generated code on the client side looks much simpler.

I did not need any special attributes, change service reference configuration, or interface references for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM