How can I retrieve the LoaderExceptions property from the Exception class? [duplicate] - c#

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.

try
{
// load the assembly or type
}
catch (Exception ex)
{
if (ex is System.Reflection.ReflectionTypeLoadException)
{
var typeLoadException = ex as ReflectionTypeLoadException;
var loaderExceptions = typeLoadException.LoaderExceptions;
}
}

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.

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

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

Related

Where are situated all SharePoint server error codes descriptions?

During execution the code belowe Microsoft.SharePoint.Client.ServerException with ServerErrorCode = -2147024894 has been thrown. But I don't know any information about this code. May be you know where I can find the correspondence between SP ServerErrorCodes and their descriptions ?
private SP.Folder GetFolderByRelativeUrl(string folderRelativePath)
{
var folder = _spClientContext.SpContext.Web.GetFolderByServerRelativeUrl(folderRelativePath);
_spClientContext.SpContext.Load(folder);
_spClientContext.SpContext.ExecuteQuery(); // here exception is thrown
return folder;
}
MSDN has no information about codes' descriptions.
UPDATED:
Full description
Microsoft.SharePoint.Client.ServerException: File Not Found.
at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream)
at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()
...
Finally, by chance I've found.
SharePoint server erros codes
Many common error codes are included in the ClientErrorCodes class:
CSOM: Microsoft.SharePoint.Client.ClientErrorCodes
JSOM: SP.ClientErrorCodes

C# Exception during loading DLL's. Can't find solution

I have a problem, while loading DLL at runtime in C#.
Following code:
foreach (var f in Directory.EnumerateFiles(startFolder, "*.dll", SearchOption.AllDirectories))
{
try
{
var assembly = Assembly.LoadFrom(f);
var types = assembly.GetTypes(); //exception raised here
foreach (var type in types)
{
if (type.GetInterface("IPlayerFactory") != null)
instances.Add((IPlayerFactory)Activator.CreateInstance(type));
}
assembly = null;
}
catch (ReflectionTypeLoadException ex) { /* later, keep reading */}
So exception says:
An exception of type 'System.Reflection.ReflectionTypeLoadException' occurred in
mscorlib.dll but was not handled in user code
Additional information: Unable to load one or more of the requested types. Retrieve the
LoaderExceptions property for more information.
I searched stack a little bit and found this catch to look for more information (source: here)
catch block:
catch (ReflectionTypeLoadException ex)
{
StringBuilder sb = new StringBuilder();
foreach (Exception exSub in ex.LoaderExceptions)
{
sb.AppendLine(exSub.Message);
FileNotFoundException exFileNotFound = exSub as FileNotFoundException;
if (exFileNotFound != null)
{
if (!string.IsNullOrEmpty(exFileNotFound.FusionLog))
{
sb.AppendLine("Fusion Log:");
sb.AppendLine(exFileNotFound.FusionLog);
}
}
sb.AppendLine();
}
string errorMessage = sb.ToString();
//Display or log the error based on your application.
Console.WriteLine(sb);
}
So i managed to extract:
'Battleships.Desktop.vshost.exe' (CLR v4.0.30319: Battleships.Desktop.vshost.exe):
Loaded 'C:\SomePath\some_dll.dll'. Cannot find
or open the PDB file.
A first chance exception of type 'System.Reflection.ReflectionTypeLoadException'
occurred in mscorlib.dll
I tried some solutions but nothing works.
Any fresh ideas?
When you build your code in debug mode, two files are created,one the class library and other the 'debug database' .pdb file.if you are running your code in debug mode, include pdb file as well in bin of your reflection code.
Another thing is to look at the Fusion logs using fuslogvw in command prompt.it may give any runtime failures/dependrncies that are not taken care of.
I received the same arcane error ("Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information") while in Release mode, so amolDotnet's solution didn't apply in my situation. For the benefit of anyone else who stumbles upon the same problem, I'll point out that I managed to fix it by adding a missing Nuget package dependency that the DLL needed. T had previously turned off dependency warnings in the Nuget package console and the DLL had functioned just fine, until I tried to load the assembly & inspect the members through Reflection. I might not have found this alternate solution if krzakov hadn't mentioned a deep inspection of the Exception info.

C# get the directory name from the DirectoryNotFoundException

I made an application that search for some files in some directories. When a directory isn't there it throws the DirectoryNotFoundException. I catch that exception but it doesn't have a DirectoryName property or something like that like the FileNotFoundException (FileName). How can I find the Directory Name from the exception properties?
There's no way to natively do this.
Add this class somewhere to your project:
public static class DirectoryNotFoundExceptionExtentions
{
public static string GetPath(this DirectoryNotFoundException dnfe)
{
System.Text.RegularExpressions.Regex pathMatcher = new System.Text.RegularExpressions.Regex(#"[^']+");
return pathMatcher.Matches(dnfe.Message)[1].Value;
}
}
Catch the exception and use the type extension like this:
catch (DirectoryNotFoundException dnfe)
{
Console.WriteLine(dnfe.GetPath());
}
It looks like a hack, but you can extract the path from the Message property. As for me, I would prefer to check if the directory exists first, by using the Directory.Exists method.
catch (DirectoryNotFoundException e)
{
// Result will be: Could not find a part of the path "C:\incorrect\path".
Console.WriteLine(e.Message);
// Result will be: C:\incorrect\path
Console.WriteLine(e.Message
.Replace("Could not find a part of the path \"", "")
.Replace("\".", ""));
}
It is a little inconsistent that FileNotFoundException has the file name, but DirectoryNotFoundException doesn't have the directory name, isn't it?
Here's a work around: Before you throw the exception, associate the errant directory name using Exception's Data property.
Immediately before you attempt to find files in a directory, save the name of the directory in a variable. Then begin a try block for the code that looks in that directory. You now have the directory name available should that block of code throw.
For example:
// ... somewhere in some method that's about to search a directory.
var dirName = directories[i]; // or something -- how do you get it before you pass it to DirectoryInfo?
try
{
SearchDirectory(dirName); // or a block of code that does the work
}
catch(Exception e)
{
// at this point, you know dirName. You can log it, add it to a list of erroring
// directories, or whatever. You could throw here, or swallow the error after logging it, etc.
}
Check that it exists first with Directory.Exists
If you are only looking to stomp this one bug in your IDE, then you can try doing this:
In Visual Studio, go to Debug -> Exceptions, then check the Thrown box for Common Language Runtime Exceptions. This will take you right to an exception when it happens, instead of waiting to get caught.
Sorry to dig up an old post, but as other has said it is pretty stupid that DirectoryNotFoundException does not have the directory as a property when FileNotFoundException does.
I've put it in as feature request for .NET:
http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/4472498-directorynotfoundexception-should-expose-the-name-
The format of the Message member of the DirectoryNotFoundException thrown from most Directory class methods is "Directory 'input' not found.". It shouldn't be hard to extract the input from this string.
Question thou, why would you need to get the input parameter from the exception if you're the one invoking the method with that exact parameter?

How to retrieve the LoaderException property?

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.
try
{
// load the assembly or type
}
catch (Exception ex)
{
if (ex is System.Reflection.ReflectionTypeLoadException)
{
var typeLoadException = ex as ReflectionTypeLoadException;
var loaderExceptions = typeLoadException.LoaderExceptions;
}
}
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.
Using Quick Watch in Visual Studio you can access the LoaderExceptions from ViewDetails of the thrown exception like this:
($exception).LoaderExceptions
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

Why can't I use the 'AccessDeniedException' namespace?

I get the message that the namespace can't be found when I use the code below. Where does the AccessDeniedException live?
try { ... }
catch (SomeKindOfException ex)
{
MessageBox.Show(ex.Message);
}
catch (AccessDeniedException ex)
{
//Do something else
}
Thanks
I don't think that's the exception you're looking for. The only one with this name (that I can find) is in a Sharepoint namespace. Try attaching the debugger and seeing exactly what the type of the thrown exception is.
The type of the exception is going to vary depending on your context. So for example, if it's an "access denied" when trying to open a file, it could be a FileLoadException, or something similar. If it's encountered because of Code Access Security, it will be SecurityException. And so on.
You may need to give the full namespace on the exception, or have a using statement at the top of your code file so .NET knows where to find the exception you're talking about. If that doesn't work, maybe you need to add the DLL that contains that exception to the "REFERENCES" list in your project.

Categories