【问题标题】:Parsing AWS SNS notification in .net core 1.0在 .net core 1.0 中解析 A​​WS SNS 通知
【发布时间】:2018-01-12 21:55:46
【问题描述】:

我有一个 VisualStudio17 无服务器应用程序项目,并且正在使用 .net core Web Api。

我想确认我的 SNS 订阅,但我遇到了一个问题,即 AWS 发送 POST 请求时标头 content-type 设置为 text/plain; charset=UTF-8 而正文是 JSON。

这是来自documentation 的示例请求:

POST / HTTP/1.1
x-amz-sns-message-type: Notification
x-amz-sns-message-id: da41e39f-ea4d-435a-b922-c6aae3915ebe
x-amz-sns-topic-arn: arn:aws:sns:us-west-2:123456789012:MyTopic
x-amz-sns-subscription-arn: arn:aws:sns:us-west-2:123456789012:MyTopic:2bcfbf39-05c3-41de-beaa-fcfcc21c8f55
Content-Length: 761
Content-Type: text/plain; charset=UTF-8
Host: ec2-50-17-44-49.compute-1.amazonaws.com
Connection: Keep-Alive
User-Agent: Amazon Simple Notification Service Agent

{
  "Type" : "Notification",
  "MessageId" : "da41e39f-ea4d-435a-b922-c6aae3915ebe",
  "TopicArn" : "arn:aws:sns:us-west-2:123456789012:MyTopic",
  "Subject" : "test",
  "Message" : "test message",
  "Timestamp" : "2012-04-25T21:49:25.719Z",
  "SignatureVersion" : "1",
  "Signature" : "EXAMPLElDMXvB8r9R83tGoNn0ecwd5UjllzsvSvbItzfaMpN2nk5HVSw7XnOn/49IkxDKz8YrlH2qJXj2iZB0Zo2O71c4qQk1fMUDi3LGpij7RCW7AW9vYYsSqIKRnFS94ilu7NFhUzLiieYr4BKHpdTmdD6c0esKEYBpabxDSc=",
  "SigningCertURL" : "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem",
  "UnsubscribeURL" : "https://sns.us-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-west-2:123456789012:MyTopic:2bcfbf39-05c3-41de-beaa-fcfcc21c8f55"
} 

内容类型:文本、正文 JSON。这使得它解析起来相当困难,而且一个简单的

public void Post([FromBody] string t) // or dynamic t for the matter

不起作用并抛出Request was short circuited at action filter 'Microsoft.AspNetCore.Mvc.ModelBinding.UnsupportedContentTypeFilter'. 异常。

我错过了什么吗?他们为什么要这样做,我该如何处理?

【问题讨论】:

    标签: json amazon-web-services .net-core amazon-sns


    【解决方案1】:

    我通过将text/plain 添加到JsonInputFormatter 应该格式化的格式中,使它像我在this 答案中描述的那样工作。

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc(config =>
        {
            foreach (var formatter in config.InputFormatters)
            {
                if (formatter.GetType() == typeof(JsonInputFormatter))
                     ((JsonInputFormatter)formatter).SupportedMediaTypes.Add(
                          MediaTypeHeaderValue.Parse("text/plain"));
            }
         });
         ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-15
      • 1970-01-01
      • 2014-08-18
      • 2021-05-26
      • 2016-12-14
      • 1970-01-01
      • 2015-04-14
      • 1970-01-01
      相关资源
      最近更新 更多