简体   繁体   English

将类从控制台应用程序传递到WCF服务

[英]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. 我是WCF的新手,我创建了WCF服务库和控制台应用程序项目。 I use Entity Framework (database-first) for connecting to the database in my WCF service library project. 我使用实体框架(数据库优先)连接到WCF服务库项目中的数据库。 I want to send a class to the WCF service (my problem). 我想向WCF服务发送一个类(我的问题)。 In my WCF project I created a ITest.cs and Test.cs that like are below: 在我的WCF项目中,我创建了一个ITest.csTest.cs ,如下所示:

ITest.cs ITest.cs

[OperationContract]
bool GetData(role rr);

Test.cs 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); 但是在这个bool re = client.GetData(rr); I have this errors: 我有这个错误:

Error 1 错误1
The best overloaded method match for 'ConsoleApplication1.ServiceReference3.TestClient.GetData(ConsoleApplication1.ServiceReference3.role)' has some invalid arguments 'ConsoleApplication1.ServiceReference3.TestClient.GetData(ConsoleApplication1.ServiceReference3.role)'的最佳重载方法匹配具有一些无效的参数

Error 2 错误2
Argument 1: cannot convert from 'ConsoleApplication1.Role' to 'ConsoleApplication1.ServiceReference3.role' 参数1:无法从“ ConsoleApplication1.Role”转换为“ 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 : 您必须在WCF模型的实体模型类中使用This DataContract

[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 : 并使用此参数将Class Object从ConsoleApplicatione 传递到WCF服务OerationContract

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. 您应该使用数据协定ConsoleApplication1.ServiceReference3.role而不是ConsoleApplication1.Role它们是不同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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