“Unable to load one or more of the requested types.”
This annoying exception greeted me this morning…
Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
It occurs when an assembly type was not able to load properly during the start-up of your application hosted in IIS. Specifically, when a dependency is missing. Depending on when the error occurs in the compile of your solution, you may not get a good error in IIS failed request trace, IIS logs proper (a simple 500 result is logged), or any extended logging assemblies you have included in your solution since the error very well could occur before those logging logic libraries have included/loaded.
To correct it you need to find the missing dependency. To find that missing dependency you can stage an add of all binaries using PowerShell. Specifically, all by a Get-ChildItem, that you can properly filter by name as needed, one by one in a “foreach” loop using the Add-Type cmdlet to stage the same .NET load performed at start-up You can closely inspect the details of the exception raised during the load of each to find any missing dependencies. Specifically, inspect the LoaderExceptions property that holds the I/O details holding the “file not found” error [like the error message in IIS suggests].
Here’s a quick script snippet. Modify the file location of your binary folder and/or your gci filter as needed:
$BinPath = "D:\Webroot\wwwroot\bin" try { $Error.Clear() Push-Location $BinPath Get-ChildItem "*.dll" | Foreach { Write-Host $_.Name; $assembly = Add-Type -Path $_.FullName; } } catch { Write-Host -foreground yellow "LoadException"; $Error | format-list -force Write-Host -foreground red $Error[0].Exception.LoaderExceptions; } Write-Host "Complete." Pop-Location
Automated PICS-Label Generation Using PowerShell for IIS 7 HTTP Error 500.19 “Failed to decrypt attribute “password””
Thank you for the help, very nice article!
Smart way of doing this! A lot easier than trying to debug or adding random try catch statements. Thanks!