T4 templates only run when I "Debug" them
Asked Answered
C

2

6

I'm having a bit of trouble here - my T4 templates that should generate C# classes refuse to successfully run when I save or "Run Custom Tool". However - it's only when I "Debug T4 Template" that it runs successfully without errors.

The error is as follows:

Error   1   Running transformation: System.MissingMethodException: Method not found: 'System.Collections.Generic.IEnumerator`1<Newtonsoft.Json.Linq.JToken> Newtonsoft.Json.Linq.JArray.GetEnumerator()'.
   at JsonCSharpClassGenerator.JsonClassGenerator.GenerateClass(JObject[] examples, JsonType type)
   at JsonCSharpClassGenerator.JsonClassGenerator.GenerateClasses() in c:\Users\Nero\Documents\Visual Studio 2013\Projects\BluePOCO\JsonCSharpClassGenerator\JsonClassGenerator.cs:line 82
   at ApiTransformer.ClassGenerator.Generate(String json, String className) in c:\Users\Nero\Documents\Visual Studio 2013\Projects\BluePOCO\ApiTransformer\ClassGenerator.cs:line 25
   at Microsoft.VisualStudio.TextTemplatingDCEBAFBE8B1AF87B73F34AE53B7F0A1037491F7FD56EC9906754016DE7399CD0992B239FF4A836115489D16A9EE78F6DCBA10BE4137758F32395F1DB7ADF7FF1.GeneratedTextTransformation.TransformText() C:\Users\Nero\documents\visual studio 2013\Projects\BluePOCO\BluePOCO\BlueprintTransformer.tt   1   1   BluePOCO

Which is obviously something to do with Json.Net. I thought the issue might have been in the Json.Net package, perhaps a discrepancy between versions - but all projects in the solution use exactly the same version.

For the record, JsonClassGenerator is this file: http://jsonclassgenerator.codeplex.com/SourceControl/latest#JsonCSharpClassGeneratorLib/JsonClassGenerator.cs

Line 82 is

GenerateClass(examples, rootType);

The obious solution would be ... debugging. However, I can't debug something that... well, refuses to bug when debugging. I seem to have exhausted my brain on this end, any ideas what I could try? Sure, the templates work fine, but having to run them every time I want them updated via right-click -> Debug? Doesn't seem to work as intended.

EDIT:

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Windows.Forms" #>
<#@ assembly name="$(SolutionDir)\BluePOCO\bin\Debug\ApiTransformer.dll" #>
<#@ assembly name="$(SolutionDir)BluePOCO\bin\Debug\RestSharp.dll"#>
<#@ import namespace="ApiTransformer"#>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Net"#>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="RestSharp"#>
<#@ output extension=".cs" #>
<#
var filename = "Source.txt";
var filepath = Path.Combine(Path.GetDirectoryName(this.Host.ResolvePath("")), "BluePOCO", filename);
var list = Transformer.GetMethodList(File.ReadAllText(filepath));
string str;
#>
using System.ComponentModel;
using Restcoration;
using RestSharp;
using Newtonsoft.Json; 
namespace BluePOCO 
{   <#foreach(var item in list){#>
<# str = item.Resource.Length > 1 ? string.Join("", item.Resource.Split('/').Select(x => x.Length > 0 ? x.Substring(0, 1).ToUpper() + x.Substring(1) : "")) : "Root";#>

<#
    var methodList = new List<string>(); 
    foreach(var response in item.Responses)
    {
        methodList.Add(string.Format("{0} = typeof({1})", Enum.GetName(typeof (HttpStatusCode), int.Parse(response.Code)), str + response.Code));
    }
    string add;  
    if(methodList.Any())
        add = ", " + string.Join(", ", methodList);
    else
        add = "";
#>

[Rest(Method = Method.<#=item.Method#><#=add#>)]<#foreach(var response in item.Responses){#>

<#=ClassGenerator.Generate(response.Json, str+response.Code)#>

<#}#>
<#}#>

}
Chrono answered 24/11, 2013 at 20:54 Comment(1)
Ever figure this out? It's happening to me 5 years later.Costive
E
0

Try

<#@ template debug="true"

Event answered 4/12, 2013 at 11:33 Comment(0)
M
0

I had the same problem when my project uses the old Newtonsoft.Json.dll v4.5.11.15520.

The problem went away after I change over to use the latest version of the Newtonsoft.Json.dll. v8.0.3.19514

Merow answered 14/4, 2016 at 0:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.