serlingpa
serlingpa

Reputation: 12660

CommunicationException in WCF

I have a problem with a WCF Service I've just created. This was working yesterday but for some reason it's just stopped working.

One of my WCF methods returns an array of an Entity Framework entity, like this:

    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 say, this was working fine yesterday. Now I'm getting a "The underlying connection was closed: The connection was closed unexpectedly." I've hunted all over the web but no-one seems to know the answer. Anyone shed any light on this issue?

Regards, Mark

Upvotes: 2

Views: 1228

Answers (2)

marc_s
marc_s

Reputation: 754220

Could it be that you're selecting a lot more entries today compared to yesterday? Could it be that your service method takes longer than the default of 60 seconds to return the data? Or could it be that the data size is going beyond 64K for the entities returned?

I would do two things:

1) Turn on the exception details, so that you can get a detailed exception message on the client - that should hopefully point you in the right direction

2) Turn on WCF message logging to see what goes across the wire

For point 1, you need to enable the serviceDebug behavior:

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

This should give you details in the client, when a call fails.

For point no. 2, you need to do a few steps:

Inside <system.serviceModel>, you need to add this diagnostics tag:

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

and then you also need to 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 are a couple of predefined trace listener types in the System.Diagnostics namespace - use any of those ready-made ones, or create your own (e.g. to log to a database or such).

Check out those additional information sources on how to enable tracing in WCF:

You can view those XML-based svclog files using the WCF Trace Viewer Tool - very handy!

Upvotes: 1

Incognito
Incognito

Reputation: 16577

Most probably you have connection problem. I mean you have no access to the resource you are trying to access. Is there any firewall or something. To be sure try to telnet the server from the client machine.

Upvotes: 0

Related Questions