2014-03-06 132 views
1

我創建了一個自定義庫項目,並且它具有Employee類以保存員工信息。無法將自定義數據類型作爲參數傳遞給wcf服務

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

您是否試圖將您的變量聲明爲Test.Web.ServiceReference1.Employee而不是Test.SampleLib.Employee? –

回答

1

您需要在自定義數據類型及其要公開的屬性上擁有[DataContract][DataMember]屬性。

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

     [DataMember] 
     public double EmployeeSalary {get; set; } 
    } 
相關問題