
{"id":1198,"date":"2013-01-18T17:49:06","date_gmt":"2013-01-18T22:49:06","guid":{"rendered":"http:\/\/www.ikriv.com\/blog\/?p=1198"},"modified":"2013-01-18T17:49:06","modified_gmt":"2013-01-18T22:49:06","slug":"mef-though-shalt-not-loadfrom","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=1198","title":{"rendered":"MEF: Though Shalt Not LoadFrom"},"content":{"rendered":"<p>I was preparing for a presentation on MEF, and I <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd460648.aspx\">stumbled<\/a> upon this sentence:<\/p>\n<p><code>catalog.Catalogs.Add(new DirectoryCatalog(\"C:\\\\SimpleCalculator\\\\SimpleCalculator\\\\Extensions\"));<\/code><\/p>\n<p>&#8220;<i>This absolute path is for debugging purposes only. In a production application, you would use a relative path.<\/i>&#8221;<\/p>\n<p>The article does not explain why it is so, but I sensed it has to do with <a href=\"http:\/\/blogs.msdn.com\/b\/suzcook\/archive\/2003\/05\/29\/57143.aspx\">LoadFrom context<\/a>. Basically, when you load an assembly via absolute path outside of your application directory, it gets placed in a special context that is a source of all kinds of trouble.<\/p>\n<p>For a moment I hoped that MEF invented some kind of cure for this disease. In <code>DirectoryCatalog<\/code> they create an <code>AssemblyCatalog<\/code> for each file found in the directory, and <code>AssemblyCatalog<\/code> does this:<\/p>\n<p><code><\/p>\n<pre>var assemblyName = AssemblyName.GetAssemblyName(filePath);\nAssembly.Load(assemblyName);<\/pre>\n<p><\/code><\/p>\n<p>Voila? Jackpot? An override of <code>Assembly.Load()<\/code> that takes an absolute path, albeit in a cloaked form!<\/p>\n<p>Not so fast, folks. Although this is called <code>Assembly.Load()<\/code>, it will still use <code>LoadForm<\/code> context if the path is outside of the application directory. This can be confirmed by writing a small example and enabling <code>LoadFromContext<\/code> debug assistant in Visual Studio (Debug&rarr;xceptions&rarr;Managed Debuggin Assistants).<\/p>\n<p>The bottom line: MEF&#8217;s <code>DirectoryCatalog<\/code>, as well as <code>Assembly.Load(AssemblyName)<\/code> will still use <code>LoadFrom<\/code> context if the path is outside of the application base. This is exactly why one should use relative paths for <code>DirectoryCatalog<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was preparing for a presentation on MEF, and I stumbled upon this sentence: catalog.Catalogs.Add(new DirectoryCatalog(&#8220;C:\\\\SimpleCalculator\\\\SimpleCalculator\\\\Extensions&#8221;)); &#8220;This absolute path is for debugging purposes only. In a production application, you would <a href=\"https:\/\/ikriv.com\/blog\/?p=1198\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[3,4,5],"tags":[],"class_list":["entry","author-ikriv","post-1198","post","type-post","status-publish","format-standard","category-dotnet","category-hack","category-dev"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1198","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1198"}],"version-history":[{"count":0,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1198\/revisions"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}