WCF中的CommunicationException

时间:2010-05-19 16:02:25

标签: wcf exception entity-framework-4

我刚刚创建的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;
        }
    }

......而且,正如我所说,这昨天工作正常。现在我得到一个“基础连接已关闭:连接意外关闭。”我已经在网上搜索过,但似乎没有人知道答案。有人对这个问题有所了解吗?

问候,马克

2 个答案:

答案 0 :(得分: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,你需要做几个步骤:

<system.serviceModel>内,您需要添加此诊断代码:

<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文件 - 非常方便!

答案 1 :(得分:0)

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