user1784973
user1784973

Reputation: 95

Unable to pass custom datatype as parameter to wcf service

I created one custom library project and it have Employee class to hold employee information.

namespace Test.SampleLib
{

    public class Employee
    {

        public string EmployeeName { get; set; }

        public double EmployeeSalary {get; set; }
    }
}

I added the Employee class library to the WCF service

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

    return composite;
}

I tried to consume the service and access the method 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

The error is

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

Upvotes: 1

Views: 348

Answers (1)

Romesh D. Niriella
Romesh D. Niriella

Reputation: 480

You need to have [DataContract] and [DataMember] attributes on custom data types and its properties you want to expose.

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

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

Upvotes: 1

Related Questions