T4MVC can only execute through the Visual Studio host


Admittedly, I have just started working with T4 so I am not up to expert level.

I do not get this error if I take a stock MVC3 app, and simply drop t4MC.tt and T4MVC.tt.settings.t4 into the root project folder.
However, if I modify my project by adding the following line;
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\TextTemplating\v10.0\Microsoft.TextTemplating.targets" />
I get the following error;
Error 24 Running transformation: System.Exception: T4MVC can only execute through the Visual Studio host
at Microsoft.VisualStudio.TextTemplating7EA676658956270405035C16AABD3C42.GeneratedTextTransformation.PrepareDataToRender(TextTransformation tt) in h:\work\MyT4MVCApp\src\MyT4MVCApp\T4MVC.tt:line 616
at Microsoft.VisualStudio.TextTemplating7EA676658956270405035C16AABD3C42.GeneratedTextTransformation.TransformText() in h:\work\MyT4MVCApp\src\MyT4MVCApp\T4MVC.tt:line 522
at Microsoft.VisualStudio.TextTemplating.TransformationRunner.RunTransformation(TemplateProcessingSession session, String source, ITextTemplatingEngineHost host, String& result). Line=0, Column=0 MyT4MVCApp

I have found some problems with adding the Microsoft.TextTemplating.target.
  1. I can no longer use macros in my TT file. i.e. <#@ include file="$(SolutionDir)..\sdk\tt\GeneratedByTool.ttinclude" #> no longer works.
  2. If I don't add Microsoft.TextTemplating.target, then my T4ParameterValues don't get into my host. i.e.
    <T4ParameterValues Include="RemoteMachine">
    <Value>TEST REMOTE MACHINE</Value>
    so the following returns an empty string if Microsoft.TextTemplating.target is not in my project.
    RemoteMachine:[<#=this.Host.ResolveParameterValue("-", "-", "RemoteMachine") #>]
needless to say it is getting frustrating...
Closed Apr 12, 2012 at 10:34 PM by davidebbo


davidebbo wrote Jan 30, 2012 at 3:06 PM

See http://stackoverflow.com/questions/5372067/using-t4mvc-with-build-script which sounds similar. Generally, because T4MVC relies heavily on the VS DTE, it would be very hard for it to run at build time.