【问题标题】:WCF WebInvoke which can accept content-type: text/plain?WCF WebInvoke 可以接受内容类型:文本/纯文本?
【发布时间】:2016-07-13 00:06:14
【问题描述】:

我正在编写 WCF REST 服务,以使用我的 WCF REST 服务接收 AWS SNS 通知消息。

但是,WCF REST 仅支持 XML 和 JSON,但由于遗留原因,Amazon SNS 使用 Content-Type: text/plain; charset=UTF-8 标头发布通知,根据 Amazon documentation

POST / HTTP/1.1
Content-Type: text/plain; charset=UTF-8
// ...

{
  "Type" : "Notification",
  // ...
  "UnsubscribeURL" : "https://sns.us-west-2.amazonaws.com/?Action=Unsubscribe&..."
}

当我使用这种“文本/纯文本”内容类型(如亚马逊)调用我的服务时,出现以下错误:

请求错误。

服务器在处理请求时遇到错误。异常消息是“传入消息具有意外的消息格式“原始”。该操作的预期消息格式为“Xml”; '杰森'。这可能是因为尚未在绑定上配置 WebContentTypeMapper。有关详细信息,请参阅 WebContentTypeMapper 的文档。有关详细信息,请参阅服务器日志。

我当前的代码:

public interface MyServiceInterface
{
    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "/AmazonIPChanges")]
    Task AmazonIPChanges(SNSNotificationMessage data);
}

[DataContract]
public class SNSNotificationMessage
{
    [DataMember]
    public string Type { get; set; }
    // ...
    [DataMember]
    public string UnsubscribeURL { get; set; }
} 

DataContract 映射到 Amazon SNS 消息。当我使用内容类型“application/json”执行 POST 时,此代码有效,但我怎样才能让它接受亚马逊的 text/plain 内容类型?

【问题讨论】:

  • 想要以明文形式将数据发布到 Web 服务是没有意义的,因此错误是正确的。您的请求到底是什么样的?
  • 你一定被误解了。 AWS SNS 是一种通知服务。我不发消息。 AWS 正在发送它。我只是想得到这个输入。
  • 是什么让您认为 SNS 以纯文本形式发布?他们的文档表明 JSON。
  • 我认为您没有完整阅读文档。有一个地方说明通知消息包含什么。您可以在那里看到,标题的内容类型为纯文本,但正文是 json 内容。您也可以搜索并查看这是一种错误,但亚马逊无法更改它,因为他们不想影响当前的客户实施。我也再次解析了数据包并查看了它包含的内容
  • 我看到你的方式更清楚地表达了问题,我试图实现 WebContentTypeMapper 自定义。你的回答和例子都很好。谢谢。

标签: c# wcf amazon-web-services amazon-sns


【解决方案1】:

如错误消息所示,您可以通过创建和应用自定义WebContentTypeMapper 来解决此问题。它应该是这样的:

namespace StackOverflow36216464
{
    public class RawContentTypeMapper : WebContentTypeMapper
    {
        public override WebContentFormat GetMessageFormatForContentType(string contentType)
        {
            switch (contentType.ToLowerInvariant())
            {
                case "text/plain":
                case "application/json":
                    return WebContentFormat.Json;
                case "application/xml":
                    return WebContentFormat.Xml;
                default:
                    return WebContentFormat.Default;
            }
        }
    }
}

这个解释请求的内容类型,并返回适当的WebContentFormat枚举成员。

然后您可以以自定义绑定的形式将其应用到您的服务:

<system.serviceModel>
    <bindings>
        <customBinding>
            <binding name="textPlainToApplicationJson">
                <webMessageEncoding webContentTypeMapperType="StackOverflow36216464.RawContentTypeMapper, StackOverflow36216464, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
                <httpTransport manualAddressing="true" />
            </binding>
        </customBinding>
    </bindings>
    <behaviors>
        <serviceBehaviors>
            <behavior name="debugServiceBehavior">
                <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="restEndpointBehavior">
                <webHttp />
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <services>
        <service
          name="StackOverflow36216464.Service1"
          behaviorConfiguration="debugServiceBehavior">
            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:65393/"/>
                </baseAddresses>
            </host>
            <endpoint address=""
                  binding="customBinding"
                  contract="StackOverflow36216464.IService1"
                  behaviorConfiguration="restEndpointBehavior"
                  bindingConfiguration="textPlainToApplicationJson"/>        
        </service>
    </services>
</system.serviceModel>

相关部分是配置自定义映射器的&lt;customBinding&gt; 元素和应用它的servcices/service/endpoint/bindingConfiguration 属性。

【讨论】:

猜你喜欢
  • 2014-01-15
  • 1970-01-01
  • 2016-09-27
  • 2018-05-11
  • 2014-06-14
  • 1970-01-01
  • 1970-01-01
  • 2012-02-15
  • 2016-06-02
相关资源
最近更新 更多