Return List <T> with WCF Service

I got a class Employee, and each employee has a list of applied sheets. Is it possible to have a list AppliedLeavelike [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 considering this issue

I am expanding my question

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

}

it shows ServiceContract ---->

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

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

    [OperationContract]
    void DeleteEmployeeByUserId(string userId);

....
}

In the client application

EmployeeServiceClient employeeService = new EmployeeServiceClient ();

Employee Employee = employeeService.GetEmployeeByUserId (id);

But when the Employee collected from the service his Null displays for leaves,

enter image description here

Can someone help me? what did i do wrong here?

+3
source share
3 answers

Yes, you can return generics from WCF service operations.

Array . .

WCF:

, , KnownTypeAttribute.

+5

IList <T> <T>

+2

Changes in my decision

, . , ( " " )

. , " WCF" "Winforms app" . , Service1 , , WCF. null.

When I put my IService1.cs = only interface = in a separate class library, access the class library on both sides (using) and generate the service link again, my list really works! The generated client-side code is much simpler.

I do not need any special attributes to change the link configuration of the service or the interface links for this.

+2
source

All Articles