I am working on a project that includes an ASP.NET WebAPI component.
Intermittently, I get the following error:
Could not load type 'System.Net.Http.Formatting.FormUrlEncodedMediaTypeFormatter' from assembly 'System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
with the source error pointing to my GlobalConfiguration.Configure()
call:
Line 35: {
Line 36: AreaRegistration.RegisterAllAreas();
Line 37: GlobalConfiguration.Configure(WebApiConfig.Register); //<-- error here
Line 38: FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
Line 39: RouteConfig.RegisterRoutes(RouteTable.Routes);
In my register, I am doing the following to return JSON by default:
var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes
.FirstOrDefault(t => t.MediaType == "application/xml");
if (appXmlType != null)
{
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
}
(not sure of the source, but found somewhere on this site as a way to return JSON by default, unless the caller asks for something else)
I'd like to continue to return JSON as the default content type going forward.
The project includes two other (non WebAPI) websites (that are MVC5), which hit the WebAPI site.
Here's the interesting thing:
What I currently do to work around the problem:
Just to repeat, this DOES NOT happen ever when the application is deployed to Azure.
I have multiple developers working on this project, and we all run into the same error at some stage.
I am referencing the following Nuget packages:
If I look inside the *.csproj
file, I have a reference to the following:
<Reference Include="System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath>
<Private>True</Private>
</Reference>
I've checked the bin
folder of the website, and all the assemblies inside it have the correct version as per the above list (including System.Net.Http.Formatting.dll).
Also note that, during debugging and development, each of the three websites are run under Local IIS
(constraints of external resources force us to do this), so I have:
http://localhost/site1
http://localhost/site2
http://localhost/api
It's becoming cumbersome to get into a debugging session, to get this error, to then stop debugging, launch the API site under the debugger, and to then switch back to what I was doing at the start.
Not sure where else to go to track down what is happening here. Any ideas?
Update - 26 July 2016
Still have this happening. Since posting the question, have updated the WebAPI to run in its own Application Pool, but that doesn't seem to have made a difference.
Uninstalled Web API 2 NuGet package Installed Web API 2 NuGet package again.
I think you are going about it wrong. Try setting JSON as your default media formatter like this:
// first clear all formatters
config.Formatters.Clear();
// Since JSON is the first added,
// it will be the "default" if no Accept header present
config.Formatters.Add(new JsonMediaTypeFormatter());
// You can choose not to add these back if you never want to use them,
// up to you.
config.Formatters.Add(new XmlMediaTypeFormatter());
config.Formatters.Add(new FormUrlEncodedMediaTypeFormatter());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With