WCF 从服务中读取数据问题

标签 wcf xml-serialization entity-framework-4.1

我想获得用户的通用列表 List<User>从我的 Web 服务到我的 Web 应用程序的对象。可能这是常见问题。我搜索了很多,但每个链接都有不同的补救措施。所以最后我决定问问这里的伙计们……无论如何

我正在使用控制台应用程序从我托管的 wcf 网络服务中获取数据。我的 WCf Web 服务具有 Entity Framework 4.1。我正在使用其模型类中的对象。当我尝试向我的 Web 应用程序添加服务引用时,它通常会在 Web 应用程序中生成代理。我能够发布数据意味着我可以创建用户。但是虽然 getusers这是返回 List ,得到以下异常......:

Exception:
An error occurred while receiving the HTTP response to http://myserver/AdminService/MyAdminService.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.

Inner Exception is :
The underlying connection was closed: An unexpected error occurred on a receive

and inner-inner exception is : Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

with message : An existing connection was forcibly closed by the remote host
Error Code :10054

Where as Event log saying :
A message was not logged.
Exception: System.ServiceModel.CommunicationException: There was an error while trying to serialize parameter http://tempuri.org/:getUsersResult. The InnerException message was 'Type 'System.Data.Entity.DynamicProxies.User_00DEC686D7E21DB0D84B595F647A03FFB4943938F76E8C3DBBE0F77F8BC29A1D' with data contract name 'User_00DEC686D7E21DB0D84B595F647A03FFB4943938F76E8C3DBBE0F77F8BC29A1D: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. ---> System.Runtime.Serialization.SerializationException: Type 'System.Data.Entity.DynamicProxies.User_00DEC686D7E21DB0D84B595F647A03FFB4943938F76E8C3DBBE0F77F8BC29A1D' with data contract name 'User_00DEC686D7E21DB0D84B595F647A03FFB4943938F76E8C3DBBE0F77F8BC29A1D: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. at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declaredType) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerializeReference(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle) at WriteArrayOfUserToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , CollectionDataContract ) at System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context) at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.XmlObjectSerializer.WriteObject(XmlDictionaryWriter writer, Object graph) at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameterPart(XmlDictionaryWriter writer, PartInfo part, Object graph) --- End of inner exception stack trace --- at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameterPart(XmlDictionaryWriter writer, PartInfo part, Object graph) at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameter(XmlDictionaryWriter writer, PartInfo part, Object graph) at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeBody(XmlDictionaryWriter writer, MessageVersion version, String action, MessageDescription messageDescription, Object returnValue, Object[] parameters, Boolean isRequest) at System.ServiceModel.Dispatcher.OperationFormatter.OperationFormatterMessage.OperationFormatterBodyWriter.OnWriteBodyContents(XmlDictionaryWriter writer) at System.ServiceModel.Channels.BodyWriter.WriteBodyContents(XmlDictionaryWriter writer) at System.ServiceModel.Channels.Message.ToString(XmlDictionaryWriter writer) at System.ServiceModel.Diagnostics.MessageLogTraceRecord.WriteTo(XmlWriter writer) at System.ServiceModel.Diagnostics.MessageLogger.LogInternal(MessageLogTraceRecord record) at System.ServiceModel.Diagnostics.MessageLogger.LogMessageImpl(Message& message, XmlReader reader, MessageLoggingSource source) at System.ServiceModel.Diagnostics.MessageLogger.LogMessage(Message& message, XmlReader reader, MessageLoggingSource source) Process Name: w3wp Process ID: 5928

这个问题应该是什么。我和团队从 3 天开始就在寻找这个。但不幸的是到目前为止还没有克服......

我尝试了很多东西,比如添加可序列化属性、端点修改……我记不起所有的了:)……也许你可以给我们正确的方向……

最佳答案

原因是 EF 类在运行时默认代理以支持延迟加载和动态更改跟踪。所以你不序列化 User 类,而是在运行时从 User 派生的类。 WCF 不喜欢这样。在您的上下文中关闭代理创建。

context.Configuration.ProxyCreationEnabled = false;

关于WCF 从服务中读取数据问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7643875/

相关文章:

c# - 为什么 XmlSerializer 将垃圾值添加到根元素的结束标记?

c# - 从 C# 调用 OData 服务

wcf - 通过 WCF 中的 HTTP Post 接受表单字段

c# - 从对象创建 Xml 文件

foreign-keys - EF 4.1 Code First 出现无效列名错误

c# - 无法在 LINQ to Entities 查询中构造实体或复杂类型

c# - Entity Framework 代码优先 - 无法在对象 'db' 中插入重复键

c# - WCF MemoryStream 返回类型是否可以写入 Http Response 对象以作为 Excel 文件下载?

wcf - 数据契约和动态返回类型 WCF

python - 我将如何着手构建 API 转换器?