return this.Request.Content.ReadAsAsync<MyCustomType>(new[] { new MyCustomTypeMediaFormatter() })
Results in
System.InvalidOperationException occurred
Message=No 'MediaTypeFormatter' is available to read an object of type 'MyCustomType' with the media type 'application/vnd.mycustomtype+xml'.
Source=System.Net.Http.Formatting
StackTrace:
at System.Net.Http.ObjectContent.SelectAndValidateReadFormatter(Boolean acceptNullFormatter)
at System.Net.Http.ObjectContent.ReadAsyncInternal[T](Boolean allowDefaultIfNoFormatter)
at System.Net.Http.ObjectContent`1.ReadAsync()
at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, IEnumerable`1 formatters)
at
InnerException:
In my custom formatter
public MyCustomMediaTypeFormatter( )
{
this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/vnd.mycustomtype+xml"));
}
Have you overridden the "CanReadType" virtual method in MediaTypeFormatter?
In old WebAPI stack, the CanReadType and CanWriteType used to return "true" by default, but now they are "false" by default. You need to explicitly override them.
Thanks,
Kiran Challa
Marked as answer by chrisortman on Feb 17, 2012 09:34 PM
chrisortman
Member
38 Points
23 Posts
ReadAsAsync<T> with CustomMediaTypeFormatter
Feb 17, 2012 09:26 PM|LINK
return this.Request.Content.ReadAsAsync<MyCustomType>(new[] { new MyCustomTypeMediaFormatter() })Results in
System.InvalidOperationException occurred Message=No 'MediaTypeFormatter' is available to read an object of type 'MyCustomType' with the media type 'application/vnd.mycustomtype+xml'. Source=System.Net.Http.Formatting StackTrace: at System.Net.Http.ObjectContent.SelectAndValidateReadFormatter(Boolean acceptNullFormatter) at System.Net.Http.ObjectContent.ReadAsyncInternal[T](Boolean allowDefaultIfNoFormatter) at System.Net.Http.ObjectContent`1.ReadAsync() at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, IEnumerable`1 formatters) at InnerException:In my custom formatter
public MyCustomMediaTypeFormatter( ) { this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/vnd.mycustomtype+xml")); }Kiran Challa
Participant
1442 Points
281 Posts
Microsoft
Re: ReadAsAsync<T> with CustomMediaTypeFormatter
Feb 17, 2012 09:29 PM|LINK
Have you overridden the "CanReadType" virtual method in MediaTypeFormatter?
In old WebAPI stack, the CanReadType and CanWriteType used to return "true" by default, but now they are "false" by default. You need to explicitly override them.
Kiran Challa
chrisortman
Member
38 Points
23 Posts
Re: ReadAsAsync<T> with CustomMediaTypeFormatter
Feb 17, 2012 09:34 PM|LINK
That did the trick thank you.