Farshid Farshid - 3 months ago 21
C# Question

Passing class from console application to WCF service

I'm newbie to WCF and I created a WCF service library and a console application project. I use Entity Framework (database-first) for connecting to the database in my WCF service library project. I want to send a class to the WCF service (my problem). In my WCF project I created a

ITest.cs
and
Test.cs
that like are below:

ITest.cs

[OperationContract]
bool GetData(role rr);


Test.cs

public bool GetData(role rr)
{
try
{
iFlowEntities db = new iFlowEntities();
db.roles.Add(rr);
db.SaveChanges();
return true;
}
catch
{
return false;
}
}


and I add this service reference to my console application project reference and I create my DB class model in console application then use this service like :

Role rr = new Role();
rr.role1 = 10;
rr.title = "sdsafas";
TestClient client = new TestClient();
bool re = client.GetData(rr); //This line has error


but in this
bool re = client.GetData(rr);
I have this errors:


Error 1

The best overloaded method match for 'ConsoleApplication1.ServiceReference3.TestClient.GetData(ConsoleApplication1.ServiceReference3.role)' has some invalid arguments

Error 2

Argument 1: cannot convert from 'ConsoleApplication1.Role' to 'ConsoleApplication1.ServiceReference3.role'


I googled but any example hasn't solution for my problem.

Answer

You must use This DataContract in your entity model Class in WCF Model :

[DataContract]
Public Class role
{
[DataMember]
public int role1;
[DataMember]
public string title;
}

But not use from client model.

And use this parameter for passing Class Object to your WCF service OerationContract from your ConsoleApplicatione :

ServiceReference3.role role = new ServiceReference3.role();

role.role1=1;
role.title="Your Title";

TestClient client = new TestClient();
bool re = client.GetData(role);
Comments