ReferenceLoopHandling.Ignore 无法让 Azure 移动应用服务忽略循环引用的序列化
ReferenceLoopHandling.Ignore not working for Azure Mobile App Service to ignore serialization of circular references
我在我的启动程序中使用以下代码 class 来防止序列化我的实体时可能导致循环引用的错误,但它不起作用。
为什么?
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.ReferenceLoopHandling = 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);
}
}
根据您的描述,我创建了我的 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 循环对象引用错误:
注意: 为了简单起见,我通过 config.Formatters.Remove(config.Formatters.XmlFormatter);
删除了 XML 格式化程序。此外,您还可以参考 Handling Circular Object References.
中关于在 XML 中保留对象引用的部分
在我删除 XML Formatter 之后,我在 JSON 中遇到了以下关于对象引用循环的错误:
然后,我按照这个 Loop Reference handling in Web API 代码示例进行操作,但最终没有运气。另外,我尝试创建一个新的 Web API 项目,发现 ReferenceLoopHandling.Ignore
可以按预期工作。
最后,我发现如果我从我的文件中删除 MobileAppController
属性
apiController
,那么它可以工作如下:
综上所述,我假设您可以尝试使用 JsonIgnore 忽略引用属性 JSON.NET,有关更多详细信息,您可以参考 fix 3:Ignore and preserve reference attributes.
我在我的启动程序中使用以下代码 class 来防止序列化我的实体时可能导致循环引用的错误,但它不起作用。
为什么?
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.ReferenceLoopHandling = 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);
}
}
根据您的描述,我创建了我的 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 循环对象引用错误:
注意: 为了简单起见,我通过 config.Formatters.Remove(config.Formatters.XmlFormatter);
删除了 XML 格式化程序。此外,您还可以参考 Handling Circular Object References.
在我删除 XML Formatter 之后,我在 JSON 中遇到了以下关于对象引用循环的错误:
然后,我按照这个 Loop Reference handling in Web API 代码示例进行操作,但最终没有运气。另外,我尝试创建一个新的 Web API 项目,发现 ReferenceLoopHandling.Ignore
可以按预期工作。
最后,我发现如果我从我的文件中删除 MobileAppController
属性
apiController
,那么它可以工作如下:
综上所述,我假设您可以尝试使用 JsonIgnore 忽略引用属性 JSON.NET,有关更多详细信息,您可以参考 fix 3:Ignore and preserve reference attributes.