0

I am trying to port a mobile service app to a web application. To do so I created a new web application and copied the relevant code from the working mobile service to the new web application that I created (using the mobile app template.
I have the following code in my Startup method in the new application:

 public partial class Startup
{
    public static void ConfigureMobileApp(IAppBuilder app)
    {
        HttpConfiguration config = new HttpConfiguration();

        new MobileAppConfiguration()
            .UseDefaultConfiguration()
            .ApplyTo(config);

        // Use Entity Framework Code First to create database tables based on your DbContext
        Database.SetInitializer(new MobileServiceInitializer());
        config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;


        config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize;
        config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
        app.UseWebApi(config);
        MobileAppSettingsDictionary settings = config.GetMobileAppSettingsProvider().GetMobileAppSettings();
   }
}

The config.Formatters were copied from the original application which returns the entity and its children to the json output of the api controller.

In the new application I had to add the [MobileAppController] to my api controllers. I get the following error from the controller in the web application app: Self referencing loop detected for property Teams (The model has Teams --> Players and players has a teamid)

Based on this detailed question: Self referencing loop detected - Getting back data from WebApi to the browser

The above code should work as it does in my mobile service app. The web service app appears to ignore the config.Formatters value as I have tried every value in the above question but I still get the same error.
If I place the [JSON Ignore] attribute before the Child list, then I do not get the error, but also do not get the children back in the json. How can I get this web application to accept the formatting values?

Community
  • 1
  • 1
Haim Katz
  • 455
  • 3
  • 15
  • 1
    Is this a dup of [Azure Mobile App customizing json serialization](https://stackoverflow.com/questions/36941834/azure-mobile-app-customizing-json-serialization) ? – dbc Jul 24 '16 at 10:59
  • 1
    Alternatively, you could add attributes to your classes directly, for instance [JsonPropertyAttribute.ItemIsReference](http://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_JsonPropertyAttribute_ItemIsReference.htm) to force `PreserveReferencesHandling` for collection items. – dbc Jul 24 '16 at 11:02
  • 2
    Thank you for your useful comments. Thank you @dbc for pointing me to this question. I spent 3 days looking and didn't find it. So apparently I was correct that the mobile app is ignoring the json formatters. You let me to this post [json net error]. Adding the attribute [JsonObject(IsReference = true)] before the DTOs solved the problem with minimum code. [json net error]:http://stackoverflow.com/questions/7397207/json-net-error-self-referencing-loop-detected-for-type/ – Haim Katz Jul 24 '16 at 13:41
  • @HaimKatz This should be an answer and not a comment... this was wrecking my head. – Inkers Dec 09 '16 at 22:30

1 Answers1

0

hank you for your useful comments. Thank you @dbc for pointing me to this question. I spent 3 days looking and didn't find it. So apparently I was correct that the mobile app is ignoring the json formatters. You let me to this post [json net error]. Adding the attribute [JsonObject(IsReference = true)] before the DTOs solved the problem with minimum code. [json net error]:stackoverflow.com/questions/7397207/… – Haim Katz Jul 24 at 13:41  

Haim Katz
  • 455
  • 3
  • 15