Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intermittent "Could not load type 'System.Net.Http.Formatting.FormUrlEncodedMediaTypeFormatter'" Error

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:

  • This DOES NOT happen when the application is deployed (to Azure)
  • This only happens occasionally
  • Typically, I run one of the MVC5 sites in the debugger and see this issue
  • If I navigate directly to the WebAPI site after the above, I'll get the "Yellow Screen of Death" with the error as per above
  • Sometimes, even after a cold reboot, this error will NOT appear, other times it will

What I currently do to work around the problem:

  • If I run the WebAPI site in the debugger, if the error is present, it will fixed itself
  • After running in the debugger (per above), I can relaunch an MVC5 site in the debugger and the issue goes away

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:

  • Microsoft.AspNet.WebApi (version 5.2.3)
  • Microsoft.AspNet.WebApi.Client (version 5.2.3)
  • Microsoft.AspNet.WebApi.Core (version 5.2.3)
  • Microsoft.AspNet.WebApi.Owin (version 5.2.2)
  • Microsoft.AspNet.WebApi.WebHost (version 5.2.3)

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.

like image 323
Brendan Green Avatar asked Jan 25 '16 21:01

Brendan Green


2 Answers

Uninstalled Web API 2 NuGet package Installed Web API 2 NuGet package again.

like image 75
dridi Avatar answered Nov 19 '22 14:11

dridi


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());
like image 32
SlaterCodes Avatar answered Nov 19 '22 15:11

SlaterCodes