Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileNotFoundException (System.Runtime, Version=4.2.1.0) when reflecting in T4 template #2000

Closed
BasPaap opened this issue Oct 15, 2018 · 11 comments

Comments

Projects
None yet
7 participants
@BasPaap
Copy link

commented Oct 15, 2018

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:

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.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
    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?

@karelz karelz added the area-aspnet label Oct 15, 2018

@Eilon Eilon removed the area-aspnet label Oct 15, 2018

@Eilon

This comment has been minimized.

Copy link
Member

commented Oct 15, 2018

@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.

@karelz

This comment has been minimized.

Copy link
Member

commented Oct 15, 2018

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!

@karelz karelz closed this Oct 15, 2018

@richimori

This comment has been minimized.

Copy link

commented Oct 31, 2018

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

@BasPaap

This comment has been minimized.

Copy link
Author

commented Oct 31, 2018

@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

@keithbeller

This comment has been minimized.

Copy link

commented Nov 23, 2018

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

@FionaHolder

This comment has been minimized.

Copy link

commented Jan 14, 2019

Also having this issue

@karelz

This comment has been minimized.

Copy link
Member

commented Jan 14, 2019

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.

@lmcarreiro

This comment has been minimized.

Copy link

commented Jan 22, 2019

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.
@lmcarreiro

This comment has been minimized.

Copy link

commented Jan 22, 2019

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.

@lmcarreiro

This comment has been minimized.

Copy link

commented Jan 22, 2019

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>
@BasPaap

This comment has been minimized.

Copy link
Author

commented Mar 11, 2019

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.