我创建了一个自定义库项目,它有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'
答案 0 :(得分:1)
您需要在自定义数据类型及其要公开的属性上具有[DataContract]
和[DataMember]
属性。
[DataContract]
public class Employee
{
[DataMember]
public string EmployeeName { get; set; }
[DataMember]
public double EmployeeSalary {get; set; }
}