使用WCF 4.0的Entity Framework 4.0 DataContractSerializer错误 [英] DataContractSerializer Error using Entity Framework 4.0 with WCF 4.0

查看:135
本文介绍了使用WCF 4.0的Entity Framework 4.0 DataContractSerializer错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我尝试通过WCF从Entity框架中检索对象列表,但是收到以下异常:尝试序列化参数< a href =http://tempuri.org/:GetAllResult =noreferrer> http://tempuri.org/:GetAllResult 。设置InnerException消息是 '类型 'System.Data.Entity.DynamicProxies.TestObject_240F2B681A782799F3A0C3AFBE4A67A7E86083C3CC4A3939573C5410B408ECCE' 与数据合同名' TestObject_240F2B681A782799F3A0C3AFBE4A67A7E86083C3CC4A3939573C5410B408ECCE: http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies '不是预期的。考虑使用DataContractResolver或将已知类型列表中不知道的任何类型添加到已知类型列表中,例如使用KnownTypeAttribute属性或将其添加到传递给DataContractSerializer的已知类型的列表中。请参阅InnerException了解更多详细信息。



我过去使用过WCF,但从未使用实体框架。我有通过实体框架生成的所有实体,并用[DataContract]和[DataMember]属性注释。我的任何实体都没有导航属性。



正在调用的GetAll()方法在抽象服务类中:

  [ServiceContract] 
public interface IService< T>
{
[OperationContract]
列表< T>得到所有();
}

我正在使用ChannelFactory来调用我的实现:

 绑定绑定=新的NetTcpBinding(); 
EndpointAddress endpointAddress = new EndpointAddress(net.tcp:// localhost:8081 /+ typeof(TestObjectService).Name);
使用(ChannelFactory< ITestObjectService> channel = new ChannelFactory< ITestObjectService>(binding,endpointAddress))
{
ITestObjectService testObjectService = channel.CreateChannel();
testObjects = testObjectService.GetAll();
channel.Close();
}

我正在托管它:

 类型t​​ype = typeof(TestObjectService); 
ServiceHost host = new ServiceHost(type,
new Uri(http:// localhost:8080 /+ type.Name),
new Uri(net.tcp:// localhost :8081 /+ type.Name));
host.Open();

当使用调试时,它会从数据库中找到对象,但是它返回对象失败。



有什么想法可能会出错?

解决方案

这是一个痛苦的想法,但它是因为EntityFramework创建一个代理您的类。 TestObject类已经正确安装,但它正在创建一个名为TestObject_240F2B681A782799F3A0C3AFBE4A67A7E86083C3CC4A3939573C5410B408ECCE的类。

为了使ChannelFactory + WCF +实体框架全部协同工作,您必须进入您的Context构造函数并添加以下内容:

  ContextOptions.ProxyCreationEnabled = false; 

我希望这有助于别人。


I am attempting to retrieve a list of objects from Entity Framework via WCF, but am receiving the following exception:

There was an error while trying to serialize parameter http://tempuri.org/:GetAllResult. The InnerException message was 'Type 'System.Data.Entity.DynamicProxies.TestObject_240F2B681A782799F3A0C3AFBE4A67A7E86083C3CC4A3939573C5410B408ECCE' with data contract name 'TestObject_240F2B681A782799F3A0C3AFBE4A67A7E86083C3CC4A3939573C5410B408ECCE:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details.

I have used WCF in the past, but never with Entity Framework. I have all my entities generated via Entity Framework and are annotated with [DataContract] and [DataMember] attributes. I have no Navigation Properties on any of my entities.

The GetAll() method being called is in an abstract service class:

[ServiceContract]
public interface IService<T>
{
    [OperationContract]
    List<T> GetAll();
}

And I am using the ChannelFactory to call my implementation:

Binding binding = new NetTcpBinding();
EndpointAddress endpointAddress = new EndpointAddress("net.tcp://localhost:8081/" + typeof(TestObjectService).Name);
using (ChannelFactory<ITestObjectService> channel = new ChannelFactory<ITestObjectService>(binding, endpointAddress))
{
    ITestObjectService testObjectService = channel.CreateChannel();
    testObjects = testObjectService.GetAll();
    channel.Close();
}

I am hosting it as such:

Type type = typeof(TestObjectService);
ServiceHost host = new ServiceHost(type,
            new Uri("http://localhost:8080/" + type.Name),
            new Uri("net.tcp://localhost:8081/" + type.Name));
host.Open();

When using debugging, it finds the objects from the database, however, it is failing returning the objects.

Any ideas as to where I may be going wrong?

解决方案

This was a pain to figure out but it is because EntityFramework creates a 'proxy' of your class. The TestObject class I had was setup correctly, but it was creating a class called: TestObject_240F2B681A782799F3A0C3AFBE4A67A7E86083C3CC4A3939573C5410B408ECCE

To make the ChannelFactory + WCF + Entity Framework all work together, you must go into your Context constructor and add the following:

ContextOptions.ProxyCreationEnabled = false;

I hope this helps someone else.

这篇关于使用WCF 4.0的Entity Framework 4.0 DataContractSerializer错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆