2017-05-10 10 views
0

원형 참조를 유발할 수있는 엔티티를 직렬화하는 동안 발생하는 오류를 방지하기 위해 시작 클래스에서 다음 코드를 사용하지만 동작하지 않습니다.ReferenceLoopHandling.Ignore가 순환 참조의 직렬화를 무시하는 Azure Mobile App 서비스에서 작동하지 않음

왜?

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); 
     } 
    } 

답변

0

설명에 따르면이 문제를 테스트하기 위해 Azure Mobile App 프로젝트를 만들었습니다. 다음과 같이 Startup.cs을 바탕으로, 나는 나의 apiController을 추가 :

[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; } 
} 

접근이 엔드 포인트, 나는 다음과 같은 XML 원형 개체 참조 오류가 발생하는 경우 : 간단한 들어 :

enter image description here

주 그런데 config.Formatters.Remove(config.Formatters.XmlFormatter);을 통해 XML 포맷터를 제거했습니다. 또한 XML의 객체 참조 보존에 대한 섹션을 Handling Circular Object References에서 참조 할 수 있습니다. 내가 XML 포맷터를 제거 후

, 그럼 내가 JSON에서 개체 참조 루프에 대한 다음과 같은 오류가 발생

enter image description here

그런 다음,이 Loop Reference handling in Web API 코드 예제를 따라했지만 결국 운이없이. 또한 새로운 웹 API 프로젝트를 만들려고했는데 ReferenceLoopHandling.Ignore이 예상대로 작동 할 수 있다는 것을 알았습니다. 난 당신이 무시을 시도 할 수 있다고 가정, 요약

enter image description here

: 결국

, 나는 내 apiController에서 MobileAppController 속성을 제거하면 다음과 같이 다음이 일할 수있는 것을 발견 JSON.NET 용 JsonIgnore을 가진 참조 속성. 자세한 내용은 fix 3:Ignore and preserve reference attributes을 참조하십시오.