Using T4 with .NET Core 2.0 and referencing a package from a template

2

I tried using T4 in a .NET Core 2.0 app project but it seems that T4 doesn't recognize included namespaces. For example, after installing Microsoft.AspNetCore.All package and having the following piece of code:

<#@ import namespace="Microsoft.Extensions.Configuration" #>

<#+ 

public class ConfigurationHelper
{

    public string ReadConfig(){
        
        var builder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("Configuration\\CodeGenerationConfig.json");

            IConfigurationRoot  Configuration = builder.Build();
            
            return Configuration["ConnectionString:Server"];
        }
}

#>

I got the error:

The type or namespace name 'IConfigurationRoot' could not be found (are you missing a using directive or an assembly reference?)

Even I tried using assembly directive:

<#@ assembly name="C:\Program Files\dotnet\store\x64\netcoreapp2.0\microsoft.extensions.configuration\2.0.0\lib\netstandard2.0\Microsoft.Extensions.Configuration.dll"

>

But the following error occurred:

System.IO.FileLoadException: Could not load file or assembly 'file:///C:\Program Files\dotnet\store\x64\netcoreapp2.0\microsoft.extensions.configuration\2.0.0\lib\netstandard2.0\Microsoft.Extensions.Configuration.dll' or one of its dependencies. Strong name signature could not be verified. The assembly may have been tampered with, or it was delay signed but not fully signed with the correct private key. (Exception from HRESULT: 0x80131045)

So the question is how can I reference a .NET Core package from within a T4 template?

P.S: I tried Scripty, but it doesn't support .NET Core yet.

.net-core
t4
asked on Stack Overflow Oct 16, 2017 by Behrouz • edited Jun 20, 2020 by Community

1 Answer

1

Update:

The Visual Studio 2017 and Xamarin Studio now supports to process *.tt files in desing time

but https://github.com/ZeekoZhu/TextTemplatingCore may still be useful if you want to process T4 templates in a dotnet core(netstandard2.0) project outside IDE (eg. in Linux or macOS with Visual Studio Code)

answered on Stack Overflow Apr 8, 2018 by Seebo Smile • edited Apr 8, 2018 by Stephen Rauch

User contributions licensed under CC BY-SA 3.0