Exception communication in WCF
I have a problem with a WCF service I just created. It worked yesterday, but for some reason it just stopped working.
One of my WCF methods returns an array of Entity Framework object, e.g .:
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;
}
}
... and as I said, it worked fine yesterday. Now I get "Connected connection was closed: connection was closed unexpectedly". I've hunted all over the web, but no one seems to know the answer. Does anyone shed some light on this issue?
Regards, Mark
a source to share
Maybe you are selecting a lot more entries today compared to yesterday? Maybe your service method takes more than 60 seconds to return data? Or could it be that the data size exceeds 64K for the returned objects?
I would do two things:
1) Include exception details so you can get a detailed exception message on the client - this will hopefully point you in the right direction
2) Turn on WCF logging to see what's going on across the wire
For point 1, you need to enable the behavior serviceDebug
:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="debug">
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="debug" name="YourWCFService">
This should provide you with details in the client when the call fails.
For a point, no. 2, you need to take a few steps:
Inside, <system.serviceModel>
you need to add this diagnostic tag:
<diagnostics>
<messageLogging
logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true"
logEntireMessage="true" logMalformedMessages="true"
maxMessagesToLog="2500" maxSizeOfMessageToLog="256000" />
</diagnostics>
and then you should also add this to your app.config or 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>
There System.Diagnostics
are several predefined trace listener types in the namespace - use any of these pre-built ones or create your own (for example, for database logins or such)
Check out these additional sources of information on how to enable tracing in WCF:
You can view these XML based svclog files using the WCF Trace Viewer - very handy!
a source to share