29

I have created a solution with two projects: A MVC 4 and a Class Library. The solution compiles and runs fine. I then install-package 'Microsoft.AspNet.WebApi.OData -pre to pull in OData WebApi functionality. When I do, I get the following error at runtime:

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

When WebApiConfig.Register(GlobalConfiguration.Configuration) runs in Global.asax

Once I do, uninstalling the package doesn't seem to fix. It removes the OData components obviously, but then leaves whatever assembly is causing this. removing with -RemoveDependencies removes ALL WebApi references in the library, and I'm unable to get it back in a working state.

What package hell am I in and how can I enable WebApi OData in a vanilla MVC4 app?

Daniel
  • 3,021
  • 5
  • 35
  • 50

7 Answers7

34

It's a mismatch on the assemblies for the given version of WebAPI. Mine was failing because I was referencing a mismatched version of System.Web.Http.WebHost. So you can...

  1. Install the latest WebAPI from NuGet

    -OR-

  2. Make sure all your Microsoft.AspNet.WebApi.* packages are on the same version

Nexxas
  • 883
  • 1
  • 8
  • 14
16

I ran "update-package Microsoft.AspNet.WebApi" in the Package Manager Console in VS and worked like a charm.

Mav55
  • 161
  • 1
  • 3
11

If you create an MVC 4 app from its template, then you are installing Web API 4.0. if you use

install-package 'Microsoft.AspNet.WebApi.OData -pre

You will be dependent on ASP.NET Web API 5.0 rc.

My suggestion is to use "Empty ASP.NET web application" template rather than MVC 4.0 template.

Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • 6
    This set me on the right track. I ended up doing Install-Package Microsoft.AspNet.WebApi -Pre and it was happy. – Daniel Aug 28 '13 at 14:10
  • 4
    Got the same error after installing `Microsoft.AspNet.WebApi.Cors` on VS2012 Web API template and editing the `WebApiConfig.Register`. After upgrading to VS2013 template all worked properly. – Ilia Barahovsky Mar 04 '14 at 07:06
5

I was facing same issue and fix is to use same version of "System.Web.Http.WebHost" as Syste.Web.Http which is 5.2.6

Govind
  • 59
  • 1
  • 2
3

Do you need the pre-release version of web API OData? If so, you need to update all the other web API packages to match that version. If not, just do,

install-package 'Microsoft.AspNet.WebApi.OData

without the -pre option

RaghuRam Nadiminti
  • 6,718
  • 1
  • 33
  • 30
2

If this package don't exist from the answers above you can install it

install-package Microsoft.AspNet.WebApi

0

I faced issue and I noticed, previous server code build with Release mode and I am uploading build with Debug mode code. So, I replaced with Release mode and then issue is resolved.

Arun Solanki
  • 174
  • 11