views:

23

answers:

2

Hi

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

A: 

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.

Incognito
No, I am not returning more results. In fact in testing, I'm returning fewer-just 5 entities.One thing that is different is that the Branch entity has a lot more dependencies-foreign keys and so on. Might WCF have trouble with these?
I didn't say it can be connected to result size. It can be connectivity issue. Please read above.
Incognito
I've added the diagnostic stuff as you suggest marc_s, and I've read through the logs being produced but I'm none the wiser. I can see where the exception is actually being thrown, but I can't see why! Aargh!!
My hunch was correct. I tried context.Detach(branch) on each entity before returning it, and the exception is no longer being thrown. That's me happy for the moment.
A: 

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!

marc_s