Shahrooz Jafari Shahrooz Jafari - 4 years ago 282
C# Question

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

I have added a new Web API project. I install Cors

PM> Install-Package Microsoft.AspNet.WebApi.Cors -Pre


Then when I run my project, I get this error:


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


This is my inner exception:


{"Attempt by method 'System.Web.Http.GlobalConfiguration..cctor()' to
access field
'System.Web.Http.GlobalConfiguration.CS$<>9__CachedAnonymousMethodDelegate2'
failed."}

Answer Source

I came across the same issue and found a blog post on it. According to this blog post if you use the release candidate it should fix the error

Install-Package Microsoft.AspNet.WebApi -IncludePrerelease

From here: http://wp.sjkp.dk/webapi-and-cors-enabled-rest-services/

This worked for me :D

In other words, it was fixed in 5.1.0-rc1 release.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download