Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将类从控制台应用程序传递到WCF服务_C#_Wcf_Parameter Passing - Fatal编程技术网

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

C# 将类从控制台应用程序传递到WCF服务,c#,wcf,parameter-passing,C#,Wcf,Parameter Passing,我是WCF的新手,我创建了一个WCF服务库和一个控制台应用程序项目。我使用实体框架(数据库优先)连接到WCF服务库项目中的数据库。我想向WCF服务发送一个类(我的问题)。在我的WCF项目中,我创建了ITest.cs和Test.cs,如下所示: ITest.cs [OperationContract] bool GetData(role rr); Test.cs public bool GetData(role rr) { try {

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

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;
        }
     }
我将此服务引用添加到控制台应用程序项目引用中,并在控制台应用程序中创建DB类模型,然后使用此服务,如:

        Role rr = new Role();
        rr.role1 = 10;
        rr.title = "sdsafas";
        TestClient client = new TestClient();
        bool re = client.GetData(rr); //This line has error
但是在这个
boolre=client.GetData(rr)我有以下错误:

错误1
与“ConsoleApplication1.ServiceReference3.TestClient.GetData(ConsoleApplication1.ServiceReference3.role)”匹配的最佳重载方法具有一些无效参数

错误2
参数1:无法从“ConsoleApplication1.Role”转换为“ConsoleApplication1.ServiceReference3.Role”


我在谷歌上搜索过,但任何例子都无法解决我的问题。

您的数据合同与服务条款不匹配

错误2
参数1:无法从“ConsoleApplication1.Role”转换为“ConsoleApplication1.ServiceReference3.Role”


确保您使用的是来自服务引用的datacontract,而不是您在控制台中创建的角色。您应该使用数据协定
ConsoleApplication1.ServiceReference3.role
而不是
ConsoleApplication1.role
它们是不同的。

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

错误2
参数1:无法从“ConsoleApplication1.Role”转换为“ConsoleApplication1.ServiceReference3.Role”


确保您使用的是来自服务引用的datacontract,而不是您在控制台中创建的角色。您应该使用数据协定
ConsoleApplication1.ServiceReference3.role
而不是
ConsoleApplication1.role
它们是不同的。

您必须在WCF model中的实体模型类中使用此DataContract

[DataContract]
Public Class role
{
[DataMember]
public int role1;
[DataMember]
public string title;
}
但不从客户机型号使用。

并使用此参数从控制台应用程序向WCF服务OERationalContract传递Class对象:

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

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

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

您必须在WCF模型中的实体模型类中使用此DataContract

[DataContract]
Public Class role
{
[DataMember]
public int role1;
[DataMember]
public string title;
}
但不从客户机型号使用。

并使用此参数从控制台应用程序向WCF服务OERationalContract传递Class对象:

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

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

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

您是否可以完全限定TestClient,以查看是否出现相同的错误,即TestClient应该是MyNamepSpace.Class.TestClient??i使用它的命名空间!喜欢使用system.servicereference;您是否可以完全限定TestClient,以查看是否出现相同的错误,即TestClient应该是MyNamepSpace.Class.TestClient??i使用它的命名空间!喜欢使用system.servicereference;