Default MediaTypeFormatter pr. service

Topics: Web Api
Dec 1, 2011 at 2:20 PM

Hi

By reading through the posts here, it seems like the way to setup a default MessageHandler for a service is to create a DelegatingHandler that'll change the Accept header to point to a given MediaTypeformatter....

The way to add the DelegatingHandler is to setup a MessageHandlerFactory in the configuration, that'll return a list of Handlers....

But what if I have a number of services, all defaulting to their own MediaTypeFormatter?? Then I'd have to either create a delegatinghandler, which can handle it for all services, or to create a configuration for each service with it's own list of handlers.....

I'm using a DI/IoC framework, so if I knew the type of the service in question, when the handlers are resolved I could resolve a different handler for each service... however the ServiceType is not given as an argument to the MessageHandlerFactory....

It would be nice if the delegate to create message handlers, was given the servicetype, just like the CreateInstance delegate does....

any plans on adding this in a future release?

Regards,

søren

 

Coordinator
Dec 2, 2011 at 12:45 AM

Could you please tell me more about why you want to have a different default formatter for each service?

Daniel Roth

Dec 2, 2011 at 7:47 AM

I working on a project with a number of services, some of these services should return HTML if called from a browser... 

Chrome "correctly" sends an Accept header of "text/html" though IE tends to just send "*/*" ....

I could of course just add a DelegatingHandler which I initialize with a list of "HTML" services and then do a check in SendAsync... however I thought it would be nicer if I could just do it in the MessageHandlerFactory at Resolve time.. but to do that, I need to know the servicetype that I'm resolving messagehandlers for... Instead, it would be nice if the servicetype was given to delegate.

 

I've managed to solve it in another way.... creating a specific messagehandlerfactory for each HTML service, which is initialized with the servicetype... it just a bit clumpsy I think....

Regards,

Søren