The type initializer for 'System.Web.Http. GlobalConfiguration' threw an exception.

L Paul
L Paul
8 Points
4 Posts

Hi,

I have created web api ( using ApiController) in existing mvc application. It is working fine. But I want to access web api from other web site through ajax. For I have installed cors and it's added reference assemble System.Web.Http.Cors. It has version 5.2.3.

After this installation I am getting following error:

Server Error in '/' Application. 
Attempt by method 'System.Web.Http .GlobalConfiguration..cctor()' to access field System.Web.Http.Global Configuration.CS$<>9__Cached AnonymousMethodDelegate2 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.FieldAccessException: Attempt by method 'System.Web.Http.GlobalConfiguration. .cctor()' to access field 'System.Web.Http.GlobalConfiguration .CS$<>9__CachedAnonymousMethodDelegate2' failed.
Source Error:

Line 20: {
Line 21: AreaRegistration.RegisterAllAreas();
Line 22: WebApiConfig.Register(GlobalConfiguration.Configuration);
Line 23: FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
Line 24: RouteConfig.RegisterRoutes(RouteTable.Routes);
Source File: d:\Application Software\Web Application\c#\MVC\VS2012\WebSolution\WebSolution\Global.asax.cs Line: 22
Stack Trace:

[FieldAccessException: Attempt by method 'System.Web.Http.GlobalConfiguration..cctor()' to access field 'System.Web.Http.GlobalConfiguration.CS$<>9__CachedAnonymousMethodDelegate2' failed.]
System.Web.Http.GlobalConfiguration..cctor() +48
[TypeInitializationException: The type initializer for 'System.Web.Http.GlobalConfiguration' threw an exception.]
System.Web.Http.GlobalConfiguration.get_Configuration() +0
WebSolution.MvcApplication.Application_Start() in d:\Application Software\Web Application\c#\MVC\VS2012\WebSolution\WebSolution\Global.asax.cs:22
[HttpException (0x80004005): The type initializer for 'System.Web.Http.GlobalConfiguration' threw an exception.]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +9859725
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): The type initializer for 'System.Web.Http.GlobalConfiguration' threw an exception.]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9873912
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.17929

Please help.

Thanks in advance.

Views: 20143
Total Answered: 5
Total Marked As Answer: 2
Posted On: 03-Oct-2015 10:14

Share:   fb twitter linkedin
Answers
Brian
Brian
2482 Points
14 Posts
         

Hi L Paul,

I came across the same issue and research on it from different blogs and forums. Since we have installed WebApi.Cors, it will install the latest versions. When I include pre release version in installation, then this error disappeard. I think you need to do following:

In the package manager console execute following command

PM> Install-Package Microsoft.AspNet.WebApi -Pre
PM> Install-Package Microsoft.AspNet.WebApi.Client -Pre
PM> Install-Package Microsoft.AspNet.WebApi.Core -Pre

 It will include pre release versons.

Posted On: 05-Oct-2015 02:54
I faced same problem. great thanks...
 - nik  25-Aug-2017 00:52
Smith
Smith
2984 Points
80 Posts
         

great!! finally found the solution. Works perfectly, thanks...

Posted On: 28-Nov-2015 05:58
Priya
Priya
1416 Points
41 Posts
         

Hi,

I was facing same problem. I solved by re-installing the package.

Thanks...

Posted On: 21-Dec-2015 23:21
Mayank
Mayank
194 Points
12 Posts
         

I was facing same issues. Errors solved by including pre release option in console manager for web api:

Install-Package Microsoft.AspNet.WebApi -IncludePrerelease

This worked for me.

Posted On: 01-Jan-2016 04:33
sam
sam
378 Points
48 Posts
         

This worked for me!!! Thanks so much!!

Posted On: 09-Jan-2016 05:34
 Log In to Chat