简体   繁体   中英

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.

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);

Your data contract did not match with the service arguement.

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

Make it sure that you are using datacontract that came from your Service Reference and not the Role you created in your console. You should use data contract ConsoleApplication1.ServiceReference3.role and not ConsoleApplication1.Role they are different.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM