Core: FileNotFoundException (System.Runtime, Version=4.2.1.0) when reflecting in T4 template

Created on 15 Oct 2018  路  18Comments  路  Source: dotnet/core

General

I have a .NET Core 2.1 ASP.NET MVC application in which I have a design time T4 template which I want to use to generate some code. Currently, it looks like this:

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="$(SolutionDir)\Bar\bin\Debug\netcoreapp2.1\Bar.dll" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="Bar" #>
<#@ output extension=".txt" #>
<#
    var myType = typeof(string);
    var myName = y.Name;
    Write(myName);    
#>

This works: the .txt file generated by this template contains "String".
However, now I have a class Foo in Foo.cs in the same folder as my template:

```c#
namespace Bar
{
public class Foo
{
}
}

Now I want to use Foo instead of string, so I just change the line 
`var myType = typeof(string);`
to
`var myType = typeof(Foo);`

and save the template. Now I get an error:

Running transformation: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.

With the following stack trace: 

File name: 'System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
at Microsoft.VisualStudio.TextTemplating9418B77BB4C607966DB4F31F6C9D57D97D9892B08324572567CEAC228ECCD2BE3839F9E2A0AE0ECA2D5DD0CCC161A70762C40A6D67A8DC505F3E88E86A36C7CE.GeneratedTextTransformation.TransformText()
at System.Dynamic.UpdateDelegates.UpdateAndExecute1T0,TRet
at CallSite.Target(Closure , CallSite , Object )
at Microsoft.VisualStudio.TextTemplating.TransformationRunner.PerformTransformation()
```
Why does it throw this error? Why does reflection work for the built-in string type, but somehow depend on .NET Framework 4.2.1 for my own .NET Core 2.1 type?

Most helpful comment

It works!!!

Just put this bindingRedirect inside the C:\Users\<user>\AppData\Local\Microsoft\VisualStudio\15.0_29f8d23a\devenv.exe.config inside <configuration> -> <runtime> -> <assemblyBinding> where are all the others bindingRedirect's

<dependentAssembly>
  <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
  <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="4.0.0.0"/>
</dependentAssembly>

All 18 comments

@karelz - this looks like an issue with running the VS Text Templating Engine (T4), not specific to ASP.NET Core.

It could be that reporting this issue through VS's built-in "Report a Problem" tool is the best avenue for this issue.

Thanks @Eilon, I incorrectly thought it was part of ASP.NET (I just saw the name T4 once before).
@BasPaap please report it to VS via "Report Problem" or via https://developercommunity.visualstudio.com/spaces/8/index.html, thanks!

@BasPaap Have you found any work around for this issue?

@richimori I haven't found a workaround, unfortunately. I reported the problem via the Visual Studio feedback tool where it has been Triaged, but no further updates. You can vote on it here if you like: https://developercommunity.visualstudio.com/content/problem/358905/filenotfoundexception-systemruntime-version4210-wh.html

Having the same issue.

Using T4 Templates in a Netcore 2.1 Unit Tests project. Getting the following error:

Severity Code Description Project File Line Suppression State
Error Running transformation: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
File name: 'System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
at Microsoft.VisualStudio.TextTemplating7287211A5B67A55BA89E7ADD09A1D5F1C756AB89B667A33B67732467A8A9894344C4A8EDFA37F134BFF90591C5A79A28A98334B7FAAD2E2CEC9F967513112B31.GeneratedTextTransformation.TransformText()
at Microsoft.VisualStudio.TextTemplating.TransformationRunner.PerformTransformation()

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog]. PROJECT NAME PATH TO FILE\file.tt 1

Also having this issue

Just a reminder that this is closed issue, not monitored or used for +1 votes. The main issue is tracked on VS Developer Community - see link above.

Maybe it's because we have a .net core assembly calling a .net framework assembly.

The assembly C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\TextTransform.exe are trying to load System.Runtime from my bin/Debug/netcoreapp2.2

Running transformation: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
File name: 'System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
   at Microsoft.VisualStudio.TextTemplatingCCD6A65ACD0C020DB5143939AD89CCAF6364E601AB0B0BD88DA2E9E732B57C5A9C1CFFAEEF1F05268508D25BBB9C886F16C987B8FFEB9F3DFC1C5D402ED92597.GeneratedTextTransformation.TransformText()
   at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
   at Microsoft.VisualStudio.TextTemplating.TransformationRunner.PerformTransformation()

=== Pre-bind state information ===
LOG: DisplayName = System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
 (Fully-specified)
LOG: Appbase = file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/
LOG: Initial PrivatePath = NULL
Calling assembly : MOPE.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\Users\u42m\AppData\Local\Microsoft\VisualStudio\15.0_29f8d23a\devenv.exe.config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/PublicAssemblies/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/PublicAssemblies/System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/PrivateAssemblies/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/PrivateAssemblies/System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/CommonExtensions/Microsoft/TemplateProviders/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/CommonExtensions/Microsoft/TemplateProviders/System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/CommonExtensions/Microsoft/TestWindow/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/CommonExtensions/Microsoft/TestWindow/System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/CommonExtensions/Platform/Debugger/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/CommonExtensions/Platform/Debugger/System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/CommonExtensions/Platform/DiagnosticsHub/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/CommonExtensions/Platform/DiagnosticsHub/System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/PrivateAssemblies/DataCollectors/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/PrivateAssemblies/DataCollectors/System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/PrivateAssemblies/DataCollectors/x86/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/PrivateAssemblies/DataCollectors/x86/System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/System.Runtime/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/PublicAssemblies/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/PublicAssemblies/System.Runtime/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/PrivateAssemblies/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/PrivateAssemblies/System.Runtime/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/CommonExtensions/Microsoft/TemplateProviders/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/CommonExtensions/Microsoft/TemplateProviders/System.Runtime/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/CommonExtensions/Microsoft/TestWindow/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/CommonExtensions/Microsoft/TestWindow/System.Runtime/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/CommonExtensions/Platform/Debugger/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/CommonExtensions/Platform/Debugger/System.Runtime/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/CommonExtensions/Platform/DiagnosticsHub/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/CommonExtensions/Platform/DiagnosticsHub/System.Runtime/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/PrivateAssemblies/DataCollectors/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/PrivateAssemblies/DataCollectors/System.Runtime/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/PrivateAssemblies/DataCollectors/x86/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/common7/ide/PrivateAssemblies/DataCollectors/x86/System.Runtime/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Users/u42m/oldD/p/MOPE/MOPE/MOPE.Core/bin/Debug/netcoreapp2.2/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Users/u42m/oldD/p/MOPE/MOPE/MOPE.Core/bin/Debug/netcoreapp2.2/System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Users/u42m/oldD/p/MOPE/MOPE/MOPE.Core/bin/Debug/netcoreapp2.2/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Users/u42m/oldD/p/MOPE/MOPE/MOPE.Core/bin/Debug/netcoreapp2.2/System.Runtime/System.Runtime.EXE.

Maybe some assemblyRedirect inside C:\Users\<user>\AppData\Local\Microsoft\VisualStudio\15.0_29f8d23a\devenv.exe.config could be a workaround, but I tried some things and can't get it working.

It works!!!

Just put this bindingRedirect inside the C:\Users\<user>\AppData\Local\Microsoft\VisualStudio\15.0_29f8d23a\devenv.exe.config inside <configuration> -> <runtime> -> <assemblyBinding> where are all the others bindingRedirect's

<dependentAssembly>
  <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
  <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="4.0.0.0"/>
</dependentAssembly>

Why, exactly, does this work? From the redirect, it looks like it's telling the runtime to use System.Runtime 4.0.0.0 instead of 4.2.1.0, but why can it find 4.0.0.0 and not 4.2.1.0?

This solution isn't working for me, but it's probably because I'm using .NET Core 3.0?
Does anyone have a solution that'll work for .NET Core 3.0?
(This .NET Core T4 bologna is killing me...)

This solution isn't working for me, but it's probably because I'm using .NET Core 3.0?
Does anyone have a solution that'll work for .NET Core 3.0?

As of VS 16.3.9, you have to edit the file in this folder (16.0_6b4c9109):

C:\Users\<user>\AppData\Local\Microsoft\VisualStudio\16.0_6b4c9109\devenv.exe.config

and then restart VS. This did the trick for me 馃ぉ I'm also on .NET Core 3.0.

For me the problem was that T4 model generator uses Framework 4.8.4075.0.
To see it you can build a SystemInfo.tt model like this:

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".txt" #>
<#  
    string output;

    output = $"System.Environment.Version: {System.Environment.Version}";
#>
<#= output  #>    
<#  
    output = $"System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription: {System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription}";
#>
<#= output  #>   

I was trying to add a reference to a .Net Core library and I got the error.
Converting the .Net Core library to a .Net Standard library and referencing the .Net Standard library into the T4 model solved the issue.

I'm using .NET Core 3.1 and had this same issue. The binding redirect worked at first, but then the assembly I was referencing added a reference to another .NET Core assembly and it stopped working. I decided to develop an alternative T4 tool that uses a hybrid of Visual Studio's .NET Framework T4 capabilities for preprocessing and a .NET Core executable to execute the template code. I'm not sure how well it works with 3.0, but it works well for my 3.1 project and hopefully it can help some others too. It's available on GitHub.

Hi @filippobottega, can you please provide your changed code. I am running into the same issue, and cannot figure out the fix. Thank you very much.

Hi @filippobottega, can you please provide your changed code. I am running into the same issue, and cannot figure out the fix. Thank you very much.

Hi @pvchandu , I don't have any code, I have only changed the project type of the library that I need to reference in T4 model. If you have the source code of the library, you have to create a new project in Visual Studio using Standard Library and you have to copy your code into this new project.

image

Than you can reference this new project into your T4 template.

I had the same error. However, my projects are setup as dual build (i.e. both net471 and .netcore20). This is a common library, so referencing a project is problematic in two ways: 1. it is a common project used across multiple applications 2. if you create a branch to make changes, you now have to update the projects that reference this project in a completely different location just test.

Resolution: Made sure that my .net471 references referenced the .net471 common library and the .netcore20, referenced the library built as .netcore20. I had them mixed.

Was this page helpful?
0 / 5 - 0 ratings