With the default "Values" service template. If I use a GET request with a body and an accept header of application/json, I get the following exception:
{"ExceptionType":"System.InvalidOperationException","Message":"No 'MediaTypeFormatter' is available to read an object of type 'Int32' with the media type ''undefined''.","StackTrace":" at System.Net.Http.ObjectContent.SelectAndValidateReadFormatter(Boolean
acceptNullFormatter)\u000d\u000a at System.Net.Http.ObjectContent.ReadAsyncInternal[T](Boolean allowDefaultIfNoFormatter)\u000d\u000a at System.Web.Http.ModelBinding.RequestContentReader.ReadWholeBodyAsync(HttpActionContext actionContext, Type modelType)\u000d\u000a
at System.Web.Http.ModelBinding.RequestContentReader.ReadContentAsync(HttpActionContext actionContext)\u000d\u000a at System.Web.Http.ModelBinding.DefaultActionValueBinder.BindValuesAsync(HttpActionContext actionContext, CancellationToken cancellationToken)\u000d\u000a
at System.Web.Http.ApiController.<>c__DisplayClass3.<ExecuteAsync>b__0()\u000d\u000a at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)\u000d\u000a at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncInternal(HttpRequestMessage
request, CancellationToken cancellationToken)\u000d\u000a at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)"}
Note the "sss" content. Clearly this request is incorrect because a GET shouldn't have a body but my question is: how do I trap this exception to log it? I've tried the following:
Custom ExceptionFilter on the service - but it doesn't seem to have got this far
Custom ActionFilterAttribute in configuration.Filters and overriding OnActionExecuted but this is never called either
DelegatingHandler - this can trap the 500 however the content is an ObjectType but I can't see an obvious way to retrieve the underlying exception to intelligently turn this into a bad request (or even a 404) and I also can't use standard logging framework
to log the exception as I don't have the Exception object.
Perhaps the way forward is to validate the GET and Body combination but this might not be the only error, ideally I'd like a reliable way to trap and log all exceptions being raised as strongly typed exceptions.
It seems like Web API is trying to read the body but because no Content-Type header has been provided, it's failing.
LittleClive
Member
91 Points
65 Posts
Trapping exception? GET request with body
Feb 23, 2012 08:37 AM|LINK
With the default "Values" service template. If I use a GET request with a body and an accept header of application/json, I get the following exception:
{"ExceptionType":"System.InvalidOperationException","Message":"No 'MediaTypeFormatter' is available to read an object of type 'Int32' with the media type ''undefined''.","StackTrace":" at System.Net.Http.ObjectContent.SelectAndValidateReadFormatter(Boolean acceptNullFormatter)\u000d\u000a at System.Net.Http.ObjectContent.ReadAsyncInternal[T](Boolean allowDefaultIfNoFormatter)\u000d\u000a at System.Web.Http.ModelBinding.RequestContentReader.ReadWholeBodyAsync(HttpActionContext actionContext, Type modelType)\u000d\u000a at System.Web.Http.ModelBinding.RequestContentReader.ReadContentAsync(HttpActionContext actionContext)\u000d\u000a at System.Web.Http.ModelBinding.DefaultActionValueBinder.BindValuesAsync(HttpActionContext actionContext, CancellationToken cancellationToken)\u000d\u000a at System.Web.Http.ApiController.<>c__DisplayClass3.<ExecuteAsync>b__0()\u000d\u000a at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)\u000d\u000a at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncInternal(HttpRequestMessage request, CancellationToken cancellationToken)\u000d\u000a at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)"}
The request I sent is:
Note the "sss" content. Clearly this request is incorrect because a GET shouldn't have a body but my question is: how do I trap this exception to log it? I've tried the following:
Perhaps the way forward is to validate the GET and Body combination but this might not be the only error, ideally I'd like a reliable way to trap and log all exceptions being raised as strongly typed exceptions.
It seems like Web API is trying to read the body but because no Content-Type header has been provided, it's failing.