尝试方法... HttpConfiguration ...访问方法... DefaultFormatters ...失败

时间:2015-01-05 13:18:24

标签: asp.net-mvc

我在做一些项目重组后遇到以下错误,我无法确定问题所在。

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 

我将所有项目绑定到.Net 4.5并将Newtonsoft Json.Net设置为6.0.1(而不是最新的6.0.7)。当我构建我的Web项目时,我收到警告“发现同一个依赖程序集的不同版本之间发生冲突”,如果我运行一些借用的代码来列出这些潜在的冲突,那么看起来Newtonsoft的版本4.5和6.0之间可能存在冲突:

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

以上是否显示了可能的原因,我该如何解决?

2 个答案:

答案 0 :(得分:6)

卸载WebApi并重新安装(通过Nuget)并且问题消失了

答案 1 :(得分:3)

很可能是版本问题,我遇到了同样的问题,并通过将Microsoft.AspNet.WebApi.WebHost包更新到最新版本来修复