wcfserializationwebhttp

WCF WebHttp & Cycles Preventing Serialization


I have created a WCF WebHttp Service using the following article as a guideline..

http://blogs.msdn.com/b/endpoint/archive/2010/01/07/getting-started-with-wcf-webhttp-services-in-net-4.aspx

I have everything working great except one part. When I query to get a list of Contacts from my database it returns them fine. However, when I use the same code structure to get a list of Accounts it tells me:

This webpage is not available

However, I can query a single Account no problem. At first I thought it might be a dependency thing where it's trying to load all the child objects on Account and so forth. But when I inspect the result coming back from the database it's a very small set of data. (42 records) So I thought it might have to do with serialization of the objects, so I did my own little Json serializer method to a file and got the following error.

Object graph for type 'MyNamespace.Model.Account' contains cycles and cannot be serialized if reference tracking is disabled.

So I did a search on the web and all the results are pointing to a singular article..

http://blogs.msdn.com/b/sowmy/archive/2006/03/26/561188.aspx

However, this article doesn't help me as it is not valid for WCF WebHttp services.

Any thoughts?


Solution

  • That article is valid for the WCF WebHttp services as well - you just need to get a reference to the ServiceHost for those services. In the example of that blog post, the service is being activated using the ASP.NET routes, and the WebServiceHostFactory, which is the component which creates the ServiceHost instance to be used by that service.

    So you'd have to create your own factory (possibly overriding WebServiceHostFactory), and on CreateServiceHost you'd call the base method to get the host, modify it according to Sowmy's article, then return it.

    I've recently posted an article about service host factories at http://blogs.msdn.com/b/carlosfigueira/archive/2011/06/14/wcf-extensibility-servicehostfactory.aspx, where you can find more information about this component.