Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mono - System.TypeLoadException: Could not load types

After recently upgrading to Xamarin Studio 6 and Mono 4.4.0.182 (running on OSX 10.10.5) we found that our application no longer runs (Asp.NET MVC/Razor website). The solution does compile correctly however.
We've tried reverting back to the previous versions which did work - Xamarin Studio 5.10.3 and Mono 4.3.2, and our application still does not run correctly. The exact same application was working fine prior to the update.
No other environment/code changes have been made, and restarts have also not worked.

Errors being generated after the update.

1st Error (on application start): System.Reflection.ReflectionTypeLoadException
This is being triggered by SimpleInjector Container.RegisterPackages(). This was working prior to the Xamarin/Mono update, and no code changes/package updates have been applied/made.

Could not load type 'System.Net.HttpListener' from assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
Could not load type 'System.Net.HttpListenerPrefixCollection' from assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
Could not load type 'System.Net.HttpWebRequest' from assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
Could not load type 'System.Net.Security.SslStream' from assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
Could not load type 'System.Net.WebSockets.ClientWebSocket' from assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

Stacktrace

at (wrapper managed-to-native) System.Reflection.Assembly:GetTypes (System.Reflection.Assembly,bool) at System.Reflection.Assembly.GetExportedTypes () [0x00000] in /private/tmp/source-mono-4.3.2/bockbuild-xamarin/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/corlib/System.Reflection/Assembly.cs:407 at SimpleInjector.PackageExtensions.GetExportedTypesFrom (System.Reflection.Assembly assembly) [0x00000] in <filename unknown>:0
at SimpleInjector.PackageExtensions+<>c.<RegisterPackages>b__1_0 (System.Reflection.Assembly assembly) [0x00000] in <filename unknown>:0
at System.Linq.Enumerable+<SelectManyIterator>c__Iterator5`3[TSource,TCollection,TResult].MoveNext () [0x00059] in <filename unknown>:0
at System.Linq.Enumerable+WhereSelectEnumerableIterator`2[TSource,TResult].MoveNext () [0x00078] in <filename unknown>:0
at System.Linq.Buffer`1[TElement]..ctor (IEnumerable`1 source) [0x00087] in <filename unknown>:0
at System.Linq.Enumerable.ToArray[TSource] (IEnumerable`1 source) [0x00011] in <filename unknown>:0
at SimpleInjector.PackageExtensions.RegisterPackages (SimpleInjector.Container container, IEnumerable`1 assemblies) [0x000f0] in <filename unknown>:0
at SimpleInjector.PackageExtensions.RegisterPackages (SimpleInjector.Container container) [0x0002f] in <filename unknown>:0
at MyApplication.Web.UI.MvcApplication.InitializeContainer (SimpleInjector.Container container) [0x00003] in /Users/*sanitized*/MyApplication.Web.UI/Global.asax.cs:57  

2nd Error (After page reload and any subsequent page requests): System.ArgumentException An item with the same key has already been added.
This is referencing a System.Web.Mvc.RouteCollectionExtensions.MapRoute call in our App_Start/RouteConfig.cs file (called in turn from Global.asax.cs, Application_Start), indicating that this file is being called at least twice. Again, this was working prior to the recent Xamarin/Mono update.

Can anyone provide any assistance and/or suggestions?

EDIT
Rolled back to Mono 4.2.4.4, and the error has gone away. Looks like there has been a change introduced into 4.3.2 at some point that has introduced this bug.

like image 418
Alex Bunn Avatar asked Jun 12 '16 22:06

Alex Bunn


1 Answers

I had a similar issue:

Could not load type 'System.Net.HttpListener' from assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

I solved adding Mono.Security to the executable project and rebuilding it. Hope this helps

like image 92
mauro Avatar answered Nov 10 '22 01:11

mauro