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

时间:2016-07-13 15:11:05

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

我有一个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"
    }
  }
}

5 个答案:

答案 0 :(得分:35)

当我阅读上面的正确答案时,我不清楚该怎么做 - 对于那些遇到同样问题的人:只需在配置/运行时/中​​的app.config / web.config中更改/添加映射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

答案 1 :(得分:34)

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

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

答案 2 :(得分:13)

升级到System.Net.Http版本4.3.0解决了它

答案 3 :(得分:1)

在将eventflow nugget包添加到WebAPI微服务之后,我在ServiceFabric应用程序中遇到了类似的错误。我尝试更新单个System.Net.Http nuGet并且没有工作,但后来我刚刚更新了所有nuGet包,错误就消失了。

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

希望有所帮助:)

答案 4 :(得分:0)

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