Return custom object from a resource and associate MediaTypeFormatter with it

Topics: Web Api
Dec 7, 2011 at 9:09 AM
Edited Dec 7, 2011 at 9:16 AM

I'm trying to return an Atom entry from a service:

 

public SyndicationItem Post(HttpRequestMessage request)
{
    return new SyndicationItem();
}

 

I also associate a custom AtomEntryMediaTypeFormatter with the 'atom/xml; type=entry' media type.

No matter if a) just SyndicationItem is returned and 'atom/xml; type=entry' is specified in the request's Accept header or b) HttpResponseMessage<SyndicationItem> is returned and Content-Type is set explicitely, I keep receiving 500 response code. The service is not even getting executed. When accessing the service via the test client the browser says [Cannot serialize member SyndicationItem.AttributeExtensions because it implements IDictionary]. Shouldn't this serialization be handled by the media type formatter I specified? 

I must be missing something obvious here.

Thanks!

Dec 7, 2011 at 2:12 PM

Alright, I'll reply to my own question. I used SyndicationItem basically to reuse its serialization functionality to atom/xml in media type formatter. The model itself of course doesn't operate on SyndicationItems, but rather on domain specific things, like ActivityShots. 

So I modified the Post method above to return ActivityShotDto, atom/xml media type converter creates new SyndicationItem based on ActivityShotDto and serializes itself to a stream and it works fine. 

I'm still not getting why SyndicationItem cannot be returned directly though.