使用 WCF 序列化 POCO 代理
当我运行服务时,出现异常:
服务器在处理请求时遇到错误。异常消息是“无法序列化 System.Data.Entity.DynamicProxies.Cosik_14C2... 类型的参数”(对于操作“GetCosik”,合同“ICosikService”),因为它不是确切的类型“Project.Domain.Entities.Cosik” ' 在方法签名中,并且不在已知类型集合中。为了序列化参数,请使用 ServiceKnownTypeAttribute 将类型添加到操作的已知类型集合中。有关更多详细信息,请参阅服务器日志。
我是 WCF 服务和实体框架的新手,非常感谢您提供任何帮助/建议。
我正在使用实体框架 4.1。使用代码优先,我创建了包含两个表的数据库:
[DataContract(IsReference=true)]
public class Cosik
{
[DataMember]
public int cosikID { get; set; }
[DataMember]
public string title { get; set; }
[DataMember]
public int DifficultyID { get; set; }
[DataMember]
public virtual Difficulty Difficulty { get; set; }
}
[DataContract(IsReference=true)]
public class Difficulty
{
[DataMember]
public int DifficultyID { get; set; }
[DataMember]
[Required]
public string NameToDisplay { get; set; }
}
接下来,我创建了 WCF 服务应用程序并使其成为 RESTful。下面是接口的代码:
[ServiceContract]
public interface ICosikService
{
[OperationContract]
[ApplyDataContractResolver]
[WebGet(UriTemplate = "/cosik")]
Cosik GetCosik();
}
以及该合同的实现,
public class RecipeService : IRecipeService
{
//repository of Cosik entities - stores collection of all
//Cosik entities that can be queried from DB
private ICosikRepository cosikRepo;
...
public Cosik GetCosik()
{
Cosik c = cosikRepo.GetById(1);
return c;
}
我实现了ApplyDataContractResolverAttribute类,给出了:http://msdn。 microsoft.com/en-us/library/ee705457.aspx 并向 GetCosik 方法添加了 [ApplyDataContractResolver] 注释。然而,这并没有帮助。
有什么建议我错过了什么吗?
When I run my service I got exception:
The server encountered an error processing the request. The exception message is 'Cannot serialize parameter of type System.Data.Entity.DynamicProxies.Cosik_14C2...' (for operation 'GetCosik', contract 'ICosikService') because it is not the exact type 'Project.Domain.Entities.Cosik' in the method signature and is not in the known types collection. In order to serialize the parameter, add the type to the known types collection for the operation using ServiceKnownTypeAttribute.'. See server logs for more details.
I'm new to WCF services and Entity Framework and I'd appreciate any help/suggestions.
I'm using Entity Framework 4.1. Using code-first I created database with two tables:
[DataContract(IsReference=true)]
public class Cosik
{
[DataMember]
public int cosikID { get; set; }
[DataMember]
public string title { get; set; }
[DataMember]
public int DifficultyID { get; set; }
[DataMember]
public virtual Difficulty Difficulty { get; set; }
}
[DataContract(IsReference=true)]
public class Difficulty
{
[DataMember]
public int DifficultyID { get; set; }
[DataMember]
[Required]
public string NameToDisplay { get; set; }
}
Next I created the WCF service application and made it RESTful. Below is code for interface:
[ServiceContract]
public interface ICosikService
{
[OperationContract]
[ApplyDataContractResolver]
[WebGet(UriTemplate = "/cosik")]
Cosik GetCosik();
}
and implementation of that contract
public class RecipeService : IRecipeService
{
//repository of Cosik entities - stores collection of all
//Cosik entities that can be queried from DB
private ICosikRepository cosikRepo;
...
public Cosik GetCosik()
{
Cosik c = cosikRepo.GetById(1);
return c;
}
I implemented ApplyDataContractResolverAttribute class given on: http://msdn.microsoft.com/en-us/library/ee705457.aspx and added [ApplyDataContractResolver] annotation to GetCosik method. However, it didn't help.
Any suggestion what I've missed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要开发自定义解析器,而是关闭代理创建。代理不适用于 WCF 等场景,因为在序列化期间必须关闭延迟加载,并且永远不会使用动态更改跟踪:
Instead of developing custom resolver turn off proxy creation. Proxies are not for scenarios like WCF because lazy loading must be turned off anyway during serialization and dynamic change tracking is never used: