【问题标题】:CommunicationException in WCFWCF 中的通信异常
【发布时间】:2010-05-19 16:02:24
【问题描述】:

我刚刚创建的 WCF 服务有问题。这是昨天的工作,但由于某种原因它刚刚停止工作。

我的一个 WCF 方法返回一个 Entity Framework 实体的数组,如下所示:

    public BranchContactDetail[] GetClosestBranches(string postcode, int howManyBranches)
    {
        GeoLocation geoLocation = GetLocationFromPostcode(postcode);
        Location location = new Location(geoLocation.Latitude, geoLocation.Longitude);

        using (BranchDirectoryEntities entities = new BranchDirectoryEntities())
        {
            var branchesInOrder = entities.BranchContactDetails
                .Where(b => b.latitude.HasValue && b.longitude.HasValue )
                .OrderBy(b => location.DistanceFrom(b.latitude, b.longitude))
                .Take(howManyBranches)
                .ToArray();

            return branchesInOrder;
        }
    }

...而且,正如我所说,昨天运行良好。现在我收到“底层连接已关闭:连接意外关闭”。我在网上搜索过,但似乎没有人知道答案。有人对这个问题有所了解吗?

问候,马克

【问题讨论】:

    标签: wcf exception entity-framework-4


    【解决方案1】:

    与昨天相比,您今天选择的条目可能更多吗?可能是您的服务方法返回数据的时间超过了默认的 60 秒?还是返回的实体的数据大小超过了 64K?

    我会做两件事:

    1) 打开异常详细信息,这样您就可以在客户端上获得详细的异常消息 - 希望这会为您指明正确的方向

    2) 打开 WCF 消息日志记录以查看传输的内容

    对于第 1 点,您需要启用 serviceDebug 行为:

      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior name="debug">
              <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <services>
          <service behaviorConfiguration="debug" name="YourWCFService">
    

    当呼叫失败时,这应该会在客户端中为您提供详细信息。

    对于点号。 2、你需要做几个步骤:

    &lt;system.serviceModel&gt;内部,需要添加这个诊断标签:

    <diagnostics>
      <messageLogging
          logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true"
          logEntireMessage="true" logMalformedMessages="true"
          maxMessagesToLog="2500" maxSizeOfMessageToLog="256000" />
    </diagnostics>
    

    然后您还需要将其添加到您的 app.config 或 web.config:

      <system.diagnostics>
        <sources>
          <source name="System.ServiceModel"
                        switchValue="Information, ActivityTracing"
                        propagateActivity="true">
            <listeners>
              <add name="default"
                   type="System.Diagnostics.XmlWriterTraceListener"
                   initializeData="C:\yourlogfile.svclog" />
            </listeners>
          </source>
        </sources>
        <trace autoflush="true" />
      </system.diagnostics>
    

    System.Diagnostics 命名空间中有几个预定义的跟踪侦听器类型 - 使用任何现成的,或创建自己的(例如,登录到数据库等)。

    查看有关如何在 WCF 中启用跟踪的其他信息源:

    您可以使用WCF Trace Viewer Tool 查看那些基于 XML 的 svclog 文件 - 非常方便!

    【讨论】:

      【解决方案2】:

      很可能您有连接问题。我的意思是您无权访问您尝试访问的资源。有没有防火墙什么的。 请务必尝试从客户端计算机远程登录服务器。

      【讨论】:

      • 不,我不会返回更多结果。事实上,在测试中,我返回的实体更少——只有 5 个。不同的是,Branch 实体有更多的依赖项——外键等等。 WCF 可能会遇到这些问题吗?
      • 我没有说它可以连接到结果大小。这可能是连接问题。请阅读以上内容。
      • 我已经按照您的建议添加了诊断内容 marc_s,并且我已经阅读了正在生成的日志,但我并不明智。我可以看到异常实际上是在哪里引发的,但我不明白为什么!啊!!
      • 我的预感是正确的。在返回之前,我在每个实体上尝试了 context.Detach(branch) ,并且不再抛出异常。此刻我很高兴。
      猜你喜欢
      • 2011-10-17
      • 1970-01-01
      • 2011-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多