簡體   English   中英

WCF WebInvoke可以接受內容類型:text / plain?

[英]WCF WebInvoke which can accept content-type: text/plain?

我正在編寫WCF REST服務以使用我的WCF REST服務接收AWS SNS通知消息。

但是,WCF REST僅支持XML和JSON,但由於傳統原因,Amazon SNS使用Content-Type: text/plain; charset=UTF-8發布其通知Content-Type: text/plain; charset=UTF-8 根據亞馬遜文檔Content-Type: text/plain; charset=UTF-8標頭:

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

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

當我使用像Amazon這樣的“text / plain”內容類型調用我的服務時,會出現一個錯誤:

請求錯誤。

服務器遇到處理請求的錯誤。 異常消息是'傳入消息具有意外的消息格式'Raw'。 該操作的預期消息格式是'Xml'; 'Json的'。 這可能是因為尚未在綁定上配置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內容類型?

您可以通過創建和應用自定義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>

相關部分是<customBinding>元素,其中配置了自定義映射器,以及應用它的servcices/service/endpoint/bindingConfiguration屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM