Attempt by security transparent method 'System.Web.Http.GlobalConfiguration.get_Configuration()' to access security critical type 'System.Web.Http.HttpConfiguration' failed. #1166

Imported from the CodePlex archive for reference purposes. Support for MvcCodeRouting has ended.

Reported on

#Attempt by security transparent method ‘System.Web.Http.GlobalConfiguration.get_Configuration()’ to access security critical type ‘System.Web.Http.HttpConfiguration’ failed.

This is my Owin startup:

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var configuration = GlobalConfiguration.Configuration;
            configuration.MapCodeRoutes(typeof (PingController));
        }
    }

Stacktrace:

[TypeAccessException: Attempt by security transparent method 'System.Web.Http.GlobalConfiguration.get_Configuration()' to access security critical type 'System.Web.Http.HttpConfiguration' failed.]
   System.Web.Http.GlobalConfiguration.get_Configuration() +0
   MvcCodeRouting.Web.Http.WebHost.PreApplicationStartCode.Start() in d:\foss\MvcCodeRouting\src\MvcCodeRouting.Web.Http.WebHost\PreApplicationStartCode.cs:35

[InvalidOperationException: The pre-application start initialization method Start on type MvcCodeRouting.Web.Http.WebHost.PreApplicationStartCode threw an exception with the following error message: Attempt by security transparent method 'System.Web.Http.GlobalConfiguration.get_Configuration()' to access security critical type 'System.Web.Http.HttpConfiguration' failed..]
   System.Web.Compilation.BuildManager.InvokePreStartInitMethodsCore(ICollection`1 methods, Func`1 setHostingEnvironmentCultures) +613
   System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +141
   System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +102
   System.Web.Compilation.BuildManager.ExecutePreAppStart() +157
   System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +531

[HttpException (0x80004005): The pre-application start initialization method Start on type MvcCodeRouting.Web.Http.WebHost.PreApplicationStartCode threw an exception with the following error message: Attempt by security transparent method 'System.Web.Http.GlobalConfiguration.get_Configuration()' to access security critical type 'System.Web.Http.HttpConfiguration' failed..]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9923088
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +90
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +261
Commented on link

A quick Google search suggests it’s a Web API versioning issue.

Commented on link

Yes it is a versioning issue, but not on my side. It seems like the library is compiled against Web Api 4.0.0.0 which is pretty old by now.

The NuGet packages are also listed with no dependencies to Asp.NET Web Api, which is clearly false.

Commented on link

Are you using an assembly redirect from 4.0.0.0 to the version you are using?

Commented on link

Good point. I have to check that next time…