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

Several FileNotFoundException using Newtonsoft.Json inside T4 template #2743

Closed
etrigoco opened this issue May 17, 2019 · 4 comments
Closed
Labels
needs-more-info Not enough information has been provided. Please share more detail as requested.

Comments

@etrigoco
Copy link

Issue

I'm having the same issue described here: FileNotFoundException (System.Runtime, Version=4.2.1.0) when reflecting in T4 template. The issue specifically happens when trying to use Newtonsoft.Json inside a T4:

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
<#@ assembly name="Newtonsoft.Json" #>
<#@ import namespace="Newtonsoft.Json" #>
<#@ import namespace="Newtonsoft.Json.Linq" #>
<#@ assembly name="$(SolutionDir)Generator.Templates\bin\Debug\netcoreapp3.0\Dotnet.Core.dll" #>
<#@ import namespace="Dotnet.Core.Dto" #>
<#
var templateFileName = Path.GetFileNameWithoutExtension(this.Host.TemplateFile);
var jsonPath = this.Host.ResolvePath($"{templateFileName}.json");
var json = File.ReadAllText(jsonPath);
var test = JsonConvert.DeserializeObject<Schema>(json);
#>

Following @lmcarreiro instructions (using machine.config instead of devenv.exe.config) I was able to get rid of the System.Runtime, Version=4.2.1.0 error, but after that I'm getting the equivalent error but this time for System.Diagnostics.Debug:

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

I can "fix" it again adding another binding redirect like this:

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

After that, the next error I get is for System.Text.Json:

Error Running transformation: System.IO.FileNotFoundException: Could not load file or assembly 'System.Text.Json, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies.

which I haven't been able to fix using the previous steps. I've also tried getting the System.Text.Json nuget package (4.6.0-preview6.19259.10), but it doesn't do much and I assume the template transformation engine is looking elsewhere.

Any idea of why this issue is even happening in the first place? As far as I know, the System.Text.Json assembly should be part of NETCore.Platforms with NET Core 3.0, shouldn't it?

I already wish I didn't have to tinker with the machine.config, but at this point I'd be happy just getting this to work.

General

I'm using VS 2019 with the latest update (16.0.4), and this is my environment as per dotnet --info:

SDK de .NET Core (reflejando cualquier global.json):
 Version:   3.0.100-preview5-011568
 Commit:    b487ff10aa

Entorno de tiempo de ejecución:
 OS Name:     Windows
 OS Version:  10.0.17134
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\3.0.100-preview5-011568\

Host (useful for support):
  Version: 3.0.0-preview5-27626-15
  Commit:  61f30f5a23

.NET Core SDKs installed:
  2.1.202 [C:\Program Files\dotnet\sdk]
  2.1.505 [C:\Program Files\dotnet\sdk]
  2.1.602 [C:\Program Files\dotnet\sdk]
  2.1.604 [C:\Program Files\dotnet\sdk]
  2.2.203 [C:\Program Files\dotnet\sdk]
  3.0.100-preview5-011568 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.0-preview5-19227-01 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.0-preview5-27626-15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.0.0-preview5-27626-15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
@karelz
Copy link
Member

karelz commented May 17, 2019

@joperezr can you please help here?
@erictrigo do you have a minimal repro you could share?

@joperezr
Copy link
Member

Can you either share a minimal repro or describe what is your app trying to do? Are you trying to use reflection to load or run some code in a specific assembly which targets .NET Standard? Are you running in .NET Framework?

@davidfowl
Copy link
Member

The t4 template is running in visual studio on .NET Framework and likely can't find the dependent assemblies being referenced by the template.

@joperezr joperezr removed their assignment Jun 26, 2019
@joperezr joperezr added the needs-more-info Not enough information has been provided. Please share more detail as requested. label Jun 26, 2019
@carlossanlop
Copy link
Member

I'm closing this issue due to lack of activity. @erictrigo feel free to re-open it if you have an update so we can assist you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-more-info Not enough information has been provided. Please share more detail as requested.
Projects
None yet
Development

No branches or pull requests

5 participants