Method not found System.Net.Http.Formatting.MediaTypeFormatter.get_SupportedMediaTypes() after adding .NET Standard 2.0 dependency
Asked Answered
A

2

47

I have a .NET Framework 4.6.1 WebApi project that is referencing a small NuGet package we use internally to share common utility methods.

We want to start moving some of our stuff to .NET Core, so I changed the utility package to target .NET Standard 2.0. This was done by simply making a new .NET Standard 2.0 project and copying the source files over.

Utility package csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
  </PropertyGroup>

</Project>

After updating the package in my WebApi project, I get the following exception on startup:

[MissingMethodException: Method not found: 'System.Collections.ObjectModel.Collection`1<System.Net.Http.Headers.MediaTypeHeaderValue> System.Net.Http.Formatting.MediaTypeFormatter.get_SupportedMediaTypes()'.]
   MyWebApiProject.Application.InitializeHttpConfiguration(HttpConfiguration config) in C:\MyWebApiProject\Global.asax.cs:44
   System.Web.Http.GlobalConfiguration.Configure(Action`1 configurationCallback) +34
   MyWebApiProject.Application.OnApplicationStarted() in C:\MyWebApiProject\Global.asax.cs:62
   Ninject.Web.Common.NinjectHttpApplication.Application_Start() +183

[HttpException (0x80004005): Method not found: 'System.Collections.ObjectModel.Collection`1<System.Net.Http.Headers.MediaTypeHeaderValue> System.Net.Http.Formatting.MediaTypeFormatter.get_SupportedMediaTypes()'.]
   System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +10104513
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +173
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +336
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +296

[HttpException (0x80004005): Method not found: 'System.Collections.ObjectModel.Collection`1<System.Net.Http.Headers.MediaTypeHeaderValue> System.Net.Http.Formatting.MediaTypeFormatter.get_SupportedMediaTypes()'.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +10085804
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +95
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254

The only changes are the version number in packages.config and the csproj.

Any ideas?

Thanks!

Adynamia answered 20/9, 2017 at 12:57 Comment(0)
A
104

Turned out to just need a binding redirect to System.Net.Http:

<dependentAssembly>
  <assemblyIdentity name="System.Net.Http" publicKeyToken="B03F5F7F11D50A3A" culture="neutral"/>
  <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0"/>
</dependentAssembly>
Adynamia answered 25/9, 2017 at 7:0 Comment(4)
Can you explain why this is required? Is this a good practice?Lyonnesse
yeah, what exactly it does?Blest
The problem is coming from a limitation on how to mix .Net framework and .Net framework. I believe you'll find a short description of the problem plus how to quickly get your binding auto generated by Visual Studio in this github issue. Go for this one for a detailed topo.Cordelier
.Net 4.7.1, migration of WebApi 5.2.3 -> 5.2.7 and the same issue happens over and over. Fix above helped me. thanks!Leblanc
C
2

This seem like a bug with .NET 4.6.1. Anyone who have this issue should upgrade to .NET 4.7.2, otherwise you might encounter similar error with different libraries. Like this one: https://github.com/dotnet/corefx/issues/7702

Cooe answered 18/9, 2019 at 7:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.