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

Tom*_*Tom 44 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
Run Code Online (Sandbox Code Playgroud)

我已经检查了对象浏览器,并且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"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

mil*_*nio 39

当我阅读上面的正确答案时,我不清楚该怎么做 - 对于那些遇到同样问题的人:只需在配置/运行时/ assemblyBinding部分中更改/添加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>
Run Code Online (Sandbox Code Playgroud)

UPDATE

.Net核心团队在2017年2月21日更新了System.Net.Http包到4.3.1.因此,如果您可以更新,则不再需要此重定向.

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


小智 36

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

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


小智 16

升级到System.Net.Http的4.3.0版为我解决了它