【问题标题】:Error while using Linq "Include" method with Entity framework 4.1, Bug?在实体框架 4.1 中使用 Linq“包含”方法时出错,Bug?
【发布时间】:2011-11-08 18:32:41
【问题描述】:

我使用 SQL Azure 作为数据库,采用 Entity Framework 4.1 的代码优先技术。我正在从 WCF 作为中间层访问 EF,并提供对 ASP.NET MVC 3 的服务引用。

EF 中的关系场景是A->B->C,所以像往常一样,我试图让 A 包括 B 包括 C,但可能性很小:

db.A.Include("B").Include("B.C")
db.A.Include("B").Include("C")

但在服务本身运行完美。由于添加了对 Web 应用程序的引用,它会尝试序列化并在下面抛出异常。我尝试进行真假延迟加载也没有成功。

"The underlying connection was closed: The connection was closed unexpectedly"
Stack Trace found:
 at System.Net.HttpWebRequest.GetResponse() at
 System.ServiceModel.Channels.HttpChannelFactory.
     HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)

我访问了this page 解决,并在web.config 中添加了建议的属性。现在我突然收到错误:

服务器没有提供有意义的回复;这可能是由 合同不匹配、会话过早关闭或内部 服务器错误

我认为这可能是客户端和服务 web.configs 中的一些标签/属性不匹配的问题,但一切都在那里。我无法通过加载其整个 cllection 属性(从 wcf 服务到 Web 应用程序)传递对象列表的一件事是发现非常繁重且性能低下。仍然面临上述问题,以在上述架构中的 Web 应用程序中获取加载列表。身体能帮上忙吗....

【问题讨论】:

  • 显示错误信息怎么样?
  • 嘿,谢谢...我已经用错误消息编辑了我的问题。

标签: wcf serialization soap entity-framework-4.1 lazy-loading


【解决方案1】:

嘿,谢谢柯克·布罗德赫斯特 ,通过这个我可以跟踪问题。并在它周围搜索。我得到了解决方案。我在我的项目中添加了这个类

public class ReferencePreservingDataContractFormatAttribute : Attribute, IOperationBehavior
        {
            #region IOperationBehavior Members
            public void AddBindingParameters(OperationDescription description, BindingParameterCollection parameters)
            {
            }

            public void ApplyClientBehavior(OperationDescription description, System.ServiceModel.Dispatcher.ClientOperation proxy)
            {
                IOperationBehavior innerBehavior = new ReferencePreservingDataContractSerializerOperationBehavior(description);
                innerBehavior.ApplyClientBehavior(description, proxy);
            }

            public void ApplyDispatchBehavior(OperationDescription description, System.ServiceModel.Dispatcher.DispatchOperation dispatch)
            {
                IOperationBehavior innerBehavior = new ReferencePreservingDataContractSerializerOperationBehavior(description);
                innerBehavior.ApplyDispatchBehavior(description, dispatch);
            }


            public void Validate(OperationDescription description)
            {
            }
            #endregion
        }

        class ReferencePreservingDataContractSerializerOperationBehavior : DataContractSerializerOperationBehavior
        {
            public ReferencePreservingDataContractSerializerOperationBehavior(OperationDescription operationDescription) : base(operationDescription) { }
            public override XmlObjectSerializer CreateSerializer(Type type, string name, string ns, IList<Type> knownTypes)
            {
                return CreateDataContractSerializer(type, name, ns, knownTypes);
            }

            private static XmlObjectSerializer CreateDataContractSerializer(Type type, string name, string ns, IList<Type> knownTypes)
            {
                return CreateDataContractSerializer(type, name, ns, knownTypes);
            }

            public override XmlObjectSerializer CreateSerializer(Type type, XmlDictionaryString name, XmlDictionaryString ns, IList<Type> knownTypes)
            {
                return new DataContractSerializer(type, name, ns, knownTypes,
                0x7FFF /*maxItemsInObjectGraph*/,
                true/*ignoreExtensionDataObject*/,
                true/*preserveObjectReferences*/,
                null/*dataContractSurrogate*/);
            }
        }
    }

并添加了:[ReferencePreservingDataContractFormat] 作为我想使用“包含”的方法定义的属性。这对我有用。 这可能对其他有需要的人有所帮助。

【讨论】:

  • 值 0x7FFF=32767 (maxItemsInObjectGraph) 将覆盖 app.config 和/或 web.config 中的任何配置,请小心。
【解决方案2】:

这是一个非常常见的 WCF 异常,可以是任意数量的事物。 StackOverflow 上有数百次针对此问题的点击。

https://stackoverflow.com/search?q=%22The+underlying+connection+was+closed%22

您是 WCF 新手吗?任何服务调用都有效吗?它可能像超出 maxMessageSize 一样简单。

我建议您在您的服务上实现日志记录 - 在您的配置文件中添加一些诊断标签。

  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel"
              switchValue="Information, ActivityTracing"
              propagateActivity="true">
        <listeners>
          <add name="traceListener"
              type="System.Diagnostics.XmlWriterTraceListener"
              initializeData="c:\log\Traces.svclog"  />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-27
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    • 2018-05-08
    • 1970-01-01
    相关资源
    最近更新 更多