No matter what method is used to specify a file in an assembly directive the T4 engine cannot find the file specified.
<#@ assembly name="$(SolutionDir)packages\TestPackage\lib\net45\Test.dll"#>
or <#@ assembly name="C:\Test.dll"#>
or any other method result in the same not found issue. The template engine seems to be able to read the file and will display its version information, even though it cannot find it.
Errors were generated when initializing the transformation object. The transformation will not be run. The following Exception was thrown:
System.IO.FileNotFoundException: Could not load file or assembly ‘Test, Version=1.0.1.0, Culture=neutral, PublicKeyToken=null’ or one of its dependencies. The system cannot find the file specified.
indicating a missing dependency. There are no dependencies for the assembly.
Why can the T4 engine not find my assembly?
When the assembly was packaged there were external references to EnvDTE or other COM interop types. When the EnvDTE (or other COM) interop is accessed in the T4 template it attempts to resolve the reference of EnvDTE, and it will resolve to the assembly attempting to be loaded. This is where the file not found exception comes from, a circular reference. It's caused by embedding interop type references in the assembly (on by default for performance reasons).
Dave Sexton found this issue 5 years ago:
More specifically, it's typeof(DTE) that is causing Visual Studio to try to load my assembly. My assembly is a .NET 4.0 assembly, and by default the reference to the automation assembly, envdte, was added with the NoPIA feature enabled. This causes the compiler to embed the interop types of envdte into my assembly. Therefore, typeof(DTE) is resolving to the DTE type in my assembly, which causes Visual Studio to require my assembly to be loaded to resolve the DTE type!
In order to resolve the issue you must disable embedding of interop types for the referenced COM assemblies.
- Open the References folder for my project (Visual Studio 2010, .NET 4.0).
- For each reference to an automation assembly; e.g., envdte, envdte80, vslangproj, vslangproj2, vslangproj80, etc...
- Select the reference and open the Properties window.
- Change the Embed Interop Types value to False.
Rebuild the original assembly and attempt to load it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With