C# 将类从控制台应用程序传递到WCF服务
我是WCF的新手,我创建了一个WCF服务库和一个控制台应用程序项目。我使用实体框架(数据库优先)连接到WCF服务库项目中的数据库。我想向WCF服务发送一个类(我的问题)。在我的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 {
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;