1

My requirement is when the build happens the T4 template should get executed.
Here is the code I have written in the template.

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>

<#@ assembly name="$(SolutionDir)dll\Newtonsoft.Json.dll" #>

<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO"#>
<#@ import namespace="System.Text"#>

<#@ import namespace="Newtonsoft.Json"#>
<#@ import namespace="Newtonsoft.Json.Linq"#>

<#@ output extension=".txt" #>

<#

string serverPath = this.Host.ResolvePath("DLLs.js");

string body;
using (var stream = new FileStream(serverPath, FileMode.Open, FileAccess.Read))
{
    var streamReader = new StreamReader(stream);
    body = streamReader.ReadToEnd();

}

#>

<#=body#>

This runs fine but when I am trying to build it is giving an exception that it is unable to find the Newtonsoft DLL.

Here is what I did in the project file to run the template when the solution builds.

<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\TextTemplating\v11.0\Microsoft.TextTemplating.targets" />
<PropertyGroup>
 <TransformOnBuild>true</TransformOnBuild>
</PropertyGroup>

  <ItemGroup>
    <T4ReferencePath Include="$(VsIdePath)PublicAssemblies\" />
  </ItemGroup>

The Newtonsoft.dll is in a folder called DLL in the root of solution folder.

I saw the post @ Get Visual Studio to run a T4 Template on every build and also followed the steps suggested by @MarkGr but with no luck.

I am using VS2012 . Can anybody tell me where am I going wrong?

Community
  • 1
  • 1
Anirban
  • 589
  • 3
  • 16
  • 40
  • Can you tell us exactly what happened (and what _didn't_ happen) when you tried those instructions? – John Saunders Dec 05 '12 at 19:29
  • When I build I get the following build error : Error 4 The host threw an exception while trying to resolve the assembly reference '$(SolutionDir)dll\Newtonsoft.Json.dll'. The transformation will not be run. The following Exception was thrown: System.IO.FileLoadException: The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047) at System.Reflection.AssemblyName.nInit(RuntimeAssembly& assembly, Boolean forIntrospection, Boolean raiseResolveEvent) at System.Reflection.AssemblyName..ctor(String assemblyName) – Anirban Dec 05 '12 at 22:55
  • Hmm. Looks like a 64/32-bit issue. – John Saunders Dec 05 '12 at 23:08
  • Try FusionLogViewer (shipped with VS) to see how the build process is resolving the assembly dependencies. That way you can see if it's probing the right paths. You could try GAC:ing the problem assembly to see if it works. – Just another metaprogrammer Dec 06 '12 at 05:58
  • I don't think MSBUild resolves $(SolutionDir) at all hence the error. Looking for a workaround... – Miha Markic Dec 03 '13 at 12:28

1 Answers1

1

Try with this ItemGroup. It should do the trick. At least in VS2013

<ItemGroup>
    <T4ParameterValues Include="SolutionDir">
      <Value>$(SolutionDir)</Value>
    </T4ParameterValues>
  </ItemGroup>

http://blogs.msdn.com/b/t4/archive/2013/08/29/what-s-new-in-t4-for-visual-studio-2013.aspx

Miha Markic
  • 3,158
  • 22
  • 28