Tuesday, September 9, 2014

The type initializer for 'System.Web.Http.GlobalConfiguration' threw an exception ASP.NET MVC5 Upgrade

I upgraded my recent project to include Authentiacation using OWIN  and so changed my framework assemblies to web api 2 compatible/ MVC5. The following issue ruined my day

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


System.TypeInitializationException was unhandled by user code
  HResult=-2146233036
  Message=The type initializer for 'System.Web.Http.GlobalConfiguration' threw an exception.
  Source=System.Web.Http.WebHost
  TypeName=System.Web.Http.GlobalConfiguration
  StackTrace:
       at System.Web.Http.GlobalConfiguration.get_Configuration()
       at VOnline.VBuy.Web.MvcApplication.RegisterAutoFacComponents() in d:\LocalRepository\VBuy\VOnline.VBuy\VOnline.VBuy\Global.asax.cs:line 50
       at VOnline.VBuy.Web.MvcApplication.Application_Start() in d:\LocalRepository\VBuy\VOnline.VBuy\VOnline.VBuy\Global.asax.cs:line 23
  InnerException: System.FieldAccessException
       HResult=-2146233081
       Message=Attempt by method 'System.Web.Http.GlobalConfiguration..cctor()' to access field 'System.Web.Http.GlobalConfiguration.CS$<>9__CachedAnonymousMethodDelegate2' failed.
       Source=System.Web.Http.WebHost
       StackTrace:
            at System.Web.Http.GlobalConfiguration..cctor()
       InnerException:

I tried a lot of options mentioned in various sites like including Cors of webapi and also MVC, re installed nuget packages after deleting etc.. and finally the below fix solved the issue.


Issue resolved:- System.Web.Http.WebHost is still pointing to old dll, I found it when debugging. I should have found this earlier, but as the issue happned after lot of migrations like
inclusion of Owin startup file etc.. got confused on what might have caused the issue.

I had Microsoft.ASP.NET.WebApi 2.2 already. However, reintalling the same after a clean did the trick.

1 comment:

Relicc said...

Yep, this helped me out. Thanks