WCF WebInvoke可以接受内容类型:文本/纯文本? [英] WCF WebInvoke which can accept content-type: text/plain?
问题描述
我正在编写WCF REST服务以通过WCF REST服务接收AWS SNS通知消息.
I'm writing a WCF REST service to receive AWS SNS Notification Message with my WCF REST Service.
但是,根据当我通过此文本/纯文本"致电我的服务时,内容类型(例如Amazon),出现错误提示:
When I call my service with this "text/plain" content type like Amazon, there is an error which says:
请求错误.
服务器在处理请求时遇到错误.异常消息是传入消息的消息格式为意外的原始".该操作的预期消息格式为"Xml"; 杰森".这可能是因为尚未在绑定上配置WebContentTypeMapper.有关更多详细信息,请参见WebContentTypeMapper的文档.有关更多详细信息,请参阅服务器日志.
The server encountered an error processing the request. The exception message is 'The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml'; 'Json'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.'. See server logs for more details.
我当前的代码:
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时,此代码有效,但是如何让它接受Amazon的text/plain内容类型?
The DataContract maps to the Amazon SNS message. This code is working when I perform a POST with content-type "application/json", but how can I let it accept Amazon's text/plain content-type?
推荐答案
如错误消息所示,您可以通过创建并应用自定义的WebContentTypeMapper
来解决此问题.它应该看起来像这样:
You can fix this, as the error message indicates, by creating and applying a custom WebContentTypeMapper
. It should look like this:
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
枚举成员.
This one interprets the content-type of the request, and returns the appropriate WebContentFormat
enum member.
然后您可以以自定义绑定的形式将其应用于您的服务:
You can then apply it to your service in the form of a custom binding:
<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
属性(在其中应用了
The relevant part being the <customBinding>
element, where the custom mapper is configured, and the servcices/service/endpoint/bindingConfiguration
attribute where it is applied.
这篇关于WCF WebInvoke可以接受内容类型:文本/纯文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!