Tom Tom - 1 month ago 122
C# Question

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

Answer

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