我试图找回通过WCF从实体框架对象的列表,但我收到以下异常:
I am attempting to retrieve a list of objects from Entity Framework via WCF, but am receiving the following exception:
有试图序列参数 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了解更多详情。
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.
我已经使用WCF过去,但从来没有与实体框架。我已通过实体框架生成所有的实体,并注有[DataContract]和[的DataMember]属性。我没有导航属性上我的任何实体。
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.
在GETALL()方法被称为是一个抽象服务类:
The GetAll() method being called is in an abstract service class:
[ServiceContract]
public interface IService<T>
{
[OperationContract]
List<T> GetAll();
}
和我使用的ChannelFactory调用我的实现:
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?
这是一个痛苦弄清楚,但是那是因为的EntityFramework创建类的代理。该的TestObject类我是正确设置,但它创建了一个名为:TestObject_240F2B681A782799F3A0C3AFBE4A67A7E86083C3CC4A3939573C5410B408ECCE
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
要进行的ChannelFactory + WCF + Entity Framework的共同努力,你必须进入你的上下文构造,并添加以下内容:
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.