SignalR 2.0.0 beta2 IJsonSerializer extensibility

前端 未结 3 990
攒了一身酷
攒了一身酷 2021-01-13 10:05

I want to add some custom serialization logic so that the converted json contains camel case properties.

For that reason i tried to replace the default IJsonSeriali

3条回答
  •  萌比男神i
    2021-01-13 10:56

    Here's an example of overriding the SignalR Dependency Resolver using StructureMap. In this particular example, I'm converting to camelCase properties and converting enums as strings.

    During startup:

    Microsoft.AspNet.SignalR.GlobalHost.DependencyResolver = new StructureMapSignalRDependencyResolver();
    

    Here's the class:

    public class StructureMapSignalRDependencyResolver : Microsoft.AspNet.SignalR.DefaultDependencyResolver
    {
        public override object GetService(Type serviceType)
        {
            object service;
            if (!serviceType.IsAbstract && !serviceType.IsInterface && serviceType.IsClass)
            {
                // Concrete type resolution
                service = StructureMap.ObjectFactory.GetInstance(serviceType);
            }
            else
            {
                // Other type resolution with base fallback
                service = StructureMap.ObjectFactory.TryGetInstance(serviceType) ?? base.GetService(serviceType);
            }
    
            return service;
        }
    
        public override IEnumerable GetServices(Type serviceType)
        {
            var objects = StructureMap.ObjectFactory.GetAllInstances(serviceType).Cast();
            return objects.Concat(base.GetServices(serviceType));
        }
    }
    
    
    

    And StructureMap was setup with:

    ObjectFactory.Configure(c =>
        {
            c.Scan(a =>
                {
                    // scan the assembly that SignalR is referenced by
                    a.AssemblyContainingType(); 
                    a.WithDefaultConventions();
                });
    
            c.For()
             .Singleton()
             .Use(new Newtonsoft.Json.JsonSerializer 
                 { 
                     ContractResolver = new SignalRContractResolver(), 
                     Converters = { new Newtonsoft.Json.Converters.StringEnumConverter() } 
                 });
        });
    

    Here is the Contract Resolver:

    public class SignalRContractResolver : Newtonsoft.Json.Serialization.IContractResolver
    {
        private readonly Assembly _assembly;
        private readonly Newtonsoft.Json.Serialization.IContractResolver _camelCaseContractResolver;
        private readonly Newtonsoft.Json.Serialization.IContractResolver _defaultContractSerializer;
    
        public SignalRContractResolver()
        {
            _defaultContractSerializer = new Newtonsoft.Json.Serialization.DefaultContractResolver();
            _camelCaseContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
            _assembly = typeof(Connection).Assembly;
        }
    
        public Newtonsoft.Json.Serialization.JsonContract ResolveContract(Type type)
        {
            if (type.Assembly.Equals(_assembly))
            {
                return _defaultContractSerializer.ResolveContract(type);
            }
    
            return _camelCaseContractResolver.ResolveContract(type);
        }
    }
    

    提交回复
    热议问题