I've got a T4 template which references another project and does some reflection on its output assembly to generate some code.
This works fine using Visual Studio 2010 SP1 at design-time, as the assembly is shadow copied before being used.
However when I try to use the MSBuild tasks from the Visual Studio Modeling SDK to do build-time integration, the referenced assembly is locked the first time this happens, and I cannot rebuild the referenced project afterwards.
I'm assuming that this is because the transformation engine is a totally different host under MSBuild, and doesn't include the shadow-copying fix that that Visual Studio host does.
Can anyone confirm if this is a known issue and if there is any workaround?
I realize that this is an older thread, but I wanted to share my experience in hopes that it helps others.
In VS 2012, we now have shadow copy support in the designer. However, when transforming T4 templates at build time (using MSBuild or building within Visual Studio), we still run into the assembly locking issue described above.
To overcome this, we load assemblies into our T4 AppDomain as byte arrays. This gives us the ability to reflect the assembly without locking it.
<## template debug="false" hostspecific="true" language="C#" #>
<## assembly name="System.Core" #>
<## assembly name="System.Linq" #>
<## assembly name="System.Runtime" #>
<## import namespace="System.Reflection" #>
<## import namespace="System.IO" #>
<## import namespace="System.Linq" #>
<## import namespace="System.Text" #>
<## import namespace="System.Collections.Generic" #>
<## output extension=".txt" #>
<#
var assemblyPath = this.Host.ResolvePath(#"..\Path\To.dll");
var assembly = LoadAssembly(assemblyPath);
foreach (Type t in assembly.GetTypes())
{
#>
<#= t.FullName #>
<#
}
#>
<#+
public Assembly LoadAssembly(string path)
{
return Assembly.Load(File.ReadAllBytes(path));
}
#>
In order to improve performance, T4 engine reuses templating AppDomain
for multiple template transformation, which causes all referenced
assembly files to be locked. This presents a problem if you are using
your own assembly during code generation, because the assembly file
cannot be recompiled until the file is unlocked by T4. You can force
T4 to unload the file by closing and reopening the Visual Studio
solution or by restarting Visual Studio itself. Alternatively,
consider using VolatileAssembly custom directive available in T4
Toolbox, which creates a shadow copy of the assembly before loading
it.
http://www.olegsych.com/2008/02/t4-assembly-directive/
Related
I'm trying to use types from my own project in a T4 template, and so I want to use the VolatileAssembly directive so I don't have to keep restarting Visual Studio:
<## VolatileAssembly processor="T4Toolbox.VolatileAssemblyProcessor" name="C:\Josh\Archimetrics\Archimetrics.Consulting\Suntex\SuntexFirstInMathTools\bin\Debug\SuntexFirstInMathTools.exe" #>
But I keep getting the following error:
A processor named 'T4Toolbox.VolatileAssemblyProcessor' could not be found for the directive named 'VolatileAssembly'. The transformation will not be run.
The following Exception was thrown:
System.IO.FileNotFoundException: Failed to resolve type for directive processor
T4Toolbox.VolatileAssemblyProcessor.
at Microsoft.VisualStudio.TextTemplating.VSHost.TextTemplatingService.ResolveDirectiveProcessor(String processorName)
at Microsoft.VisualStudio.TextTemplating.Engine.ProcessCustomDirectives(ITextTemplatingEngineHost host, TemplateProcessingSession session, IEnumerable`1 directivesToBeProcessed)
I've installed the T4 toolbox and restarted, but for some reason it will not recognize the VolatileAssemblyProcessor.
How can I fix this?
They are also harder to use than templates because code they generate
is invisible to you by default. In addition, the VolatileAssembly is
no longer necessary because in Visual Studio 2012 the assembly
directive shadow-copies the assembly files before loading them
- the developers page -
So I think you do not need this directive anymore. Only use <## assembly name="YourPathOrName" #> will fix your problem.
I have the following code in a tester class in my main assembly, PocoGenerator. This assembly is supposed to use a T4 template to generate POCO's based on L2S entities in a referenced assembly (a project reference), DataObjects.
var assemblyName = "DataObjects";
var dataObjects = AppDomain.CurrentDomain.Load(new AssemblyName(assemblyName));
Try as I may, I cannot get T4 to find the DataObjects assembly. I have tried various forms of assembly directives, like:
<## assembly name="DataObjects" #>
<## assembly name="DataObjects, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" #>
to no avail. The code above works in the tester class, but not in the template. What am I doing wrong?
ADDED:
I have resolved this issue by using the absolute path to the assembly in bot places I reference it, the directive as well as the class feature block, i.e.
<## assembly name="C:\Development\PocoGenerator\DataObjects\bin\Debug\DataObjects.dll" #>
and
var sourceAssembly = Assembly.LoadFile(#"C:\Development\PocoGenerator\DataObjects\bin\Debug\DataObjects.dll");
But I really don't like this, as I would like to use this template in various projects, and I just plain hate duplication, especially of magic strings.
<## assembly name="$(ProjectDir)bin\Debug\ProofOfConcept.dll" #>
Happy Coding!
To reference assembly in T4 template in VS2010 you have some options:
GAC your assemblies and use Namespace Reference or Fully Qualified Type Name
Use a hard-coded Fully Qualified UNC path
Copy assembly to Visual Studio "Public Assemblies Folder" and use Namespace Reference or Fully Qualified Type Name.
Use or Define a Windows Environment Variable to build a Fully Qualified UNC path.
Use a Visual Studio Macro to build a Fully Qualified UNC path.
I would suggest that you put a referenced assembly in your Public Assemblies Folder, another, maybe even better solution would be to hard code the path of your referenced assemblies.
Very nice post on this topic: T4 Template error - Assembly Directive cannot locate referenced assembly in Visual Studio 2010 project.
Basically MS decided to the the braking change, that the project referenced assemblies are not referenced by T4 engine, too.
T4's assembly set is completely separated from the containing project's assembly set to avoid picking up the wrong assemblies when a project targets previous framework versions. Project assemblies are no longer used to resolve template assembly directives.
More on that: What's new in T4 in Visual Studio 2010
I had a similar problem when I tried to include Less Css for .NET in my Web project.
I've ended up with copying the assembly in the root folder of my project and including it as a reference in the project itself. Then, I've added the following lines in the .tt file:
<## assembly name="dotless.Core.dll" #>
<## import namespace="dotless.Core" #>
<## import namespace="dotless.Core.configuration" #>
I'm sure that something similar should work with your assembly as well...
I've found there are a number of cases in creating and using the gax toolkit and packages where the build is perfectly happy with the way references are structured but the runtime gets all bothered because it can't find what it's looking for - this usually occurs when the main assembly references an assembly that uses gax elements and then that assembly in turn references another assembly that the main does not.
try directly including the assembly in question in your main assembly - and consider that you may need to write post build instructions to move it to an 'expected' location - while a nusiance, it should beat having to hardwire the path.
YMMV
It's been a while since I last used T4 and this is probably a silly question...
Is it possible to reference an arbitrary assembly from a template?
Example:
I have a class that I'd like to use in Project X
Project X.Test references X and contains the .tt file
I assume the following should work
<## assembly name="X" #>
But I get the following error on save:
Compiling transformation: Metadata
file 'X' could not be found
What am I doing wrong?
(In case anyone's interested: I'm trying to automatically generate a particular type of tests based on some metadata that I get from X)
Update: it looks like VS2010 has broken the assembly resolution behavior that I was expecting. From Link:
T4's assembly set is completely
separated from the containing
project's assembly set to avoid
picking up the wrong assemblies when a
project targets previous framework
versions. Project assemblies are no
longer used to resolve template
assembly directives.
Are there any workarounds, besides using absolute paths?
You can use VS macro variables such as $(SolutionDir) in your reference as of VS2010
e.g.
<## assembly name="$(SolutionDir)\Project1\bin\debug\Foo.dll" #>
You can also check here on SO: Can't reference an assembly in a T4 template
#GarethJ gives a good answer but for all the methods of referencing an assembly from a T4 template try this: T4 Template error - Assembly Directive cannot locate referenced assembly in Visual Studio 2010 project.
And if you like the VS Macro solution then you can find 'em all here: Macros for Build Commands and Properties
I'm new to Visual Studio's T4 template engine, and I'm wondering how I can use COM objects to build templates. Specifically, SQL-DMO (to generate classes from stored procedures on a SQL 2000 server, SNO is not an option).
Have any of you guys done this? Any tips?
<## import namespace="SQLDMO" #>
is not working.
Thanks!
You can use SMO 2008 with SQL Server 2000 (download here).
In order to use a COM object, you need to reference its primary interop assembly with use <## assembly #> directive first. If the assembly is not available, you can generate it with tlbimp.
I am using T4 to generate some screens and middle-tier code for a project, and would like to use Linq to simplify some of my template code. However, when I try to use Linq, the template reports a syntax error.
By default in Visual Studio 2008 (and as used in most online examples) the template is compiled with the 2.0 Framework, which does not include Linq. (See MSDN forum thread)
To solve the problem, three steps are needed:
In your template's language attribute, specify "C#v3.5" or "VBv3.5" - this step is not required for VS2010, where .Net 4.0 is always used.
Add an assembly directive for System.Core.dll
Import the System.Linq namespace
Your template will now look something like this:
<## template language="C#v3.5" #>
<## assembly name="System.Core" #>
<## import namespace="System.Linq" #>
You can now use Linq and other new language features in your template.