{"id":2517,"date":"2022-08-30T15:25:31","date_gmt":"2022-08-30T15:25:31","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/28\/razorengine-referenced-assemblies-are-not-loaded-if-only-consumed-in-a-view-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:25:31","modified_gmt":"2022-08-30T15:25:31","slug":"razorengine-referenced-assemblies-are-not-loaded-if-only-consumed-in-a-view-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/razorengine-referenced-assemblies-are-not-loaded-if-only-consumed-in-a-view-collection-of-common-programming-errors\/","title":{"rendered":"RazorEngine &#8211; Referenced assemblies are not loaded if only consumed in a view-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m using RazorEngine to perform some templating in a service layer, outside of the context of MVC and I&#8217;m having problems with assemblies not being loaded into the AppDomain when the classes in those assemblies are used only in the views. Let me elaborate.<\/p>\n<p>I have some Razor templates that are embedded resources in a standalone assembly (let&#8217;s call it Assembly A).<\/p>\n<p>One of the templates (<code>MyTemplate<\/code>) consumes a utility class (<code>MyHelperClass<\/code>) which resides in another assembly (let&#8217;s call it Assembly B).<\/p>\n<p>Assembly A has a hard reference to Assembly B.<\/p>\n<p>When I try to parse the template that references <code>MyHelperClass<\/code> I get the familiar, &#8220;are you missing a using directive or an assembly namespace&#8221;. When I debug and look at the modules window in Visual Studio I can see that Assembly B is not loaded into the AppDomain. If I debug through the code and load the assembly via the intermediate window before the failing line of code, it works.<\/p>\n<p>So, it seems that merely being consumed in a view isn&#8217;t enough for the .NET framework to load an assembly into the AppDomain. I assume this may well be the case in a standard MVC project as well, although I haven&#8217;t tested this.<\/p>\n<p>Two questions really.<\/p>\n<ol>\n<li>Is a reference in a view not enough to cause the runtime to load an assembly?<\/li>\n<li>In this situation, what&#8217;s the best way to force this assembly to load without having to resort to <code>Assembly.Load<\/code> or referencing a dummy class.<\/li>\n<\/ol>\n<ol>\n<li>\n<p>Declare the namespace of <code>MyHelperClass<\/code> in web.config:<\/p>\n<pre><code>\n  \n\n\n  \n    \n  \n\n<\/code><\/pre>\n<\/li>\n<li>\n<p>In the end I decided that runtime compilation for these templates was just too fragile and the possibility of having this crop up in the future made me feel very uneasy. Instead I decided to change my project to use the RazorGenerator NuGet package and have the views precompiled to avoid any runtime surprises.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-01-28 10:02:53. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m using RazorEngine to perform some templating in a service layer, outside of the context of MVC and I&#8217;m having problems with assemblies not being loaded into the AppDomain when the classes in those assemblies are used only in the views. Let me elaborate. I have some Razor templates that are embedded resources in a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2517","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2517","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2517"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2517\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}