繁体   English   中英

连接响应属性为驼峰式

[英]Connection response properties are camel case

我不知道为什么会这样。 我正在将我的angular 4应用程序从托管服务器连接到signalR集线器,它的工作原理就像一个超级按钮(2.2.2版)

我现在必须向另一个项目添加第二个signalR连接,并且由于某些未知的原因,响应的属性全部是camelCase而不是PascalCase。 但是,jquery.signalr-2.2.2.js文件期望它们为PascalCase,并抛出服务器版本“未定义”的错误。

由于他正在寻找res.ProtocolVersion,因此“未定义”是合乎逻辑的,并且该属性在我的反序列化响应中不存在。 但是,我确实有一个res.protocolVersion,它拥有他需要的确切值。

我为此已经浪费了很多时间,我们非常感谢您的帮助!

调试jquery.signalR-2.2.2.js

编辑:@ rory-mccrossan 在此处输入图片说明 我想了很多,这就是为什么我注释掉了服务器端的json序列化器/格式化程序代码,但无济于事。

我正在考虑下一个建议

因此,在罗瑞(Rory)的提示下,我在互联网上搜索了更多内容,当然,其他人也遇到了这个问题:

SignalR:使用骆驼套

但是,该解决方案不适用于我:/

然后,我找到了一个类似的解决方案,但是在这里,您将始终具有默认的合同解析器,除非对象来自某个库(带有视图模型的库)。 https://blogs.msdn.microsoft.com/stuartleeks/2012/09/10/automatic-camel-casing-of-properties-with-signalr-hubs/

注意:这不是理想的解决方案,但它是最适合我的情况的解决方案

因此,我想出了一个新的解决方案,它与存在问题的现有代码很好地结合在一起。

解决此问题的另一种方法是让您的应用使用DefaultContractResolver SignalR现在将连接,但是您的其他应用程序将中断。 为了缓解该问题,我正在使用两种简单的扩展方法。

首先,我扩展了HttpConfiguration类,以换出CamelCasePropertyNamesContractResolver的格式化程序

public static class HttpConfigurationExtensions
    {
        public static HttpConfiguration ToCamelCaseHttpConfiguration(this HttpConfiguration configuration)
        {
            var jsonFormatter = configuration.Formatters.OfType<JsonMediaTypeFormatter>().FirstOrDefault();
            bool needToAddFormatter = jsonFormatter == null;
            if (needToAddFormatter)
            {
                jsonFormatter = new JsonMediaTypeFormatter();
            }
            jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            jsonFormatter.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
            if (needToAddFormatter)
            {
                configuration.Formatters.Add(jsonFormatter);
            }
            return configuration;
        }
    }

现有的Web API将始终返回HttpResponseMessage ,这就是为什么我可以像以前那样处理它。

api调用示例

[Route("")]
[HttpPost]
public async Task<HttpResponseMessage> CreateSetting(Setting setting)
{
   // the response object is basically the data you want to return
   var responseData = await ...

   return Request.CreateResponse(responseData.StatusCode, responseData);
}

我注意到每个Api调用都使用Request.CreateResponse(...)方法。 我没有立即看到的是Microsoft实际上已经预见到了所有必要的重载,这意味着我不能只做自己的Request.CreateResponse(...)实现。

这就是为什么它被称为MakeResponse

public static class HttpRequestMessageExtensions
{
    public static HttpResponseMessage MakeResponse<T>(this HttpRequestMessage request, T response) where T : Response
    {
        return request.CreateResponse(response.StatusCode, response,
            request.GetConfiguration().ToCamelCaseHttpConfiguration());
    }
}

Response类是您希望api返回的数据结构。 在我们的api中,它们全都包裹在这些结构之一中。 这会导致api的响应类似于Slack API上的响应。

因此,现在所有控制器都使用Request.MakeResponse(responseData)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM