title: Intermittent "Could not load type 'System.Net.Http.Formatting.FormUrlEncodedMediaTypeFormatter'" Error
body: <p>I am working on a project that includes an ASP.NET WebAPI component.</p>
  
  <p>Intermittently, I get the following error: </p>
  
  <blockquote>
    <p>Could not load type
    'System.Net.Http.Formatting.FormUrlEncodedMediaTypeFormatter' from
    assembly 'System.Net.Http.Formatting, Version=5.2.3.0,
    Culture=neutral, PublicKeyToken=31bf3856ad364e35'.</p>
  </blockquote>
  
  <p>with the source error pointing to my <code>GlobalConfiguration.Configure()</code> call:</p>
  
  <pre><code>Line 35: { 
  Line 36:     AreaRegistration.RegisterAllAreas(); 
  Line 37:     GlobalConfiguration.Configure(WebApiConfig.Register); //&lt;-- error here
  Line 38:     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
  Line 39:     RouteConfig.RegisterRoutes(RouteTable.Routes);
  </code></pre>
  
  <p>In my register, I am doing the following to return JSON by default:</p>
  
  <pre><code>var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes
                   .FirstOrDefault(t =&gt; t.MediaType == "application/xml");
  
  if (appXmlType != null)
  {
      config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
  }
  </code></pre>
  
  <p>(<em>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</em>)</p>
  
  <p>I'd like to continue to return JSON as the default content type going forward.</p>
  
  <p>The project includes two other (non WebAPI) websites (that are MVC5), which hit the WebAPI site.</p>
  
  <p>Here's the interesting thing:</p>
  
  <ul>
  <li>This <strong>DOES NOT</strong> happen when the application is deployed (to Azure)</li>
  <li>This only happens occasionally</li>
  <li>Typically, I run one of the MVC5 sites in the debugger and see this issue</li>
  <li>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</li>
  <li>Sometimes, even after a cold reboot, this error will NOT appear, other times it will</li>
  </ul>
  
  <p>What I currently do to work around the problem:</p>
  
  <ul>
  <li>If I run the WebAPI site in the debugger, if the error is present, it will fixed itself</li>
  <li>After running in the debugger (per above), I can relaunch an MVC5 site in the debugger and the issue goes away</li>
  </ul>
  
  <p>Just to repeat, this <strong>DOES NOT</strong> happen <strong><em>ever</em></strong> when the application is deployed to Azure.</p>
  
  <p>I have multiple developers working on this project, and we all run into the same error at some stage.</p>
  
  <p>I am referencing the following Nuget packages:</p>
  
  <ul>
  <li>Microsoft.AspNet.WebApi (version 5.2.3)</li>
  <li>Microsoft.AspNet.WebApi.Client (version 5.2.3)</li>
  <li>Microsoft.AspNet.WebApi.Core (version 5.2.3)</li>
  <li>Microsoft.AspNet.WebApi.Owin (version 5.2.2)</li>
  <li>Microsoft.AspNet.WebApi.WebHost (version 5.2.3)</li>
  </ul>
  
  <p>If I look inside the <code>*.csproj</code> file, I have a reference to the following:</p>
  
  <pre><code>&lt;Reference Include="System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"&gt;
    &lt;HintPath&gt;..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll&lt;/HintPath&gt;
    &lt;Private&gt;True&lt;/Private&gt;
  &lt;/Reference&gt;
  </code></pre>
  
  <p>I've checked the <code>bin</code> folder of the website, and all the assemblies inside it have the correct version as per the above list (including <strong>System.Net.Http.Formatting.dll</strong>).</p>
  
  <p>Also note that, during debugging and development, each of the three websites are run under <code>Local IIS</code> (constraints of external resources force us to do this), so I have:</p>
  
  <ul>
  <li><code>http://localhost/site1</code></li>
  <li><code>http://localhost/site2</code></li>
  <li><code>http://localhost/api</code></li>
  </ul>
  
  <p>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.</p>
  
  <p>Not sure where else to go to track down what is happening here.  Any ideas?</p>
  
  <p><strong>Update - 26 July 2016</strong> </p>
  
  <p>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.</p>