类型参数 'System.Net.Http.Headers.MediaTypeHeaderValue' 违反了类型参数 'T' 的约束

Type argument 'System.Net.Http.Headers.MediaTypeHeaderValue' violates the constraint of type parameter 'T'

我有一个 Web API 解决方案(针对 .NET 4.6),其中包含几个相当轻量级的 .NET Core 项目。我已将 .NET Core 项目打包为 NuGet 包并将它们安装到 Web API 项目。

一切正常,但是当 运行 它时,我在应用程序初始化时遇到以下异常。

Method System.Net.Http.CloneableExtensions.Clone: type argument 'System.Net.Http.Headers.MediaTypeHeaderValue' violates the constraint of type parameter 'T'.

[VerificationException: Method System.Net.Http.CloneableExtensions.Clone: type argument 'System.Net.Http.Headers.MediaTypeHeaderValue' violates the constraint of type parameter 'T'.]
   System.Net.Http.Formatting.MediaTypeConstants.get_ApplicationJsonMediaType() +0
   System.Net.Http.Formatting.JsonMediaTypeFormatter..ctor() +64
   System.Net.Http.Formatting.MediaTypeFormatterCollection.CreateDefaultFormatters() +41
   System.Web.Http.HttpConfiguration.DefaultFormatters(HttpConfiguration config) +26
   System.Web.Http.HttpConfiguration..ctor(HttpRouteCollection routes) +214
   System.Web.Http.GlobalConfiguration.<CreateConfiguration>b__0() +60
   System.Lazy`1.CreateValue() +411
   System.Lazy`1.LazyInitValue() +183
   System.Lazy`1.get_Value() +75
   System.Web.Http.GlobalConfiguration.get_Configuration() +27
   Runpath.Platform.Web.DependencyResolution.StructureMapBootStrapper.Initialise() in C:\Code3\Runpath\Markets\Platform\Main - Copy\Runpath.Platform.Web\DependencyResolution\StructureMapBootStrapper.cs:15
   Runpath.Platform.Web.WebApiApplication.Application_Start() in C:\Code3\Runpath\Markets\Platform\Main - Copy\Runpath.Platform.Web\Global.asax.cs:30

[HttpException (0x80004005): Method System.Net.Http.CloneableExtensions.Clone: type argument 'System.Net.Http.Headers.MediaTypeHeaderValue' violates the constraint of type parameter 'T'.]
   System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +493
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +176
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +364
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +303

[HttpException (0x80004005): Method System.Net.Http.CloneableExtensions.Clone: type argument 'System.Net.Http.Headers.MediaTypeHeaderValue' violates the constraint of type parameter 'T'.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +770
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +95
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +195

我已经在对象浏览器中检查过,MediaTypeHeaderValue 确实实现了 ICloneable。知道是什么原因造成的吗?

我还应该说,当我用 .NET 4.6 版本替换 .NET Core 项目时没问题。

编辑

根据 Johnathan 的回复,我设法通过更新 project.json 以使用 System.Net.Http 4.0.0.0 for .NET 4.6 使其工作:

{
  "version": "1.0.3-*",

  "dependencies": {
    "NETStandard.Library": "1.6.0"
  },

  "frameworks": {
    "net46": {
      "dependencies": {
        "System.Net.Http": "4.0.0"
      }
    },
    "netstandard1.6": {
      "imports": "dnxcore50"
    }
  }
}

System.Net.Http 的最新 NuGet 版本存在问题。 现在,要么将 System.Net.Http 降级到 v4.0.0.0,要么使用 Framework 4.6 中内置的版本。

https://github.com/dotnet/corefx/issues/9884

当我阅读上面的正确答案时,我并没有立即清楚该怎么做 - 对于那些 运行 遇到同样问题的人:只是 change/add 你 app.config 中的映射/ web.config 在 configuration/runtime/assemblyBinding 部分:

  <dependentAssembly>
    <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.0.0.0" />
  </dependentAssembly>

更新

.Net 核心团队于 2017 年 2 月 21 日将 System.Net.Http 软件包更新为 4.3.1。 因此,如果您可以更新,则不再需要此重定向。

问题详情:https://github.com/dotnet/corefx/issues/11100

升级到 System.Net.Http 的 4.3.0 版本为我解决了这个问题

将事件流块包添加到 WebAPI 微服务后,我在 ServiceFabric 应用程序中遇到了类似的错误。我尝试更新单个 System.Net.Http nuGet,但没有用,但后来我更新了所有 nuGet 包,错误消失了。

右键单击项目并选择管理 NuGet 程序包,转到更新并全部更新。这确实主要将我升级到 4.3,但还有其他软件包也需要更新。

希望对您有所帮助:)

using Microsoft.Net.Http.Headers; 代替 using System.Net.Http.Headers; 适合我。