Visual Studio Development Bookmark and Share   
 index > Visual Studio Guidance Automation Toolkit > How to pass parameters to T4 templates using GAX?
 

How to pass parameters to T4 templates using GAX?


I have the following template, created by the Clarius editor. When I right click and do a "Preview Transformation", the values set in the Properties window are properly substituted, but the actual .cs file (in the solution)is NOT generated properly. I also get a compile error:

Error1Running transformation: System.ArgumentNullException: Value cannot be null.
Parameter name: objectToConvert
at Microsoft.VisualStudio.TextTemplating.ToStringHelper.ToStringWithCulture(Object objectToConvert)
at Microsoft.VisualStudio.TextTemplating71AC0EACE4EB44F4B73CCBE83655D590.GeneratedTextTransformation.TransformText()C:\TT\TemplateTest\TemplateTest\test.tt11

Does anyone know to fix this?

<#@templatelanguage="C#"#>
<#@assemblyname="System.dll"#>
<#@propertyprocessor="PropertyProcessor"name="ClassName"type="System.String,mscorlib,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089"#>
<#@propertyprocessor="PropertyProcessor"name="TargetNamespace"type="System.String,mscorlib,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089"#>
<#@propertyprocessor="PropertyProcessor"name="HelloMessage"type="System.String,mscorlib,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089"#>
usingSystem;
usingSystem.Text;
namespace<#=TargetNamespace#>
{
class<#=ClassName#>
{
publicstaticstringhelloMessage="<#=HelloMessage#>";
staticvoidMain(string[]args)
{
Console.WriteLine(helloMessage);
Console.ReadLine();
}
}
}

I'm also attempting to run this code to substitute the parameters into the template, but that's not working either. I'm assuming that these issues are related and as soon as I can get the .cs file generating properly that I'll be able to do it through code too.

publicvoidProcessTemplate(stringSchemaName)
{
//Preparetemplateparameters
vararguments=newDictionary<string,PropertyData>();
arguments.Add("ClassName",newPropertyData("MyClass",typeof(string)));
arguments.Add("TargetNamespace",newPropertyData("MyNameSpace",typeof(string)));
arguments.Add("HelloMessage",newPropertyData("HelloMessage",typeof(string)));
//InitializeGAXtemplatehost
stringcurrentDirectory=Directory.GetCurrentDirectory();
TemplateHosthost=newTemplateHost(currentDirectory,arguments);
host.TemplateFile=Path.Combine(currentDirectory,"ServiceTemplate.tt");
//Transformtemplate
stringtemplate=File.ReadAllText(host.TemplateFile);
ITextTemplatingEngineengine=newEngine();
stringoutput=engine.ProcessTemplate(template,host);
//Saveoutput
stringoutputFile=Path.ChangeExtension(host.TemplateFile,".txt");
File.WriteAllText(outputFile,output);
}

Thanks for any help. I'm stumped.
ParaDiddl  Wednesday, July 02, 2008 8:26 PM
Hi,
I'm not sure if you're trying to use this sample, because it looks pretty same.
http://weblogs.asp.net/jescrich/archive/2007/05/03/testing-t4-templates-using-the-gax-host.aspx

if so, are you using the sample as is? which gax and vs version?

The sample that I made is based on a default guidance package so if you just try to create a default guidance package you'll get a exactly the same template (Program.cs.t4).

did you create your own recipe?, can you post how the recipe looks.?

jose.

Jose Escrich - weblogs.asp.net/jescrich
  • Proposed As Answer byJose Escrich Thursday, July 03, 2008 1:56 PM
  •  
Jose Escrich  Thursday, July 03, 2008 1:56 PM

I'm basing this sample off of that sample and the examples on Oleg Sych's site www.olegsych.com . I'm using the latest versions of GAX/GAT and VS 2008. I have the Clarius editor installed and I've tried changing the template host to Clarius, DSL Tools, and GAX. I don't have a recipe associated with this. What exactly is a recipe anyways?

ParaDiddl  Thursday, July 03, 2008 2:09 PM
I am experiencing the same problem with visual studio 2008. When I try to run my unit test, which uses the Engine and the TemplateHost classes, I am getting this error

Test method mymethod t threw exception: System.EntryPointNotFoundException: Entry point was not found..

Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost.get_TemplateFile()
Microsoft.VisualStudio.TextTemplating.Engine.ProcessTemplate(String content, ITextTemplatingEngineHost host)
mymethodt() in myfile.cs: line 78

I am using GAX Feb 2008 release and GAT for VS2008 .

Any suggestion?

Thanks in advance

BFC  Thursday, September 11, 2008 11:28 PM
Hi there,

the problem nowadays is that the GAX host still references Text Templateing 1.1. However Visual Studio uses newer Assmebly. As there are now dual references assembly resolver cannot load the stuff anymore resulting in the System.EntryPointNotFoundException.

To work around this problem you need a seperate AppDomain. The blogpost shown here has instructions where you can find the T4GAXRunner.tt which instanciates GAX host in a seperate domain and runs your T4 template from there. It also alows you to pass parameters in very nicely.

http://tangibleengineering.blogspot.com/2009/05/how-to-call-gax-based-t4-templates-that.html

If you just want to run GAX host from a program make sure you use the old version of the dll (there are two on your system)

Regards

Tim
Tim F. Fischer  Sunday, May 03, 2009 7:57 PM

You can use google to search for other answers

Custom Search

More Threads

• Exception thrown on Register SCSF
• Guidance automation extensions exception
• Programatically Executing a recipe while changing an input argument
• Where is the offline content file saved for SCSF application
• VSTS 2008 - installed GAX but GAT fails: C# Language is not installed
• Creating solution folders which do not appear on disk
• GAX/GAT breaking DSL
• SCSF May 2007. Adding Components to the toolbox
• Access Denied Error while installing custom Guidance package
• conditionally adding a binary reference to a project