Tech Talk A software developer on all things tech, and then some

29Nov/110

Error While Sending a Class Collection Over a WCF Service

I had spent a great while debugging an operation which returned a collection an entity object. However, I had a bug in my code and I could not find what was causing it. I was given the following message over, and over, and over.

The socket connection was aborted. This could be caused by an error processing your message
or a receive timeout being exceeded by the remote host, or an underlying network resource issue.
Local socket timeout was '00:00:59.4260000'."}


I decided to change up the calls to asynchronous to see if I could change up the error message a little bit and get a better clue. To do this, add an event handler for when the operation completes, and hook it up.

private void btnGrabNewOrders_Click(object sender, RoutedEventArgs e)
{
    try
    {
         ProxyService.GrabNewOrdersCompleted += new EventHandler(ProxyService_GrabNewOrdersCompleted);
         ProxyService.GrabNewOrdersAsync(ComputerID,CompanyID);
    }
    catch (Exception ex)
    {
         MessageBox.Show("Error grabbing the orders" + Environment.NewLine + Environment.NewLine + ex.Message);
    }
}

private void ProxyService_GrabNewOrdersCompleted(object sender, GrabNewOrdersCompletedEventArgs e)
{
    try
    {
         List Orders = e.Result.ToList();
         MessageBox.Show("Order Count : " + Orders.Count().ToString());
    }
    catch (TargetInvocationException ex)
    {
         MessageBox.Show("Error grabbing the orders" + Environment.NewLine + ex.Message);
    }
}

Then got this error message which was as vauge :

The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error.

I then went searching for answers again when I found a useful tool in my debugging, the trace XML stuff going on. To do this I did two things. Create a folder named log under the C directory on my server. I then added the following code to my App.config under the <configuration> section.

<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Error, Critical" propagateActivity="true">
<listeners>
<add name="traceListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData= "c:\log\WCF_Errors.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>

Alas, I found the issue. This was the first time I had created a WCF operation, data contract, etc... and had gotten my serializable tags mixed up with my data contracts, giving me a huge head ache. Long story short, I had set all my entity models to [serializable] which would work fine for me if they weren't entity objects. Since they contained the foreign key fix up and collections which reference their parent, it was creating a cycle within WCF, not allowing the serialization and deserialization. To get around this, I added [DataContract] to all my models instead of the serialization. I then added [DataMember] to all the properties I needed to send, and everything worked like a charm sending accross the wire with both my collection of my order object and a singular item.

Side Note : Another great debugging trick you can do is enable the errors to be displayed in your app config. I talk about how to do that here.

Comments (0) Trackbacks (0)

No comments yet.


Leave a Reply

No trackbacks yet.

%d bloggers like this: