定义ContractResolver ,继承 IContractResolver
using System; using System.Reflection; using Newtonsoft.Json.Serialization; using SignalR; namespace Loveboat.Configuration { 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).Assembly; } #region IContractResolver Members public JsonContract ResolveContract(Type type) { if (type.Assembly.Equals(_assembly)) return _defaultContractSerializer.ResolveContract(type); return _camelCaseContractResolver.ResolveContract(type); } #endregion } }
配置应用:
var settings = new JsonSerializerSettings(); settings.ContractResolver = new SignalRContractResolver(); var serializer = JsonSerializer.Create(settings); GlobalHost.DependencyResolver.Register(typeof (JsonSerializer), () => serializer);
var settings = new JsonSerializerSettings {ContractResolver = new SignalRContractResolver()}; var serializer = JsonSerializer.Create(settings); builder.Register(c => serializer).As<JsonSerializer>();
阅读全文
公众号近期文章
赞赏支持
0 Responses to “Automatic camel-casing of properties with SignalR hubs”