SignalR .NET Core camelCase JSON合约解析器 [英] SignalR .NET Core camelCase JSON Contract Resolver

查看:65
本文介绍了SignalR .NET Core camelCase JSON合约解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用.NET Core RC2.使SignalR正常工作,但尝试使其返回JSON中的camelCase属性.

Using .NET Core RC2. Got SignalR working, but trying to get it returning camelCase properties in JSON.

对于我正在使用的API ...

For APIs I'm using...

services.AddMvc().AddJsonOptions(o => {
    o.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});

SignalR也许还没有任何东西(毕竟,它甚至还不应该工作……),但是想知道是否有人知道了吗?我已经尝试过一些诸如...

Maybe there's just nothing in place yet for SignalR (after all, it's not even supposed to work yet...), but wondering if anyone's figured it out yet? I've tried a few things like...

services.AddTransient<IContractResolver, CamelCasePropertyNamesContractResolver>();

...但不行.

有人工作了吗?

推荐答案

基于此问题从SignalR Core存储库开始,目前尚无本地方法,但是您可以按照

Based on this issue from the SignalR Core repository, there is no native way of doing this as of right now, but you can create a custom contract resolver as indicated in this comment on an old SignalR issue.

由于该线程适用于SignalR 2.2.0,因此让它适用于SignalR Core.

Since that thread is for SignalR 2.2.0, let's make it work for SignalR Core.

using System;
using System.Reflection;
using Microsoft.AspNetCore.SignalR.Infrastructure;
using Newtonsoft.Json.Serialization;

    public class SignalRContractResolver : IContractResolver
    {
        private readonly Assembly _assembly;
        private readonly IContractResolver _camelCaseContractResolver;
        private readonly IContractResolver _defaultContractSerializer;

        public SignalRContractResolver()
        {
            _defaultContractSerializer = new DefaultContractResolver();
            _camelCaseContractResolver = new CamelCasePropertyNamesContractResolver();
            _assembly = typeof(Connection).GetTypeInfo().Assembly;
        }


        public JsonContract ResolveContract(Type type)
        {
            if (type.GetTypeInfo().Assembly.Equals(_assembly))
                return _defaultContractSerializer.ResolveContract(type);

            return _camelCaseContractResolver.ResolveContract(type);
        }

    }

这里发生的是,您无法将SignalC内部使用骆驼案合同解析器,因为它会中断与客户端的通信.

What happens here is that you cannot use the camel case contract resolver for the SignalR internals, because it would break the communication with the client.

因此,每次我们使用ResolveContract方法解析合同时,都必须检查当前解析类型的程序集,并检查它是否是SignalR内部的.如果没有,那么我们可以使用骆驼案解决合同.

So every time we resolve a contract in the ResolveContract method we have to check the assembly of the type currently resolved and check wether it is SignalR internal. If not, then we can resolve the contract using camel case.

这时,我们需要在框架中注册此合同解析器.

At this point, we need to register this contract resolver in the framework.

public void ConfigureServices(IServiceCollection services)
    {
        var settings = new JsonSerializerSettings();
        settings.ContractResolver = new SignalRContractResolver();

        var serializer = JsonSerializer.Create(settings);

        services.Add(new ServiceDescriptor(typeof(JsonSerializer), 
                                           provider => serializer,
                                           ServiceLifetime.Transient));

        // register other services like SignalR, MVC and custom services
     }

祝你好运!

这篇关于SignalR .NET Core camelCase JSON合约解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆