Reputation: 7006
I have a selfhosted WebApi 1 and a costum tracer.
Everything is logged and traced correctly when there are valid requests and responses; however when there is a POST request whose size is larger than the default maxRequestLength the server returns a 400 (Bad Request) to the client and the execution in the pipeline terminates. As the result of this, the tracer will never capture the error.
Looking at the ASP.NET WebApi Message LifeCycle diagram HERE I assume that the 400 response is generated and returned way before the tracer starts its job in the pipeline.
Considering that such error could be easily captured on the Global.asax (On_Error method) what is the solution in capturing errors in a selfhosted WebApi?
Thanks in advance.
Upvotes: 0
Views: 1039
Reputation: 142252
I'm assuming you are using Web API 1 self host. Under the covers this is a WCF service host. That MaxRequestLength is a WCF thing.
So, if you want to trap it you will need to drop down to WCF customization and configuration. If you create a configuration class that is derived from HttpSelfHostConfiguration there is an overload that allows you to configure the binding and with a whole lot of WCF magic you probably can capture the error.
My guess is that it is not going to be pretty.
My first suggestion would be to look into the WebAPI 2 Owin HttpListener host. This is not WCF based and you may find it easier to trap these kinds of issues. To be honest I really don't know if the Owin HTTPListener host has a built in equivalent to MaxRequestLength, you may actually have to build it yourself as a MessageHandler.
Upvotes: 1