我找到了基于此错误报告的解决方案:https ://github.com/SignalR/SignalR/issues/500
首先,创建一个自定义的 IContractResolver
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;
}
public JsonContract ResolveContract(Type type)
{
if (type.Assembly.Equals(_assembly))
return _defaultContractSerializer.ResolveContract(type);
return _camelCaseContractResolver.ResolveContract(type);
}
}
接下来,在 ApplicationStart 中注册
var settings = new JsonSerializerSettings();
settings.Converters.Add(new StringEnumConverter());
settings.ContractResolver = new SignalRContractResolver();
var serializer = new JsonNetSerializer(settings);
GlobalHost.DependencyResolver.Register(typeof(IJsonSerializer), () => serializer);
感谢halter73为我指明了正确的方向!