添加.NET Standard 2.0依赖项后找不到System.Net.Http.Formatting.MediaTypeFormatter.get_SupportedMediaTypes()的方法

时间:2017-09-20 12:57:32

标签: c# .net-standard-2.0

我有一个.NET Framework 4.6.1 WebApi项目,它引用了我们在内部用来共享常用实用程序方法的小型NuGet包。

我们希望开始将部分内容移至.NET Core,因此我将实用程序包更改为目标.NET Standard 2.0。这是通过简单地创建一个新的.NET Standard 2.0项目并复制源文件来完成的。

实用程序包csproj:

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

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

</Project>

在我的WebApi项目中更新包后,我在启动时遇到以下异常:

[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

唯一的变化是packages.config和csproj中的版本号。

有什么想法吗?

谢谢!

2 个答案:

答案 0 :(得分:72)

原来只需要一个绑定重定向到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>

答案 1 :(得分:0)

这似乎是.NET 4.6.1的错误。有此问题的任何人都应升级到.NET 4.7.2,否则,使用其他库可能会遇到类似的错误。像这样一个:https://github.com/dotnet/corefx/issues/7702