为什么要使用[Datacontract]和[DataMember] WCF?

时间:2016-02-21 11:44:36

标签: wcf datacontract datamember

假设我们为WCF服务和具有Employee表的数据库提供了以下代码。

为什么要使用[DataContract]以及使用所有[DataMember]有什么好处? 我们可以使用所有Employee详细信息,而无需使用[DataContract]

[ServiceContract]
public interface IEmployeeService
{
    [OperationContract]
    Employee GetEmployeeDetails(int EmpId);
}

[DataContract]
public class Employee
{
    private string m_Name;
    private int m_Age;
    private int m_Salary;

    [DataMember]
    public string Name
    {
        get { return m_Name; }
        set { m_Name = value; }
    }

    [DataMember]
    public int Age
    {
        get { return m_Age; }
        set { m_Age = value; }
    }

    [DataMember]
    public int Salary
    {
        get { return m_Salary; }
        set { m_Salary = value; }
    }

}

服务实施:

public class EmployeeService : IEmployeeService
{
    public Employee GetEmployeeDetails(int empId)
    {

        Employee empDetail = new Employee();
        return empDetail;
    }
}

我们的客户:

private void btnGetDetails_Click(object sender, EventArgs e)
    {
        EmployeeServiceClient objEmployeeClient = new EmployeeServiceClient();
        Employee empDetails;
        empDetails = objEmployeeClient.GetEmployeeDetails(empId);
    }

0 个答案:

没有答案