WCF WebInvoke 可以接受内容类型:text/plain?

Ker*_*ncı 5 c# wcf amazon-web-services amazon-sns

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

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

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

{
  "Type" : "Notification",
  // ...
  "UnsubscribeURL" : "https://sns.us-west-2.amazonaws.com/?Action=Unsubscribe&..."
}
Run Code Online (Sandbox Code Playgroud)

当我使用亚马逊等“文本/纯文本”内容类型调用我的服务时,出现错误:

请求错误。

服务器在处理请求时遇到错误。异常消息是“传入消息具有意外的消息格式‘Raw’”。该操作的预期消息格式为“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; }
} 
Run Code Online (Sandbox Code Playgroud)

DataContract 映射到 Amazon SNS 消息。当我使用内容类型“application/json”执行 POST 时,此代码可以正常工作,但如何让它接受亚马逊的文本/纯内容类型?

Cod*_*ter 4

正如错误消息所示,您可以通过创建并应用自定义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;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这个解释请求的内容类型,并返回适当的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>
Run Code Online (Sandbox Code Playgroud)

相关部分是<customBinding>配置自定义映射器的元素以及servcices/service/endpoint/bindingConfiguration应用它的属性。