C#, Entity Framework, WCF and JSON serialization

Ever tried to serialize Entity Framework with WCF?
Have you tried it with the Lazy-Initialization feature?
And how about the Proxy Creation Enabled?

There's a problem of circular reference if one table is referencing another table - which referencing the first one.

We have had a great deal of problems with it. We even tried to consult an external expert of Entity Framework who simply told us it's impossible to serialize it on WCF.
oh darn... :(

We tried many solutions before we gave up:
like that one:
http://geekswithblogs.net/danemorgridge/archive/2010/05/04/entity-framework-4-wcf-amp-lazy-loading-tip.aspx

and that:
http://blogs.microsoft.co.il/blogs/gilf/archive/2011/10/17/avoiding-circular-reference-for-entity-in-json-serialization.aspx

We even used t4templates to create a whole new set of flattened objects (!!!)
But it doesn't really make life easier as one has to load separately each and every object.

But this was not what we were looking for. We needed a solution that finds the spanning tree of a graph and serializing it into JSON.

Eventually the solution unexpectedly arise in the form of the open source JSON Serialization for .NET:
http://json.codeplex.com/

One of the features of this excellent util is to keep track of the objects which were already serialized in order not to re-serialize it again.
I was ready to implement it myself as I found out it already exists - which saved me a lot of time and work.

So, I would like to thank these guys who made this excellent code.

Comments

Rich Sorensen said…
I am currently trying to implement a WCF rest-based web service to host EF, and am struggling with the same problem.

However, the Json.Net documentation does not cover how to implement it in an environment such as ours. Here are the specifics:

1.EF 6.1.1, Framework 4.5, VS 2012.

2.A C# WCF Rest-based web service (not using WinAPI) backending a Winforms app.

3.The Winforms client currently uses the HttpWebRequest and HttpWebResponse classes to communicate with the web service.

4.The EF classes must retain their current foreign-key record buffer properties (implemented as ICollection objects in our case), as I need to support Linq ".Include()" statements in some of our Linq-to-Sql DAL methods. Lazy Loading is turned off, and other minor changes in the EF class objects can be made as long as the record buffer functionality is preserved

Questions:

1.Is replacing MS Json with Json.Net in the above environment feasible?

2.If so, what changes to the app.config and web.config files should be made, and what logic should be added on the client and server sides?

Popular posts from this blog

pip install pymssql fails with 'sqlfront.h': No such file or directory

ASP.NET 2.0 with MySQL 4.1.2 - unicode problem

NLP Toolkit (Made for self use - but feel free to use it too :) )