Samples not working for Web API Help Page when using an Entity Framework Complex Type in the model

asp.net-web-api entity-framework serialization

Question

I have an EF code first model that includes some Complex Types. Generating Web API controllers works without any problem, but when generating the Web API Help Page as described in http://blogs.msdn.com/b/yaohuang1/archive/2012/08/15/introducing-the-asp-net-web-api-help-page-preview.aspx then the samples don't get generated. The error message given is :

An exception has occurred while using the formatter 'JsonMediaTypeFormatter' to generate sample for media type 'application/json'. Exception message: One or more errors occurred.

The same thing happens for the rest of the media types. I know there was a problem some time ago with complex types and serialization but it was solved when the Json.NET formatter was included by default in Web API.

Could this be related? Anyone else having the same problem? Any ideas?

1
14
2/22/2013 5:09:18 PM

Accepted Answer

My guess is that this is most probably due to the referencing loops that your model has. For example, can you give a try in generating the help page by making the following change to the Json formatter. you should see the sample getting generated:

        config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize; 
        config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;

Also, you should see similar error even during runtime actually if you do not make the above settings.

For XmlFormatter and self referencing loops, you can look here.

32
2/23/2013 7:15:44 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow