Azure Mobile App customizing json serialization

一个人想着一个人 提交于 2019-12-06 03:46:34

It appears that Azure Mobile Apps aren't currently respecting serializer settings that are set in the OWIN startup class. I don't know if they're getting overwritten or just not used but they aren't getting picked up by the controllers.

As a workaround, it seems you can set the serializer settings from inside the controllers:

public class SomeController : ApiController
{
    public object Get()
    {
          SetSerializerSettings();
          Do your logic....
    }

    private void SetSerializerSettings()
    {
          this.Configuration.Formatters.JsonFormatter.SerializerSettings = 
              new JsonSerializerSettings
              {
                 Converters = { new StringEnumConverter { CamelCaseText = true }, },
                 ContractResolver = 
                       new CamelCasePropertyNamesContractResolver { IgnoreSerializableAttribute = true },
                 DefaultValueHandling = DefaultValueHandling.Ignore,
                 NullValueHandling = NullValueHandling.Ignore,
                 Formatting = Formatting.Indented
              };
    }

}

The Configuration property hasn't been set yet in the constructor so you can't put the SetSerializerSettings() there because it gets overwritten. And these settings seem to persist as long as the process is running, so this is a bit redundant, but it does seem to get the job done. I hope someone can come along and provide the right way to do it!

After spending a lot of time on this, I think the best you can do is create a MobileAppControllerConfigProvider and pass it to WithMobileAppControllerConfigProvider.

This is what I'm doing, to get all controllers to respect JsonConvert.DefaultSettings:

JsonConvert.DefaultSettings = () => new JsonSerializerSettings { /* something */ };

var provider = new MobileConfigProvider();

var config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
config.Formatters.Remove(config.Formatters.XmlFormatter);
config.Formatters.JsonFormatter.UseDataContractJsonSerializer = false;
config.Formatters.JsonFormatter.SerializerSettings = provider.Settings;

new MobileAppConfiguration().
    MapApiControllers().
    AddMobileAppHomeController().
    AddPushNotifications().
    WithMobileAppControllerConfigProvider(provider).
    ApplyTo(config);

And:

sealed class MobileConfigProvider : MobileAppControllerConfigProvider
{
    readonly Lazy<JsonSerializerSettings> settings = new Lazy<JsonSerializerSettings>(JsonConvert.DefaultSettings);

    public JsonSerializerSettings Settings => settings.Value;

    public override void Configure(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor)
    {
        base.Configure(controllerSettings, controllerDescriptor);
        controllerSettings.Formatters.JsonFormatter.SerializerSettings = Settings;
    }
}

As DevNoob's answer, the serializer settings in the OWIN startup class are not working. When the setting is in the Initialize(HttpControllerContext controllerContext) method in each contoller class, it works. In my case, I have the self referencing problem, so I have solved the problem like this:

public class CustomerController : TableController<Customer>
{
    protected override void Initialize(HttpControllerContext controllerContext)
    {

        controllerContext.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;

        base.Initialize(controllerContext);
        MyMobileAppContext context = new MyMobileAppContext();
        DomainManager = new EntityDomainManager<Customer>(context, Request);
    }

....

}

I suggest being careful with the answers provided here. Everything is fine until we use offline sync tables in iOS apps.

In my case they crashed without any good reason, most likely they need some non-default serializer setting to function properly. I used Nuno Cruses's solution and when I reverted all got back to normal.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!