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

本文关键字:WCF 服务 应用程序 控制台 | 更新日期: 2023-09-27 18:01:10

我是WCF的新手,我创建了一个WCF服务库和一个控制台应用程序项目。我使用实体框架(数据库优先(连接到我的WCF服务库项目中的数据库。我想向WCF服务发送一个类(我的问题(。在我的WCF项目中,我创建了一个ITest.csTest.cs,如下所示:

ITest.cs

[OperationContract]
bool GetData(role rr);

测试.cs

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

我将这个服务引用添加到我的控制台应用程序项目引用中,并在控制台应用程序中创建我的DB类模型,然后使用这个服务,如:

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

但在这个bool re = client.GetData(rr);中,我有这样的错误:

错误1
与"ConsoleApplication1.ServiceReference3.TestClient.GetData(ConsoleApplication1.ServiceReference3.角色("匹配的最佳重载方法具有一些无效参数

错误2
参数1:无法从"ConsoleApplication1.Role"转换为"ConsoleAApplication1.ServiceReference3.Role">

我在谷歌上搜索了一下,但任何例子都不能解决我的问题。

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

您必须在实体模型中使用ThisDataContractWCF模型中的类:

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

但不使用来自客户端的模型

并使用此参数从ConsoleApplicatione:向WCF服务传递类对象

ServiceReference3.role role = new ServiceReference3.role();
role.role1=1;
role.title="Your Title";
TestClient client = new TestClient();
bool re = client.GetData(role);

您的数据协定与服务论证不匹配。

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

确保您使用的数据合约来自您的服务参考,而不是您在控制台中创建的角色。您应该使用数据契约ConsoleApplication1.ServiceReference3.role而不是ConsoleApplication1.Role,它们是不同的。