WCF中的通信异常
我刚创建的WCF服务有问题。这是工作昨天,但由于某种原因,它只是停止工作 我的一个WCF方法返回实体框架实体的数组,如下所示:WCF中的通信异常,wcf,exception,entity-framework-4,Wcf,Exception,Entity Framework 4,我刚创建的WCF服务有问题。这是工作昨天,但由于某种原因,它只是停止工作 我的一个WCF方法返回实体框架实体的数组,如下所示: public BranchContactDetail[] GetClosestBranches(string postcode, int howManyBranches) { GeoLocation geoLocation = GetLocationFromPostcode(postcode); Location loca
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;
}
}
…而且,正如我所说的,昨天这一切都很顺利。现在我得到了一个“底层连接被关闭:连接意外关闭”的答案。我在网上搜寻过,但似乎没有人知道答案。有人能解释一下这个问题吗
您好,Mark您很可能有连接问题。我的意思是你无法访问你试图访问的资源。有防火墙之类的吗。
请务必尝试从客户端计算机远程登录服务器。可能是您今天选择的条目比昨天多得多吗?您的服务方法返回数据的时间是否会超过默认值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点,您需要执行以下几个步骤:
在
中,您需要添加此诊断标签:
<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>
您可以使用-非常方便 不,我不会返回更多结果。事实上,在测试中,我只返回了5个实体。不同的一点是,分支实体具有更多的依赖项、外键等等。WCF可能有这些问题吗?我没说它可以连接到结果大小。这可能是连接问题。请阅读上面的内容。我已经按照你建议的marc_s添加了诊断内容,我已经阅读了正在生成的日志,但我一点也不知道。我可以看到异常实际被抛出的位置,但我不知道为什么!啊!!我的直觉是正确的。在返回每个实体之前,我尝试了context.Detach(branch),并且不再抛出异常。这是我目前的快乐。