Entity Framework and WCF – Three ways to play nice together

Standard

Below are the three common issues that always seem to initially catch me out on projects that involve Entity Framework and WCF. They usually manifest themselves with the really helpful error message “The underlying connection was closed…”. You can spin up WCF tracing but generally they all relate to an issue with the serialisation of the types which should be fixed by one of the following:

1) Lazy Loading – Turn it off (YourContext.ContextOptions.LazyLoadEnabled == false).
2) Dynamic Proxies – They don’t play nice over the wire – Turn these off too (ContextOptions.ProxyCreationEnabled == false).
3) Cyclic references – This one is a bit more complicated but nothing that can’t be fixed through a custom attribute on your Operation or Service Contracts. It’s explained in excellent detail with full source code over here.

Remember folks the best way to mitigate these issues is “Integrate early. Integrate often.”

Advertisements