【发布时间】:2010-07-30 15:20:26
【问题描述】:
我正在尝试通过 WCF 从 Entity Framework 检索对象列表,但收到以下异常:
尝试序列化参数http://tempuri.org/:GetAllResult 时出错。该消息的InnerException是‘用数据合同名称‘TestObject_240F2B681A782799F3A0C3AFBE4A67A7E86083C3CC4A3939573C5410B408ECCE“类型’System.Data.Entity.DynamicProxies.TestObject_240F2B681A782799F3A0C3AFBE4A67A7E86083C3CC4A3939573C5410B408ECCE:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies’预计不会。考虑使用 DataContractResolver 或将任何静态未知的类型添加到已知类型列表中 - 例如,通过使用 KnownTypeAttribute 属性或将它们添加到传递给 DataContractSerializer 的已知类型列表中。有关详细信息,请参阅 InnerException。
我过去使用过 WCF,但从未使用过实体框架。我的所有实体都是通过 Entity Framework 生成的,并使用 [DataContract] 和 [DataMember] 属性进行了注释。我的任何实体都没有导航属性。
被调用的 GetAll() 方法在一个抽象服务类中:
[ServiceContract]
public interface IService<T>
{
[OperationContract]
List<T> GetAll();
}
我正在使用 ChannelFactory 来调用我的实现:
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();
}
我是这样托管的:
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();
使用调试时,它从数据库中查找对象,但是返回对象失败。
关于我可能出错的地方有什么想法吗?
【问题讨论】:
标签: .net entity-framework