Could not load type 'System.Web.Http.WebHost.R

2019-07-03 22:10发布

I tried to use latest Asp.Net Web API in Mono, but the error shows like this:

System.TypeLoadException
Could not load type 'System.Web.Http.WebHost.Routing.HttpRouteExceptionHandler' from assembly 'System.Web.Http.WebHost, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

Description: HTTP 500.Error processing request.
Details: Non-web exception. Exception origin (name of application or object): System.Web.
Exception stack trace:
  at System.Web.Routing.UrlRoutingModule.PostResolveRequestCache (System.Web.HttpContextBase context) [0x00054] in /Volumes/build-root-ramdisk/mono-3.4.0/mcs/class/System.Web.Routing/System.Web.Routing/UrlRoutingModule.cs:128 
  at System.Web.Routing.UrlRoutingModule.PostResolveRequestCache (System.Object o, System.EventArgs e) [0x00007] in /Volumes/build-root-ramdisk/mono-3.4.0/mcs/class/System.Web.Routing/System.Web.Routing/UrlRoutingModule.cs:88 
  at System.Web.HttpApplication+<RunHooks>c__Iterator0.MoveNext () [0x001ba] in /Volumes/build-root-ramdisk/mono-3.4.0/mcs/class/System.Web/System.Web/HttpApplication.cs:1050 
  at System.Web.HttpApplication+<Pipeline>c__Iterator1.MoveNext () [0x0073b] in /Volumes/build-root-ramdisk/mono-3.4.0/mcs/class/System.Web/System.Web/HttpApplication.cs:1260 
  at System.Web.HttpApplication.Tick () [0x00000] in /Volumes/build-root-ramdisk/mono-3.4.0/mcs/class/System.Web/System.Web/HttpApplication.cs:932 

Version Information: 3.4.0 ((no/c3fc3ba Thu May 1 22:52:17 EDT 2014); ASP.NET Version: 4.0.30319.17020

And what dlls I should include via Nuget? Currently I referenced these assemblies:

- System (from GAC)
- System.Web (from GAC)
- System.Web.Http (from Nuget)
- System.Net.Http.Formatting (from Nuget)
- System.Web.Http.WebHost (from Nuget)
- Newtonsoft.Json (from Nuget)

3条回答
ゆ 、 Hurt°
2楼-- · 2019-07-03 22:55

This possibly can be gotten around by simply not using config.MapHttpAttributeRoutes();, but instead specifying Web API routes manually (e.g. config.Routes.MapHttpRoute...).

查看更多
【Aperson】
3楼-- · 2019-07-03 23:00

I guess I'm adding my answer too late, but I just had similar issue, and to debug, I added following line to source code:

var httpHost = new System.Web.Http.WebHost.HttpControllerHandler(null);

lo and behold, the compilation failed with (line breaks are mine here):

Global.asax.cs(116,56): error CS1684: Reference to type
 `System.Web.HttpTaskAsyncHandler' claims it is defined assembly 
 `System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a',
  but it could not be found

so, basically - yes. Busted System.Web.dll

查看更多
霸刀☆藐视天下
4楼-- · 2019-07-03 23:06

There is a similar bug in Xamarin bugzilla. Apparently Mono aspnetwebstack needs to be updated first in order to use latest Web API libraries:

https://bugzilla.xamarin.com/show_bug.cgi?id=19314

查看更多
登录 后发表回答