Custom Formatter not getting called (solved)

Topics: Web Api
May 31, 2012 at 2:34 PM

Hi!

I followed an example in Contact Manager sample project and am trying to return a jpg through a Web API Get call, the formatter is inserted but not called when sending the response.

Detailed steps I took:

  1. Created my custom Formatter and overriden the OnWriteToStream method.
  2. Created a sub-class of DelegatingHandler and overriden SendAsync in order to change the request's Header accept value to "image/jpeg"
  3. In Global.asax.cs, Application_Start() I insert the new custom Formatter into the beginning of the configuration Formatter list.
  4. Run the application and from chrome I send the request to the API.
  5. The request is caught by the handler and modified to accept only "image/jpg" Mime type
  6. The controller does it's thing and returns a HttpResponseMessage<myClass> with the results Header content type changed to "image/jpeg"
  7. the results returns to chrome in JSon format without passing through my formatter class.

Any idea of what I might be doing wrong?

I can provide sample code or a sample project if it helps.

 

Thanks in advance!

Take care,

Ricardo

Coordinator
May 31, 2012 at 2:39 PM
Did you also override CanWriteType?

Sent from my Windows Phone

From: ricardobentes
Sent: 5/31/2012 7:35 AM
To: Glenn Block
Subject: Custom Formatter not getting called [wcf:357855]

From: ricardobentes

Hi!

I followed an example in Contact Manager sample project and am trying to return a jpg through a Web API Get call, the formatter is inserted but not called when sending the response.

Detailed steps I took:

  1. Created my custom Formatter and overriden the OnWriteToStream method.
  2. Created a sub-class of DelegatingHandler and overriden SendAsync in order to change the request's Header accept value to "image/jpeg"
  3. In Global.asax.cs, Application_Start() I insert the new custom Formatter into the beginning of the configuration Formatter list.
  4. Run the application and from chrome I send the request to the API.
  5. The request is caught by the handler and modified to accept only "image/jpg" Mime type
  6. The controller does it's thing and returns a HttpResponseMessage<myClass> with the results Header content type changed to "image/jpeg"
  7. the results returns to chrome in JSon format without passing through my formatter class.

Any idea of what I might be doing wrong?

I can provide sample code or a sample project if it helps.

Thanks in advance!

Take care,

Ricardo

May 31, 2012 at 2:58 PM

Hi! 

Thank you for your reply!

 

Yes, just like in the example:

protected override bool CanReadType(Type type)
        {
            return true;
        }

May 31, 2012 at 3:00 PM

Oh man!

I'm so dumb! I've confused write and read and overrode wrong one!

I'll try that right now!

May 31, 2012 at 3:00 PM

Oh man!

I'm so dumb! I've confused write and read and overrode wrong one!

I'll try that right now!

May 31, 2012 at 3:13 PM

Yep, that was it.

Man this is embarassing...

 

Thanks a lot for your help gblock! :)