Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a Web API solution (targeting .NET 4.6) with a couple of fairly lightweight .NET Core projects in it. I've packaged the .NET Core projects up as a NuGet package and installed them to the Web API project.

Everything builds fine, but when running it, I get the following exception when the application is initialising.

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

I've checked in Object Browser, and MediaTypeHeaderValue does implement ICloneable. Any ideas what could be causing this?

I should also say that it's fine when I replace the .NET Core projects with .NET 4.6 versions.

Edit

As per Johnathan's response, I managed to get it working by updating project.json to use 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"
    }
  }
}
like image 639
Tom Avatar asked Jul 13 '16 15:07

Tom


2 Answers

It was not immediately clear to me what to do when I read the correct answer above - For those running into the same issue : just change/add the mapping in your app.config / web.config in the configuration/runtime/assemblyBinding section:

  <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>

UPDATE

.Net core team updated System.Net.Http package to 4.3.1 on 21/2/2017. So if you can update you shouldn't need this redirect anymore.

Details of the issue: https://github.com/dotnet/corefx/issues/11100

like image 119
milanio Avatar answered Oct 16 '22 04:10

milanio


It's an issue with the latest NuGet version of System.Net.Http. For now, either downgrade the System.Net.Http to v4.0.0.0 or use the version built into Framework 4.6.

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

like image 37
Jonathan Avatar answered Oct 16 '22 04:10

Jonathan