asp.net-web-api.net-4.8asp.net-web-api-routing

MuchRandomly Content-Type is not getting seen by a .Net Framework WebAPI server


Documenting a much random issue, probably of .NetFramework, that happens once every 3-4 weeks.

We have 3 load-balanced servers in AWS behind ALB. And occassionally, one of the WebAPI servers stops seeing the Content-Type header and infers it to be application/octet-stream.

This is the Good Server that says that the Content-Type application/asdf is invalid. Good Server showing proper error



This is the Faulty Server that says the the no Content-Type header was sent, so it is inferring it to be application/octed-stream. Please Note that through Postman, I am sending the Content-Type header as application/asdf which is simply getting ignored.

Faulty Server showing Content-Type isn't sent and has to be inferred

Please note that this has nothing to do with AWS ALB as I have checked this from the localmachine using curl as well. Response is the same.

This is the JsonMediaformatterConfiguration that works properly once I restart the IIS process


Solution

  • Problem was in a particuarly less used features implementation that was done on 2017.

    https://stackoverflow.com/a/41231353

    This suggested to use reflection to change some basic settings in the .NetFramework. So it solved one problem, but introduced another one.

    The above answer was added on 2016 and the last comment(added in 2022) says:

    The problem with using this is that it breaks all the other API calls that didn't have this problem. I think because of the BindingFlags.Static which is required to make this work.

    Apparently, whenever this code was triggered by any customer, all the ApiControllers started rejecting all calls.