Geeks With Blogs

News Google

Nick Harrison Blog<Nick>.Next()

I recently had to introduce a DTO pattern on a project. The application was split between a server side and a desktop side using http based WCF to handle the communication.

The project had been working well passing NHibernate entities back and forth, but there was too much data being transferred.

So we introduced the DTO pattern. I added a GetDto method to the base class for the Entities. I added a GetEntity to the base class for the DTOs. We created DTO objects for the entities that we wanted to switch over.

We spent a little bit of time debugging and working out the nuances in the GetDto and GetEntity methods. Everything was working fine. We modified the service classes and proxies to expect the DTO instead of the Entity. Everything is looking fine.

Now we try to pass data back and forth …

"The connection was closed unexpectedly"


We start debugging. We can step into the code on the desktop side. Everything looks fine. We step into the code on the server side. Everything looks fine. We finish the method on the server side, and we are slapped in the face with "The connection was closed unexpectedly".

I search all over the place for possible causes. I blame WCF configuration. I blame the alignment of the stars. I blame the weather. I blame the driver who cut me off in traffic.

Stubbornly, I refuse to blame the DTOs. This is a simple pattern. I have used it before to solve this very problem. Surely they cannot be the problem.

I switch back to passing Entities. The closing connection problem goes away. This lets everyone off the hook. Except for that driver. I will find a way to blame him yet.

So it has to be a problem with the DTO. I am stumped…

I go back to looking at the Entity and comparing it to the DTO. Both have the DataContract attribute. Both have DataMember attributes for every property. They do not appear to be any meaningful differences I explicitly call serialize on the DTO. It serializes without a problem.

I am still stumped. On a lark, we decide to try changing the base class of the DTO to match the base class of the Entity.

This is when we discover that EntityBase has a DataContract attribute, and DTOBase does not. We make the change.

The clouds part. The angels sing! Amazingly problem solved!

This should not have caused the problem. The error message should have been much better and definitely point us in a good direction, but what a relief that we figured it out.

Hopefully this will save someone else some trouble shooting time.

Now what can I blame on that driver …



Posted on Friday, July 16, 2010 4:26 PM | Back to top

Comments on this post: Error Serializing Data after Switching from an Entity to a DTO

# re: Error Serializing Data after Switching from an Entity to a DTO
Requesting Gravatar...
So bit late discovering your blog article, it could really save my time... I understand your feelings described above :)
Left by Viktor on Oct 18, 2010 7:13 AM

comments powered by Disqus

Copyright © Nick Harrison | Powered by: