T4 Assembly directive cannot find the file specified
Asked Answered
M

1

5

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?

Monition answered 22/1, 2015 at 4:0 Comment(0)
M
8

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.

  1. Open the References folder for my project (Visual Studio 2010, .NET 4.0).
  2. For each reference to an automation assembly; e.g., envdte, envdte80, vslangproj, vslangproj2, vslangproj80, etc...
  3. Select the reference and open the Properties window.
  4. Change the Embed Interop Types value to False.

Rebuild the original assembly and attempt to load it.

Monition answered 22/1, 2015 at 4:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.