<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Suzanne Cook's .NET CLR Notes : Loader Debugging Advice</title><link>http://blogs.msdn.com/suzcook/archive/tags/Loader+Debugging+Advice/default.aspx</link><description>Tags: Loader Debugging Advice</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Debugging an InvalidCastException</title><link>http://blogs.msdn.com/suzcook/archive/2004/06/02/debugging-an-invalidcastexception.aspx</link><pubDate>Thu, 03 Jun 2004 04:57:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:147195</guid><dc:creator>Suzanne Cook</dc:creator><slash:comments>11</slash:comments><comments>http://blogs.msdn.com/suzcook/comments/147195.aspx</comments><wfw:commentRss>http://blogs.msdn.com/suzcook/commentrss.aspx?PostID=147195</wfw:commentRss><wfw:comment>http://blogs.msdn.com/suzcook/rsscomments.aspx?PostID=147195</wfw:comment><description>&lt;P&gt;First, obviously, find the two types for which the cast failed and&amp;nbsp;verify that they are the same type or otherwise castable.&lt;/P&gt;
&lt;P&gt;Next,&amp;nbsp;if&amp;nbsp;the type was just&amp;nbsp;deserialized, also &lt;A href="http://blogs.msdn.com/suzcook/archive/2003/05/29/57120.aspx" mce_href="http://blogs.msdn.com/suzcook/archive/2003/05/29/57120.aspx"&gt;verify that its assembly successfully loaded&lt;/A&gt; in the target appdomain.&lt;/P&gt;
&lt;P&gt;If everything seems fine, check to see if the assemblies for those two types are loaded from different locations&amp;nbsp;and in&amp;nbsp;the same appdomain. (The actual cast is done in just one appdomain, even if the exception happens when passing a type between two appdomains.) Even if the bits of those assemblies are totally identical, if they are loaded from different paths, they will be considered different, so their types will be considered different. (See &lt;A href="http://blogs.msdn.com/suzcook/archive/2003/07/21/57232.aspx" mce_href="http://blogs.msdn.com/suzcook/archive/2003/07/21/57232.aspx"&gt;Comparing Already-Loaded Assemblies&lt;/A&gt;.)&lt;/P&gt;
&lt;P&gt;A quick way to check for that is to examine the loaded module window of a debugger to see if that assembly&amp;nbsp;was loaded multiple times. If it was, break on module loads to get the callstack for the unexpected load. If that's inconvenient, try getting the &lt;A href="http://blogs.msdn.com/suzcook/archive/2003/05/29/57120.aspx" mce_href="http://blogs.msdn.com/suzcook/archive/2003/05/29/57120.aspx"&gt;Fusion log&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;Usually, the problem is that:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The assembly is available in the GAC (or the ApplicationBase) and loaded there by static reference (something was compiled against that assembly). 
&lt;LI&gt;It has also been loaded dynamically by path, from another path (LoadFrom(), LoadFile(), etc.). 
&lt;LI&gt;Then, the code tries to cast the type from (2) to the corresponding type from (1). &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;To fix this, once you find the offending caller, you will need to either cause the two types to be loaded from the same assembly from the exact same path, or avoid doing the cast. To decide between the assemblies at paths (1) and (2), see &lt;A href="http://blogs.msdn.com/suzcook/archive/2003/05/29/57143.aspx" mce_href="http://blogs.msdn.com/suzcook/archive/2003/05/29/57143.aspx"&gt;Choosing a Binding Context&lt;/A&gt;. Usually, I recommend using (1) - see &lt;A href="http://blogs.msdn.com/suzcook/archive/2003/06/13/57180.aspx" mce_href="http://blogs.msdn.com/suzcook/archive/2003/06/13/57180.aspx"&gt;Switching to the Load Context&lt;/A&gt; for help with&amp;nbsp;implementing that.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=147195" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/suzcook/archive/tags/Loader+Debugging+Advice/default.aspx">Loader Debugging Advice</category></item><item><title>Debugging a MissingMethodException, MissingFieldException, TypeLoadException</title><link>http://blogs.msdn.com/suzcook/archive/2004/02/13/debugging-a-missingmethodexception-missingfieldexception-typeloadexception.aspx</link><pubDate>Sat, 14 Feb 2004 03:03:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:72728</guid><dc:creator>Suzanne Cook</dc:creator><slash:comments>13</slash:comments><comments>http://blogs.msdn.com/suzcook/comments/72728.aspx</comments><wfw:commentRss>http://blogs.msdn.com/suzcook/commentrss.aspx?PostID=72728</wfw:commentRss><wfw:comment>http://blogs.msdn.com/suzcook/rsscomments.aspx?PostID=72728</wfw:comment><description>&lt;P&gt;Say you've just installed some assemblies from a third party&amp;nbsp;and now you're seeing a MissingMethodException, MissingFieldException, or&amp;nbsp;TypeLoadException (during the run of an application using those assemblies). Below are the common causes.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Loading failures&lt;/STRONG&gt;&lt;BR&gt;First, check for assembly binding failures by &lt;A href="http://blogs.msdn.com/suzcook/archive/2003/05/29/57120.aspx" mce_href="http://blogs.msdn.com/suzcook/archive/2003/05/29/57120.aspx"&gt;getting the Fusion log&lt;/A&gt;. Look for the assembly containing that method/field/type or assemblies containing types referenced by it. If an assembly failed to load, use the instructions at the same link to help resolve that issue.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Unexpected assembly version loaded&lt;/STRONG&gt; &lt;BR&gt;But, if that's not the problem, turn on the Fusion log for everything - not just failures - and check to see if the wrong version of those assemblies is being loaded. Look at the display names requested. Are any requesting outdated versions, even after policy has been applied (see further down in the log for the post-policy display name)? If so, you may want to recompile part of your app so that it has current references.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Loaded from unexpected path&lt;/STRONG&gt; &lt;BR&gt;If that doesn't help, run filever.exe on the file at the path it was loaded from. You can get that from the loaded modules list in a debugger. It's also the last path listed in the Fusion log, for a successful log (if it's in the GAC, no path is listed). Make sure it is the same path as you would expect.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Not in this version of the assembly&lt;/STRONG&gt;&lt;BR&gt;Next, run ildasm.exe on the file &lt;EM&gt;at the path it was loaded from at runtime.&lt;/EM&gt; Make sure that the method/field/type is there and&amp;nbsp;is defined&amp;nbsp;how you would expect it. Maybe the file has changed, adding or removing methods or the like,&amp;nbsp;but the assembly version and location have stayed the same.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Invalid IL&lt;/STRONG&gt; &lt;BR&gt;Now, try running peverify.exe on the file containing the method/field/type using the path it was loaded from at runtime. If it gives an error, check back to see if there was a warning at compile-time for this file.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=72728" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/suzcook/archive/tags/Loader+Debugging+Advice/default.aspx">Loader Debugging Advice</category></item><item><title>Where to Find Technical Support</title><link>http://blogs.msdn.com/suzcook/archive/2003/12/05/where-to-find-technical-support.aspx</link><pubDate>Sat, 06 Dec 2003 02:13:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:57253</guid><dc:creator>Suzanne Cook</dc:creator><slash:comments>13</slash:comments><comments>http://blogs.msdn.com/suzcook/comments/57253.aspx</comments><wfw:commentRss>http://blogs.msdn.com/suzcook/commentrss.aspx?PostID=57253</wfw:commentRss><wfw:comment>http://blogs.msdn.com/suzcook/rsscomments.aspx?PostID=57253</wfw:comment><description>&lt;P&gt;Microsoft's official support website is &lt;A href="http://support.microsoft.com/" mce_href="http://support.microsoft.com/"&gt;http://support.microsoft.com/&lt;/A&gt;. It has all kinds of resources like product FAQs, downloads, searchable KB articles, newsgroup pointers, and ways to reach people to help with your individual needs or feedback. &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;FONT color=#000000&gt;Unfortunately, I can't give attention to individual customer issues.&lt;/FONT&gt;&lt;/STRONG&gt; That's because I work in product design and development, not customer support. Someone has to be focused on that or else we'd never ship anything! So, I'm going to have to leave your questions and comments to MS's official channels which specialize in that. Please keep comments you post here general and about the loader or performance. &lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=57253" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/suzcook/archive/tags/Loader+Debugging+Advice/default.aspx">Loader Debugging Advice</category></item><item><title>Determining the Referencing Assembly</title><link>http://blogs.msdn.com/suzcook/archive/2003/11/14/determining-the-referencing-assembly.aspx</link><pubDate>Sat, 15 Nov 2003 10:23:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:57250</guid><dc:creator>Suzanne Cook</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/suzcook/comments/57250.aspx</comments><wfw:commentRss>http://blogs.msdn.com/suzcook/commentrss.aspx?PostID=57250</wfw:commentRss><wfw:comment>http://blogs.msdn.com/suzcook/rsscomments.aspx?PostID=57250</wfw:comment><description>&lt;P&gt;Say you're debugging your application and you see that version 1.0 of an assembly is being loaded when you thought it should be version 2.0. Where is the reference to 1.0 coming from? &lt;/P&gt;
&lt;P&gt;The easiest way to find out is to look at the &lt;A href="http://blogs.msdn.com/suzcook/archive/2003/05/29/57120.aspx" mce_href="http://blogs.msdn.com/suzcook/archive/2003/05/29/57120.aspx"&gt;Fusion log&lt;/A&gt; for this bind. If the version 1.0 assembly was successfully loaded, use the ForceLog/"Log all binds" option of FusLogVw. Then, look for the line in the log showing the calling assembly: &lt;/P&gt;
&lt;P&gt;&lt;I&gt;Calling assembly : referencingAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=12ab3bf24c56c45b. &lt;/I&gt;&lt;/P&gt;
&lt;P&gt;It shows the display name of the calling assembly when available. It doesn't tell you whether this is a static or a dynamic reference because Fusion doesn't know or care (that doesn't matter for binding purposes). So, this could mean that referencingAssembly was built against the other 1.0 assembly, or that it asked for it at runtime via Assembly.Load(), etc. &lt;/P&gt;
&lt;P&gt;Sometimes the calling assembly is not specified in the log. There are a few possible cases where that happens: 
&lt;UL&gt;
&lt;LI&gt;The assembly was requested by unmanaged code (interop). 
&lt;LI&gt;The calling assembly was in another appdomain (AppDomain.CreateInstance(), etc.). 
&lt;LI&gt;The calling assembly had not been loaded through Fusion (Assembly.Load(byte[]), Assembly.LoadFile(), etc.). &lt;/LI&gt;&lt;/UL&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=57250" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/suzcook/archive/tags/Loader+Debugging+Advice/default.aspx">Loader Debugging Advice</category></item><item><title>ReflectionTypeLoadException</title><link>http://blogs.msdn.com/suzcook/archive/2003/08/11/57236.aspx</link><pubDate>Tue, 12 Aug 2003 01:40:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:57236</guid><dc:creator>Suzanne Cook</dc:creator><slash:comments>11</slash:comments><comments>http://blogs.msdn.com/suzcook/comments/57236.aspx</comments><wfw:commentRss>http://blogs.msdn.com/suzcook/commentrss.aspx?PostID=57236</wfw:commentRss><wfw:comment>http://blogs.msdn.com/suzcook/rsscomments.aspx?PostID=57236</wfw:comment><description>&lt;P&gt;If a type can't be loaded for some reason during a call to Module.GetTypes(), ReflectionTypeLoadException will be thrown. Assembly.GetTypes() also throws this because it calls Module.GetTypes(). &lt;/P&gt;
&lt;P&gt;The Message for this exception is "One or more exceptions have been thrown while loading the types" or "Unable to load one or more of the types in the assembly", which doesn't seem very descriptive. But, the exception actually provides more info that that. Just get the LoaderExceptions property of the ReflectionTypeLoadException instance. That will give an array of the exceptions caught while loading all of the types from the module. If the exceptions are due to an assembly loading problem, see my &lt;a href="http://blogs.msdn.com/suzcook/archive/2003/05/29/57120.aspx"&gt;general debugging advice&lt;/A&gt;. &lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=57236" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/suzcook/archive/tags/Loader+Debugging+Advice/default.aspx">Loader Debugging Advice</category></item><item><title>Debugging Assembly Loading Failures</title><link>http://blogs.msdn.com/suzcook/archive/2003/05/29/debugging-assembly-loading-failures.aspx</link><pubDate>Fri, 30 May 2003 00:09:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:57120</guid><dc:creator>Suzanne Cook</dc:creator><slash:comments>106</slash:comments><comments>http://blogs.msdn.com/suzcook/comments/57120.aspx</comments><wfw:commentRss>http://blogs.msdn.com/suzcook/commentrss.aspx?PostID=57120</wfw:commentRss><wfw:comment>http://blogs.msdn.com/suzcook/rsscomments.aspx?PostID=57120</wfw:comment><description>&lt;P&gt;So...you're seeing a FileNotFoundException, FileLoadException, BadImageFormatException or you suspect an assembly loading failure? Try the steps below to start your debugging process. &lt;/P&gt;
&lt;P&gt;First, get the Message property from the exception. If the exception's InnerException property is set, get the Message property from that. If the message is generic, also get the hresult by calling System.Runtime.InteropServices.Marshal.GetHRForException(), passing in&amp;nbsp;the Exception object. If that info is not readily available, you may need to attach a debugger and set it to catch managed exceptions. Then, get the hresult by retrieving the private _HResult field of the managed Exception object. &lt;/P&gt;
&lt;P&gt;&lt;B&gt;Retrieving the Fusion log&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;If the exception message isn t enough for you to determine what the problem is, try getting the Fusion log. It will describe the binding failure (if this is due to an assembly binding failure, instead of a loading failure after the file is found). The exception may already include the log. If not, to get it, run fuslogvw.exe. If you don't have&amp;nbsp;fuslogvw.exe already, install the Framework SDK.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;For pre-v2.0: click on "Log Failures." If this is an ASP.NET or .NET Windows service app, select the Custom option and&amp;nbsp;using regedit, set [HKLM\Software\Microsoft\Fusion\LogPath] to point to&amp;nbsp;an existing&amp;nbsp;directory (like c:\mylogs, not c:\). If you need to log all binds, not just failing ones, set [HKLM\Software\Microsoft\Fusion\ForceLog] as a DWORD value to 1. 
&lt;LI&gt;For v2: click on "Settings," then choose "Log bind failures to disk" if you only care about the failures or "Log all binds to disk" if you want to see all binding requests. 
&lt;LI&gt;To turn on failure logging during a test run instead of by hand, have your script set [HKLM\Software\Microsoft\Fusion\LogFailures] as a DWORD value to 1 using regedit.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Then, click "OK." Now, re-run the process. Next, click "Refresh" in fuslogvw, and a line should appear for each binding failure (or for each bind, if ForceLog is set). Now, double-click on the line in the "Application" column for the interesting bind, and a web page should come up with the Fusion log. &lt;/P&gt;
&lt;P&gt;If the file was found, it was loaded from the last path probed in the log (or from the GAC, if there is no probed path shown).&lt;/P&gt;
&lt;P&gt;Note: Unless you are explicitly debugging the failure of a resource to load, you will likely want to ignore failures to find assemblies with the ".resources" extension with the culture set to something other than "neutral". Those are expected failures when the ResourceManager is probing for satellite assemblies.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Troubleshooting Fusion logging&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Keep in mind that there may not be any entries if there were no binds or if there were no binding failures (when logging failures only). (And don't forget to restart your application's process after changing logging settings.) If there are not as many entries as you expect, you may need to clear your download cache to make room for them. To do that, in Internet Explorer, choose “Tools“, “Internet Options“, “Delete Files”, “OK“, then re-run your app and finally click “Refresh“ again in fuslogvw.&lt;/P&gt;
&lt;P&gt;There is only one Fusion log saved per display name/codebase. So, if the same exact reference is requested twice in the same process, it will only show up once in fuslogvw. To see the duplicate logs, you'll need to stop execution (for example, with breakpoints in your debugger) and then get the log at that time.&lt;/P&gt;
&lt;P&gt;&lt;B&gt;For FileNotFoundException:&lt;/B&gt; &lt;BR&gt;At the bottom of the log will be the paths that Fusion tried probing for this assembly. If this was a load by path (as in Assembly.LoadFrom()), there will be just one path, and your assembly will need to be there to be found. Otherwise, your assembly will need to be on one of the probing paths listed or in the GAC if it's to be found. &lt;/P&gt;
&lt;P&gt;You may also get this exception if an unmanaged dependency or internal module of the assembly failed to load. Try running depends.exe on the file to verify that unmanaged dependencies can be loaded. Note that if you re using ASP.NET, the PATH environment variable it's using may differ from the one the command line uses. If all of them could be loaded, try ildasm.exe on the file, double-click on "MANIFEST" and look for ".file" entries. Each of those files will need to be in the same directory as the manifest-containing file.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;B&gt;For BadImageFormatException:&lt;/B&gt; &lt;BR&gt;Try running peverify.exe on the file. That will give a more specific description about why it s considered a bad image. Keep in mind that modules built against v2 can not be&amp;nbsp;loaded by&amp;nbsp;a pre-v2 CLR.&lt;/P&gt;
&lt;P&gt;&lt;B&gt;For SecurityException:&lt;/B&gt; &lt;BR&gt;You need Execute permission for loading any assembly. Also, if a codebase was used to load this file, you would need both FileIOPermission.Read and FileIOPermission.PathDiscovery or else WebPermission to the location (depending on whether this is a local file or a URL). Try caspol.exe to check your managed security settings. &lt;/P&gt;
&lt;P&gt;&lt;B&gt;For FileLoadException:&lt;/B&gt; &lt;/P&gt;
&lt;P&gt;&lt;I&gt;For an "Access is denied" message (for hresult E_ACCESSDENIED, 0x80070005):&lt;/I&gt; &lt;BR&gt;Run tlist -m on the file to see if another process has the file locked and without share-read access. If not, check the ACLs for the file and its dependencies (especially if you're using impersonation). &lt;/P&gt;
&lt;P&gt;&lt;I&gt;For a "The located assembly's manifest definition with name [yourAssembly] does not match the assembly reference" message (for hresult FUSION_E_REF_DEF_MISMATCH, 0x80131040):&lt;/I&gt; &lt;BR&gt;The Fusion log will say which part of the assembly reference failed to match what was found. It will be the assembly name, culture, public key (or token) or version (if the found assembly was strongly-named). &lt;/P&gt;
&lt;P&gt;&lt;EM&gt;For "Unverifiable image [yourAssembly] can not be run" or "Can not run executable [yourAssembly] because it contains relocations" messages (for hresult COR_E_FIXUPSINEXE, 0x80131019):&lt;/EM&gt; &lt;BR&gt;That image must be run as the process exe or else be compiled as a dll. This is because MC++ has made optimizations for you in your image, based on the assumption that it will be the process exe. If it's not the process exe, it won t be loaded at the expected location, so the assumed offsets will be incorrect. When the CLR sees such a file loaded as a non-process exe, it will reject the load. &lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=57120" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/suzcook/archive/tags/Loader+Debugging+Advice/default.aspx">Loader Debugging Advice</category></item></channel></rss>