I'm getting the following error after doing some project restructuring and I can't nail down where the problem lies.
Server Error in '/' Application.
Attempt by method 'System.Web.Http.HttpConfiguration..ctor(System.Web.Http.HttpRouteCollection)' to access method 'System.Web.Http.HttpConfiguration.DefaultFormatters()' failed.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.MethodAccessException: Attempt by method 'System.Web.Http.HttpConfiguration..ctor(System.Web.Http.HttpRouteCollection)' to access method 'System.Web.Http.HttpConfiguration.DefaultFormatters()' failed.
Source Error:
Line 15: routes.IgnoreRoute( "{resource}.axd/{*pathInfo}" );
Line 16:
Line 17: routes.MapHttpRoute(
Line 18: name: "DefaultApi",
Line 19: routeTemplate: "api/{controller}/{action}",
Source File: c:\Dev\MyProject\Branches\Phase.2.3-Sprint 16\Client\ManagementSite\App_Start\RouteConfig.cs Line: 17
Stack Trace:
[MethodAccessException: Attempt by method 'System.Web.Http.HttpConfiguration..ctor(System.Web.Http.HttpRouteCollection)' to access method 'System.Web.Http.HttpConfiguration.DefaultFormatters()' failed.]
System.Web.Http.HttpConfiguration..ctor(HttpRouteCollection routes) +53
System.Web.Http.GlobalConfiguration.<.cctor>b__0() +54
System.Lazy`1.CreateValue() +416
System.Lazy`1.LazyInitValue() +152
System.Lazy`1.get_Value() +75
System.Web.Http.GlobalConfiguration.get_Configuration() +27
System.Web.Http.RouteCollectionExtensions.MapHttpRoute(RouteCollection routes, String name, String routeTemplate, Object defaults, Object constraints, HttpMessageHandler handler) +67
System.Web.Http.RouteCollectionExtensions.MapHttpRoute(RouteCollection routes, String name, String routeTemplate, Object defaults) +18
MyProjectManagementSite.RouteConfig.RegisterRoutes(RouteCollection routes) in c:\Dev\MyProject\Branches\Phase.2.3-Sprint 16\Client\ManagementSite\App_Start\RouteConfig.cs:17
MyProjectManagementSite.MvcApplication.Application_Start() in c:\Dev\MyProject\Branches\Phase.2.3-Sprint 16\Client\ManagementSite\Global.asax.cs:26
[HttpException (0x80004005): Attempt by method 'System.Web.Http.HttpConfiguration..ctor(System.Web.Http.HttpRouteCollection)' to access method 'System.Web.Http.HttpConfiguration.DefaultFormatters()' failed.]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +9905705
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118
System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +172
System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +336
System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +296
[HttpException (0x80004005): Attempt by method 'System.Web.Http.HttpConfiguration..ctor(System.Web.Http.HttpRouteCollection)' to access method 'System.Web.Http.HttpConfiguration.DefaultFormatters()' failed.]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9885060
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34009
I tied down all the projects to .Net 4.5 and set Newtonsoft Json.Net to 6.0.1 across the board (instead of 6.0.7 which is the latest). When I build my web project I get a warning "Found conflicts between different versions of the same dependent assembly" and if I run some borrowed code to list those potential conflicts it appears that there could be a conflict between version 4.5 and 6.0 of Newtonsoft:
Possible conflicts for System.Net.Http:
System.Net.Http.Formatting references System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.ServiceModel references System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Web.Http references System.Net.Http, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Web.Http.WebHost references System.Net.Http, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
MyProjectManagementSite references System.Net.Http, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
MyProjectManagementSite references System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Possible conflicts for Newtonsoft.Json:
System.Net.Http.Formatting references Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
System.Web.Http references Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
MyProject.Base references Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
MyProjectManagementSite references Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
Does the above show up the likely cause, and how can I resolve it?
Uninstalled WebApi and reinstalled again (via Nuget) and the problem went away
Most likely it's a versioning problem, I had the same issue and was fixed by updating Microsoft.AspNet.WebApi.WebHost
package to the latest version
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With