T4 Templating with third party builds

I need to create a JSON schema in a T4 template and find the new Schema class Newtonsoft more than suitable for this purpose (within a console application, tested), however I cannot get it to play ball with the others as the Newtonsoft instance always returns null.

T4 announcement:

<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="Newtonsoft.Json.dll" #>
<#@ assembly name="Newtonsoft.Json.Schema.dll" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>

      

Assembly references point to DLL files and I have a folder lookup set in the project settings for the project, screenshot below:

enter image description here

Attempting to do something like below fails because Newtonsoft cannot be found:

var schema = Newtonsoft.Json.Schema.JSchema.Parse (jsoncontent);

Error: The metadata file 'Newtonsoft.Json.Schema.dll' was not found .

+3


source to share


2 answers


T4 templates do not use the reference path defined in the project. T4 supports several variables inside Visual Studio:

<#@ assembly name="$(SolutionDir)\MyProject\bin\Debug\SomeLibrary.Dll" #>

      



There is fooobar.com/questions/173050 / ... about this.

If you reference the .dll and copy it to the output directory, you can use $ (TargetDir) in the path, so you don't need to include the NuGet package version number, which will change when the NuGet package is updated.

+4


source


The solution found to this was not as specific as the initial error stated.

My version of Newtonsoft.Json is version 7.0.1, but the compiled version of Newtonsoft.Json.Schema was against version 6.0.8, which threw an internal "version difference" error but never popped to the top of the stack, and T4 just notified. that the metadata cannot be found (theoretically correct), but not very specific.



I grabbed a copy of Newtonsoft.Json.Schema from GitHub and compiled this against version 7, from NuGet and the error went away.

+1


source







All Articles