Tom
Tom

Reputation: 1581

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

Upvotes: 46

Views: 24735

Answers (5)

Nour
Nour

Reputation: 136

using Microsoft.Net.Http.Headers; instead of using System.Net.Http.Headers; works for me.

Upvotes: 2

milanio
milanio

Reputation: 4232

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

Upvotes: 42

gperrego
gperrego

Reputation: 354

I had a similar error in a ServiceFabric application after adding the eventflow nugget packages to a WebAPI microservice. I tried updating the individual System.Net.Http nuGet and that didn't work but then I just updated all nuGet packages and the error went away.

Right click on the project and choose Manage NuGet packages, go to updates and update all. This did upgrade me mainly to 4.3 but there were other packages that needed updates also.

Hope that helps :)

Upvotes: 1

Gertjan
Gertjan

Reputation: 529

Upgrade to version 4.3.0 of System.Net.Http solved it for me

Upvotes: 18

Jonathan
Jonathan

Reputation: 386

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

Upvotes: 37

Related Questions