【问题标题】:Type argument 'System.Net.Http.Headers.MediaTypeHeaderValue' violates the constraint of type parameter 'T'类型参数“System.Net.Http.Headers.MediaTypeHeaderValue”违反类型参数“T”的约束
【发布时间】:2016-11-16 06:28:02
【问题描述】:

我有一个 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 Core 项目替换为 .NET 4.6 版本时,这很好。

编辑

根据 Johnathan 的回复,我设法通过更新 project.json 以将 System.Net.Http 4.0.0.0 用于 .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"
    }
  }
}

【问题讨论】:

    标签: c# asp.net-web-api .net-core


    【解决方案1】:

    当我阅读上面的正确答案时,我并不清楚该怎么做 - 对于那些遇到相同问题的人:只需在配置/运行时 / 中的 app.config / web.config 中更改/添加映射组装绑定部分:

      <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 核心团队于 21/2/2017 更新了 System.Net.Http 包到 4.3.1。 因此,如果您可以更新,则不再需要此重定向。

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

    【讨论】:

    • 如果您碰巧使用System.Net.Http 4.1 中添加的功能,使用此解决方案可能会遇到运行时问题。正如@Gertjan 提到的那样,最安全的解决方案是升级到 System.Net.Http 4.3。
    • 这实际上并不容易@Igrosales。如果您使用 System.Net.Http 4.3.0,则实际上需要此解决方法。 .net 核心团队在此处跟踪该问题:github.com/dotnet/corefx/issues/11100 这就是您需要此重定向的原因,这就是为什么在某些情况下,当您尝试在完整的 .net 中使用 .net core (pcl) 库时,一切都崩溃了。建议的解决方案是更新到 System.Net.Http 4.3.1
    【解决方案2】:

    这是 System.Net.Http 的最新 NuGet 版本的问题。 目前,要么将 System.Net.Http 降级到 v4.0.0.0,要么使用 Framework 4.6 内置的版本。

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

    【讨论】:

    • 设置为 4.0.0 或转到 .Net 4.6 此处未解决....我在网上阅读了很多内容,但在这里无法修复。你能帮我吗?
    • 这个问题有进展吗?
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

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

      希望有帮助:)

      【讨论】:

        【解决方案5】:

        using Microsoft.Net.Http.Headers; 代替 using System.Net.Http.Headers; 对我有用。

        【讨论】:

          猜你喜欢
          • 2022-11-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-30
          • 1970-01-01
          • 2017-01-25
          相关资源
          最近更新 更多