我有一个包含3个项目的c#解决方案:
在项目模型下,我有一个模型员工:
[Table("employee")]
public class Employee
{
[Key, Column("organizationid", TypeName = "uniqueidentifier", Order=0)]
public Guid OrganizationId { get; set; }
[Key, Column("personid", TypeName = "uniqueidentifier", Order=1)]
public Guid PersonId { get; set; }
[Column("jobtitle", TypeName = "nvarchar")]
public String JobTitle { get; set; }
[Column("active", TypeName = "bit")]
public Boolean Active { get; set; }
[Column("telecom", TypeName = "nvarchar")]
public String Telecom { get; set; }
[Column("email", TypeName = "nvarchar")]
public String Email { get; set; }
[Column("confidentialitycode", TypeName = "nvarchar")]
public String ConfidentialityCode { get; set; }
[Column("priority", TypeName = "int")]
public Int32 Priority { get; set; }
#region Foreign Relations
[ForeignKey("OrganizationId")]
public virtual Organization CurrentOrganization { get; set; }
[ForeignKey("PersonId")]
public virtual Person CurrentPerson { get; set; }
#endregion
}
然后我创建了一个名为 Test.svc 的WCF服务,其中包含以下内容:
public class Test : ITest
{
public Model.POCO.Employee DoWork()
{
Model.POCO.Employee newItem = new Model.POCO.Employee();
newItem.PersonId = Guid.NewGuid();
newItem.OrganizationId = Guid.NewGuid();
newItem.Priority = 1;
newItem.Telecom = "";
newItem.JobTitle = "";
newItem.Email = "";
newItem.Active = true;
return newItem;
}
}
[ServiceContract]
public interface ITest
{
[OperationContract]
Model.POCO.Employee DoWork();
}
在客户端项目中,我添加了一个按钮,在click事件中我有这个代码:
private void button1_Click(object sender, EventArgs e)
{
DataReference.WCFTest.TestClient svc = new DataReference.WCFTest.TestClient();
var employee = svc.DoWork();
svc = null;
}
如果我查看“var employee”,我可以看到该对象在那里并且工作正常,但“employee”不是 Model.POCO.Employee 类型,而是 WCFTest。员工类型。
如何让我的WCF服务返回Model.POCO.Employee?这有什么解决方法吗?或者我可以将WCFTest.Employee自动挂载到Model.POCO.Employee吗?
非常感谢。
答案 0 :(得分:1)
如何让我的WCF服务返回Model.POCO.Employee?
确实如此,您的客户只是不知道该类型。
从客户端项目中引用Models项目并确保在“Configure Service Reference”(或在创建一个“Advanced”下),在“Reuse types”中检查Models程序集。