147

I get a error message while updating my service reference:

Custom tool warning: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

How can I retrieve the LoaderException property?

Update: My errors went away when I reimported the domain object projects. I have no idea why this fixed the issue, but I'm happy it's working.

rozon
  • 2,518
  • 4
  • 23
  • 37
  • 6
    How did you manage to fix this? What do you mean by "reimporting domain object project"? (remove reference to the project and re-add it again?) – Nikos Tsokos Jun 01 '12 at 08:13

4 Answers4

159
try
{
  // load the assembly or type
}
catch (Exception ex)
{
  if (ex is System.Reflection.ReflectionTypeLoadException)
  {
    var typeLoadException = ex as ReflectionTypeLoadException;
    var loaderExceptions  = typeLoadException.LoaderExceptions;
  }
}
KBoek
  • 5,794
  • 5
  • 32
  • 49
  • I'm not known to write fault free code, so there might be problems in the service. However when I generate code with the svcutil I get code that seems correct with my types reused (witch is my main issue actually) – rozon Jan 12 '11 at 11:25
  • 1
    I marked you as answer because I think it will be most helpful for others experiencing this warning/error. – rozon Jan 14 '11 at 06:37
  • 3
    I'm trying to use this solution, but I have no entry point in my WCF service around which to actually wrap a try block. – Jordan Nov 21 '12 at 19:47
  • @Jordan Send me (some of) the code in a PM and I'll have a look – KBoek Nov 23 '12 at 09:19
  • @KBoek, nevermind. I figured out the issue. It was a configuration problem with my end points. – Jordan Nov 27 '12 at 14:45
  • 3
    This answer would work for me if it had anything to do with my code. I've placed a `try...catch` around my entire `Installer.cs` class, and nothing! My code does not throw this when it runs, only when I try to execute the Installer. Does anyone know **How to Retrieve the LoaderExceptions property for more information?** Like, browse to a folder and view a log file? –  Mar 18 '13 at 13:46
  • 6
    The catch clause will catch all exceptions but only handle ReflectionTypeLoadExceptions. Something like this would be better. `catch (ReflectionTypeLoadException ex) { var typeLoadException = ex as ReflectionTypeLoadException; var loaderExceptions = typeLoadException.LoaderExceptions; }` – Scott Munro Oct 17 '13 at 12:19
  • 3
    @ScottMunro: If you're catching it as one type, why create another variable with the same type and do a cast? This should suffice: `catch (ReflectionTypeLoadException ex) { var loaderExceptions = ex.LoaderExceptions; }`. Also, unless you expect the cast to fail and will check for null, it's better to do a direct cast so it will fail immediately and not later with a null reference exception: `var typeLoadException = (ReflectionTypeLoadException)ex;` – Nelson Rothermel Aug 08 '14 at 16:07
  • @NelsonRothermel My point was that it would be better to catch a specific exception rather than Exception. I did not need to change any of the code inside the conditional to make this point but your code is obviously an improvement. – Scott Munro Aug 11 '14 at 01:30
  • @ScottMunro Fair enough. :) – Nelson Rothermel Aug 12 '14 at 02:28
  • It doesn't even run for christ sakes. "var" undefined. Removed the var, and set the $ before the variables. Still doesn't save anything to the variables. – Owl Jul 14 '17 at 12:13
  • 1
    @Owl, putting $ in front of the variables is so PHP ... And "var" is supported since .NET 3.0, see https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var – KBoek Jul 14 '17 at 16:17
  • KBoek: Well i actually like $ variables - as a perl / bash programmer, not sure why I couldn't get this working, I suspect as you mentioned wrong version of .net... – Owl Jul 20 '17 at 12:23
83
catch (ReflectionTypeLoadException ex)
{        
    foreach (var item in ex.LoaderExceptions)
    {
          MessageBox.Show(item.Message);                    
    }
}

I'm sorry for resurrecting an old thread, but wanted to post a different solution to pull the loader exception (Using the actual ReflectionTypeLoadException) for anybody else to come across this.

Jacob Saylor
  • 2,371
  • 1
  • 31
  • 46
  • 2
    Just an FYI, if you're running a Win Service (like I am), MessageBox will not appear as UI elements are automatically blocked. Rest of implementation was helpful. Just saved to a log file instead. Thanks. – Vippy Sep 16 '14 at 21:42
  • 2
    "The catch statement is missing its statement block". God, I hate powershell. – Owl Jul 14 '17 at 12:24
  • Many thanks, this solution is accurate as it pin points the exact problem. No point in catching the exception and do nothing. – Muhammad Ashhar Hasan Jan 26 '19 at 08:10
10

Using Quick Watch in Visual Studio you can access the LoaderExceptions from ViewDetails of the thrown exception like this:

($exception).LoaderExceptions
hugo4711
  • 113
  • 1
  • 4
  • Thanks! Makes me wonder why not include that information without the need to access `LoaderExceptions`, as it actually contains relevant information. – GuiRitter Apr 30 '20 at 19:06
  • Not all Exceptions have the LoaderExceptions property. Make sure you are casting or catching an exception of the appropriate type. – Suncat2000 Jun 28 '22 at 17:14
2

Another Alternative for those who are probing around and/or in interactive mode:

$Error[0].Exception.LoaderExceptions

Note: [0] grabs the most recent Error from the stack

techsaint
  • 752
  • 9
  • 22