Google


   


You are here: CodeIdol.com > C# > C# Cookbook, 2nd Edition > Exception Handling > Debugging Problems When Loading An Assembly

SAVE
Digg
Shown on del.icio.us del.icio.us
See Whos Talking About This on Technorati Technorati
I've Reddit reddit

Recipe 7.7. Debugging Problems When Loading an Assembly

Problem

You want to use a reflection-based technique, such as the static Assembly.LoadFrom method, to load an assembly. If this method fails, you want to collect as much useful information as you can as to why the assembly failed to load.

Solution

Either call the ToString method on the exception object or use the FusionLog property on BadImageFormatException, FileLoadException, or FileNotFoundException. When an exception occurs while using a file, the exception contains extra information that is taken from the fusion log. To see this in action, run the following code:

	public static void LoadMissingDLL( )
	{
	    // Load the DLL.
	    try
	    {
	        Assembly reflectedAssembly = Assembly.LoadFrom("BadFileName.dll");
	    }
	    catch (FileNotFoundException fnf)
	    {
	        // This displays the fusion log information only.
	        Console.WriteLine(fnf.FusionLog);
	    }
	    catch (Exception e)     // Note that you would use one catch block or the other,
	                           // not both.
	        // This displays the exception information along
	        // with any fusion log information.
	        Console.WriteLine(e.ToString( ));
	    }
	}

Discussion

Use this technique to debug problems when loading an assembly from a file. When using the ToString method of the Exception object, notice the bottom part of the error message that starts with "Fusion log follows." This is the section that can provide some clue as to why the reflection APIs could not find your assembly. If you want just the fusion information, you can use the FusionLog property of one of the aforementioned exception objects. The Assembly Binding Log Viewer (fuslogvw.exe) is another place where the load failure information can be retrieved. In order for this log to be filled, the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ Fusion|ForceLog and LogFailures entries should be specified as type DWORD with a value of 1 for each entry. Without these entries, the log will not record failure information, and it will only be available from the FusionLog property.

See Also

See the "BadImageFormatException Class," "FileLoadException Class," and "File-NotFoundException Class" topics in the MSDN documentation.


SAVE
Digg
Shown on del.icio.us del.icio.us
See Whos Talking About This on Technorati Technorati
I've Reddit reddit

You are here: CodeIdol.com > C# > C# Cookbook, 2nd Edition > Exception Handling > Debugging Problems When Loading An Assembly


ADBRITE ads links
   
Related tags







Popular Categories
Unix books and guides

AJAX popular information
C# language guides
Windows books and cookbooks

.......








Business Key Top Sites

be number one
rate your site




    С 2009 года мы стали переводить структура сайта на различные языки. Сайт теперь будет содержать книги не только на английском языке, но также и на других европейских языках, в том числе и на Русском языке.

    Русский Polski Francais Deutsch
    support sitemap terms

© CodeIdol Labs, 2007 - 2009