title: Unable to load assembly in VS, but works in MSBuild
body: <p>I'm trying to load an assembly in a T4 template. While it works perfectly fine when executing the transforms from MSBuild, the T4 engine reports that it cannot find the assembly when running from Visual Studio (using the <code>Build -&gt; Transform All T4 templates</code> menu action or when using transform on save).</p>
  
  <p>I've set it up as recommended <a href="http://msdn.microsoft.com/en-us/library/gg586946.aspx" rel="nofollow">in the documentation</a>:</p>
  
  <pre><code>  &lt;PropertyGroup&gt;
      &lt;BuildSystemLibrary&gt;$(SolutionDir)src\Csw.BuildSystem\bin\Release\Csw.BuildSystem.dll&lt;/BuildSystemLibrary&gt;
    &lt;/PropertyGroup&gt;
    &lt;ItemGroup&gt;
      &lt;T4ParameterValues Include="BuildSystemLibrary"&gt;
        &lt;Value&gt;$(BuildSystemLibrary)&lt;/Value&gt;
        &lt;Visible&gt;False&lt;/Visible&gt;
      &lt;/T4ParameterValues&gt;
    &lt;/ItemGroup&gt;
  </code></pre>
  
  <p><em>Well, with the exception of using <code>SolutionDir</code> instead of <code>MSBuildProjectDirectory</code>, the latter being unavailable in VS (I don't know if the documentation is flawed, or if things changed or even if my environment somehow is different). Anyway, <code>SolutionDir</code> is available for MSBuild also, and as I mentioned already builds go smoothly.</em></p>
  
  <p>The template is equally uninteresting:</p>
  
  <pre><code>&lt;#@ template debug="false" hostspecific="true" language="C#" #&gt;
  &lt;#@ output extension=".html" #&gt;
  &lt;#@ assembly name="$(BuildSystemLibrary)" #&gt;
  &lt;#@ import namespace="Csw.BuildSystem" #&gt;
  ...
  </code></pre>
  
  <p>The error reported in visual Studio's error list is:</p>
  
  <pre><code>Running transformation: System.IO.FileNotFoundException: Could not load file or assembly 'Csw.BuildSystem, Version=1.0.0.3, Culture=neutral, PublicKeyToken=50a5e7eafae5071b' or one of its dependencies. The system cannot find the file specified.
  File name: 'Csw.BuildSystem, Version=1.0.0.3, Culture=neutral, PublicKeyToken=50a5e7eafae5071b'
  </code></pre>
  
  <p>Now, while it does sound like <code>SolutionDir</code> might not be expanding to the correct path in VS, I know for sure that this is not the case. The file is definitely found on the filesystem, as its version number, culture and public key tokens are extracted. This is not a hypothetical cache glitch either, the error will show a new version number if I try to bump it.</p>
  
  <p>I then used the <a href="http://msdn.microsoft.com/en-us/library/e74a18c4.aspx" rel="nofollow">Assembly Binding Log Viewer</a>, which reports the following:</p>
  
  <pre><code>*** Assembly Binder Log Entry  (3/4/2014 @ 10:33:58 AM) ***
  
  The operation failed.
  Bind result: hr = 0x80070002. The system cannot find the file specified.
  
  Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
  Running under executable  C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe
  --- A detailed error log follows. 
  
  === Pre-bind state information ===
  LOG: DisplayName = Csw.BuildSystem, Version=1.0.0.3, Culture=neutral, PublicKeyToken=50a5e7eafae5071b
   (Fully-specified)
  LOG: Appbase = file:///C:/Program Files (x86)/Microsoft Visual Studio 12.0/Common7/IDE/
  LOG: Initial PrivatePath = NULL
  LOG: Dynamic Base = NULL
  LOG: Cache Base = NULL
  LOG: AppName = devenv.exe
  Calling assembly : (Unknown).
  ===
  LOG: This bind starts in default load context.
  LOG: Using application configuration file: C:\Users\tne\AppData\Local\Microsoft\VisualStudio\12.0\devenv.exe.config
  LOG: Using host configuration file: 
  LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
  LOG: Post-policy reference: Csw.BuildSystem, Version=1.0.0.3, Culture=neutral, PublicKeyToken=50a5e7eafae5071b
  LOG: GAC Lookup was unsuccessful.
  LOG: Attempting download of new URL file...
  ...
  </code></pre>
  
  <p>It then attempts to find it in many local directories.</p>
  
  <p>So, it would seem that it finds the assembly, extracts its strong name, then for some reason looks it up on the GAC instead of just using it.</p>
  
  <p>I've installed the assembly in the GAC using <code>gacutil</code> to confirm this; the T4 transformation subsequently works fine from Visual Studio. Obviously, I'm not exactly keen to require the installation of the assembly in the GAC of all dev machines. Plus, I'd like to understand what's going on.</p>
  
  <p>I've had a look at the VS conf (devenv.exe.config) as well as the machine conf (machine.config) files in search of some "Policy" rules that would forbid the use of arbitrary DLLs on the filesystem from devenv (or that would require assemblies to be in the GAC), but I wouldn't know where to begin.</p>
  
  <p>I also tried to compile the assembly to target both MSIL and x86 (since VS is 32bit), which doesn't change anything.</p>
  
  <p>Because it's supposed to be such a trivial thing to do, I suspect I've done a stupid mistake somewhere (which is why I detailed my process so extensively, sorry for the verbosity). Can anybody catch it?</p>