无法将自定义数据类型作为参数传递给wcf服务

时间:2014-03-06 12:16:48

标签: asp.net wcf

我创建了一个自定义库项目,它有Employee类来保存员工信息。

namespace Test.SampleLib
{

    public class Employee
    {

        public string EmployeeName { get; set; }

        public double EmployeeSalary {get; set; }
    }
}

我将Employee类库添加到WCF服务

public Test.SampleLib.Employee GetDataUsingDataContract(Test.SampleLib.Employee composite)
{

    return composite;
}

我尝试使用该服务并访问方法GetDataUsingDataContract()

ServiceReference1.Service1Client objServiceRef = new ServiceReference1.Service1Client();
Test.SampleLib.Employee objEmployee = new SampleLib.Employee();
objEmployee.EmployeeName = "Kumar";
objEmployee.EmployeeSalary = 30000;

objServiceRef.GetDataUsingDataContract(objEmployee); //Gives errror

错误是

'Argument 1: cannot convert from 'Test.SampleLib.Employee' to 'Test.Web.ServiceReference1.Employee'

1 个答案:

答案 0 :(得分:1)

您需要在自定义数据类型及其要公开的属性上具有[DataContract][DataMember]属性。

    [DataContract]
    public class Employee
    {
        [DataMember]
        public string EmployeeName { get; set; }

        [DataMember]
        public double EmployeeSalary {get; set; }
    }