JsonSerializer RRS feed

  • Question

  • User656510293 posted

    I am confused about how to setup custom json-serializer settings. I cannot override JsonSerializer & getting it to work but I seem to be able to modify the existing, this to me indicates timing issue or signalr simply ignores the overriden one. A note is that the custom contract resolver defined in the json serializer settings is called in the none working scenario but its only called for signalr members/classes & not for my members.

    Scenario is self-hosted signalr

    <package id="Microsoft.Owin" version="4.0.0" targetFramework="net462" />
    <package id="Microsoft.Owin.SelfHost" version="4.0.0" targetFramework="net462" />
    <package id="Microsoft.AspNet.SignalR.Core" version="2.2.3" targetFramework="net462" />
    <package id="Microsoft.AspNet.SignalR.SelfHost" version="2.2.3" targetFramework="net462" />

            public void Configuration(IAppBuilder app)
                var config = new HttpConfiguration();
                this.ConfigureWebApi(app, config);
            private void ConfigureBehaviors(HttpConfiguration config)
                config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
                var serializerSettings = OwinStartup.JsonSerializerSettings.Value;
                config.Formatters.JsonFormatter.SerializerSettings = serializerSettings;
                //JsonConvert.DefaultSettings = () => { return serializerSettings; };
                // WORKS
                var serializer = GlobalHost.DependencyResolver.GetService(typeof(JsonSerializer)) as JsonSerializer;
                serializer.Formatting = serializerSettings.Formatting;
                serializer.NullValueHandling = serializerSettings.NullValueHandling;
                serializer.ContractResolver = serializerSettings.ContractResolver;
                // Does not work (but if retriving it & manually serializing e.g. inside hub it correct)
                //GlobalHost.DependencyResolver.Register(typeof(JsonSerializer), () => JsonSerializer.Create(serializerSettings));

    Wednesday, January 23, 2019 7:18 PM


All replies