0

I'm using the following code in my startup class to prevent errors serializing my entities which may cause circular references, but it is not working.

Why?

public partial class Startup
    {
        public static void ConfigureMobileApp(IAppBuilder app)
        {

            HttpConfiguration config = new HttpConfiguration();

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

            config.MapHttpAttributeRoutes();

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

            config.Formatters.JsonFormatter.SerializerSettings.Re‌​ferenceLoopHandling = ReferenceLoopHandling.Ignore;

            config.Services.Add(typeof(IExceptionLogger), new AiExceptionLogger());

            if (string.IsNullOrEmpty(settings.HostName))
            {
                app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions
                {
                    // This middleware is intended to be used locally for debugging. By default, HostName will
                    // only have a value when running in an App Service application.
                    SigningKey = ConfigurationManager.AppSettings["SigningKey"],
                    ValidAudiences = new[] { ConfigurationManager.AppSettings["ValidAudience"] },
                    ValidIssuers = new[] { ConfigurationManager.AppSettings["ValidIssuer"] },
                    TokenHandler = config.GetAppServiceTokenHandler()
                });
            }

            app.UseWebApi(config);
        }
    }
juliano.net
  • 7,982
  • 13
  • 70
  • 164

1 Answers1

0

According to your description, I created my Azure Mobile App project to test this issue. Based on your Startup.cs, I added my apiController as follows:

[MobileAppController]
public class ValuesController : ApiController
{
    [Route("api/values")]
    public HttpResponseMessage Get()
    {
        Department sales = new Department() { Name = "Sales" };
        Employee alice = new Employee() { Name = "Alice", Department = sales };
        sales.Manager = alice;
        return Request.CreateResponse(sales);
    }
}

public class Employee
{
    public string Name { get; set; }
    //[JsonIgnore]
    public Department Department { get; set; }
}

public class Department
{
    public string Name { get; set; }
    public Employee Manager { get; set; }
}

When access this endpoint, I encountered the following XML Circular Object References error:

enter image description here

Note: For a simple way, I removed the XML Formatter via config.Formatters.Remove(config.Formatters.XmlFormatter);. Also, you could refer to the section about preserving object references in XML from Handling Circular Object References.

After I removed XML Formatter, then I encountered the following error about object references loop in JSON:

enter image description here

Then, I followed this Loop Reference handling in Web API code sample, but without luck in the end. Also, I tried to create a new Web API project and found that ReferenceLoopHandling.Ignore could work as expected.

In the end, I found that if I remove the MobileAppController attribute from my apiController, then it could work as follows:

enter image description here

In summary, I assumed that you could try to ignore the reference attributes with the JsonIgnore for JSON.NET, for more details you could refer to fix 3:Ignore and preserve reference attributes.

Bruce Chen
  • 18,207
  • 2
  • 21
  • 35