Wednesday, April 13, 2011

Solving RAW issue with WCF and content-type

A couple days ago, I faced an issue when posting content with content-type:text/xml. The error which I received is "The expected body format value is 'RAW'". Around the google I found some topic discuss my issue. Follow by these posts, I solved my issue. Thanks God, it take me 3 hours.



In the 1st link, it just tell me how to implement the WebContentFormat extension. Beside, the 2nd link tell me how to config it in Web.config

Below is my implement (have some differences)

1 - I defined the new class which was named RawContentTypeMapper inherit from WebContentTypeMapper. This class will change the any content-type to RAW

1 using System.ServiceModel.Channels;

2

3 namespace AtlanticGateway.GatewayComponent

4 {

5 public class RawContentTypeMapper : WebContentTypeMapper

6 {

7 public override WebContentFormat GetMessageFormatForContentType(string contentType)

8 {

9 if (contentType.Contains("text/xml") || contentType.Contains("application/xml"))

10 {

11 return WebContentFormat.Raw;

12 }

13 else

14 {

15 return WebContentFormat.Default;

16 }

17 }

18 }

19 }


2 - Reference this new WebContentTypeMapper in your config file in customBinding:

25 <service behaviorConfiguration="NotificationServiceBehavior"

26 name="AtlanticGateway.GatewayComponent.Services.NotificationService">

27 <endpoint behaviorConfiguration="web" binding="customBinding" bindingConfiguration="RawReceiveCapable"

28 contract="AtlanticGateway.GatewayComponent.Message.INotificationService" />

29



74 <bindings>

75 <customBinding>

76 <binding name="RawReceiveCapable">

77

78 <webMessageEncoding webContentTypeMapperType=

79 "AtlanticGateway.GatewayComponent.RawContentTypeMapper, AtlanticGateway.GatewayComponent,

80 Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />

81 <httpTransport manualAddressing="true" maxReceivedMessageSize="524288000"

82 transferMode="Streamed" />

83

84


That's all.

Thanks David and