gpt4 book ai didi

WCF 从服务中读取数据问题

转载 作者:行者123 更新时间:2023-12-02 07:45:03 24 4
gpt4 key购买 nike

我想获得用户的通用列表 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com