In VS2017 Community, I cannot debug T4 Templates, which works in 2015.
I have a very basic template, such as this...
<## template debug="true" hostspecific="false" language="C#" #>
<## output extension=".txt" #>
<#
var a = "Hello";
var b = "World";
#>
<#=($"{a} {b}!")#>
Run Custom Tool and Transform All T4 Templates both options work, and text file contains expected output
Hello World!
If I put breakpoint somewhere and use Debug T4 Template from the context menu of .tt, it throws this error
Unable to start transformation run creation process.
However it works fine in VS 2015, and I'm able to debug there.
What I could be missing? how to debug T4 Templates in VS 2017? Note that I don't have any Tool/ Extension installed in VS2015 to debug T4
I have had the same issue, I don't know why it doesn't work this way but I have a work around.
Set debug to true, and add the diagnostic namespace
<## template language="C#" debug="true" #>
<## import namespace="System.Diagnostics" #>
In your T4 template write
Debugger.Launch();
Then run your template (easiest way it just to save it) and it will ask if you would like to debug in a new instance of visual studio.
The easiest solution is to just add these two lines to the top of your T4 template.
<## template debug="true" hostspecific="false" language="C#" #>
<# System.Diagnostics.Debugger.Launch(); #>
Then just run the template by saving the file and visual studio will prompt you to debug in a new instance.
If you use Host in your template and you get the error The name 'Host' does not exist in the current context then set `hostspecific="true"'.
Related
Using VS2022 17.2.0 Preview 2.0 to generate data layer using T4 templates.
Part of the T4 uses VS interop / DTE to access projects in solution.
The following T4 is a test:
<## template debug="false" hostspecific="true" language="C#" #>
<## assembly name="Microsoft.VisualStudio.Shell.Interop"#>
<## import namespace="Microsoft.VisualStudio.Shell"#>
<## import namespace="Microsoft.VisualStudio.Shell.Interop"#>
<## import namespace="EnvDTE" #>
<## import namespace="EnvDTE80" #>
<## output extension=".txt" #>
<#
var hostServiceProvider = Host as IServiceProvider;
var dte = hostServiceProvider.GetService(typeof(DTE)) as DTE2;
foreach (Project project in dte.Solution)
{
#><#= project.Name #>
<#
}
#>
This produces following exception:
Error Running transformation: System.Runtime.Serialization.SerializationException: Type 'Microsoft.VisualStudio.CommonIDE.Solutions.CMiscProject' in Assembly 'Microsoft.VisualStudio.CommonIDE, Version=17.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.
Issue did not exist in Preview 1.0 or in VS2019.
I have had a look around and pulled in nuget package for Microsoft.VisualStudio.Interop, version 17.1.32210.191, but problem persists when accessing anything through the EnvDTE.DTE(2).
I know I'm jumping the gun on this as it is a preview version, but has anyone had this issue and solved it? Is there a different approach needed to access projects in the solution from the T4 template?
The error does not occur when debugging the T4 template.
I had a bit of a play (and a lot of a Google) and found the following solved it for me under VS 2022:
Ensure you have the following assemblies and namespaces
<## assembly name="Microsoft.VisualStudio.Interop" #>
<## import namespace="EnvDTE" #>
<## import namespace="EnvDTE80" #>
<## import namespace="Microsoft.VisualStudio.TextTemplating" #>
and then swap out the IServiceProvider's GetService for GetCOMService
//var dte = hostServiceProvider.GetService(typeof(DTE)) as DTE2;
var dte = hostServiceProvider.GetCOMService(typeof(DTE)) as DTE2;
Mostly from answer here: https://stackoverflow.com/a/53346767/2797450
How can I use Linq in a T4 template
This is my software environment information
vs2012
.net version 4.0
This is t4 template:
<## templatedebug="true" hostSpecific="true" #>
<## output extension=".cs" #>
<## Assembly Name="System.Core.dll" #>
<## import namespace="System" #>
<## import namespace="System.Linq" #>
When I call engine.ProcessTemplate(inputTemplate, host), it returns the contents of
ErrorGeneratingOutput. Why is that?
Old question I know, but I've just found the same thing.
When you reference System.Core, don't include .dll:
<## assembly name="System.Core" #>
You need to look at the error in the errors window of visual studio to see more information.
Also you can right click the .tt file and say debug template.
I have a T4 template to generate C# classes for the entities backing my .edmx model. The template starts with this header:
<## template language="C#" debug="false" hostspecific="true"#>
<## include file="EF.Utility.CS.ttinclude"#>
<## output extension=".cs"#>
Attempting to build the project results in these errors
error CS0234: The type or namespace name 'Design' does not exist in the namespace 'System.Data.Entity'...
error CS0246: The type or namespace name 'EnvDTE' could not be found (are you missing a using directive...
error CS0234: The type or namespace name 'VisualStudio' does not exist in the namespace 'Microsoft'...
How can I fix this?
It turns out than, based on a VS Extensibility thread, the reason for the problem was
the Clarius Visual T4 extension. It reset this node in the .csproj
file to
<Compile Include="SomeModel.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>SomeModel.cs</LastGenOutput>
</Compile>
from
<None Include="SomeModel.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>SomeModel.cs</LastGenOutput>
</None>
The solution is to manually change the node to None in the .csproj file. Changing it
back via the Visual Studio properties editor for the .tt file does not work.
Finally, disabling the extension prevents this from happening again.
Just simply add needed assemblies at the beginning of your .tt file; like this:
<## template language="C#" debug="false" hostspecific="true"#>
<## assembly name="System.Data.Entity" #>
<## assembly name="System.Data.Entity.Design" #>
...
I am taking a look into T4 and scaffolding and I decided to give it a try. So I got the MvcScaffolding package on NuGet in order to customize a "Create" template in a test project.
After I have done very small changes (added css styles and translated the button texts) I decided to test my template by generating a View with my own "Create" scaffolding template.
Then I got the error bellow. I have checked the references on my project and everything seems there. Any ideas on how to fix this?
EDIT: I have just realized that some of my VS2010 AddOns were generating this error. Once disabled, it worked but my template wasn't used...
I was facing the same error and was able to solve it by directly pointing to the referenced libaries in my View template:
<## assembly name="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.ComponentModel.DataAnnotations.dll" #>
<## assembly name="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Core.dll" #>
<## assembly name="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Data.Entity.dll" #>
<## assembly name="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Data.Linq.dll" #>
The source of this issue was an extension installed -> tangible T4 Editor. After deinstalling my T4 templates started to work without complete paths.
I'm trying to run a T4 template that opens a XML file and uses it contents to generate a code artifact. However, I'm getting the an error message when I try to run a T4 template similar to the one below
<## template debug="false" hostspecific="false" language="C#" #>
<## assembly name="System.Xml.dll" #>
<## assembly name="System.Xml.Linq.dll" #>
<## import namespace="System.IO" #>
<## import namespace="System.Xml" #>
<## import namespace="System.Xml.Linq" #>
<## output extension=".cs" #>
namespace ConsoleApplication1
{
<# XElement fragment = XElement.Load("data.xml"); #>
...
Visual Studio 2010 error list is showing the following message
Running transformation: System.IO.FileNotFoundException: Could not find file 'C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\data.xml'.
It is trying to open the file on the path where the TextTemplateFileGenerator custom tool runs. I'd like it to open the file relative to my project path, because other developers on my team use different folder structures. Does anyone know if it is something possible to accomplish?
Change hostspecific option in template directive to "true" and call Host.ResolvePath("data.xml").
I had a similar problem but Host.ResolvePath didn't work for me because my relative path contained "..\.." in it. I worked around it by doing this:
string ttpath = this.Host.TemplateFile;
string resolvedPath = Path.GetFullPath(Path.GetDirectoryName(ttpath) + #"..\..\<Path To File>");