locked
T4 templates assembly reference RRS feed

  • Question

  • Hi,

    How can I add assembly reference to my T4 template but not having one in my output but from GAC or some Public assmebly FOlder?

    this doesn't work ...
    <#@ Assembly Name="MyUtilities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=62d75fdadbb6ec71" #>

    this neither
    <#@ Assembly Name="MyUtilities.dll" #>

    this neither
    <#@ Assembly Name="MyUtilities" #>

    this is some assembly that is installed along with some other package and exists iin GAC.
    If I put this assembly in my Output it works but this is something I don't want to do.

    all the time I got error...
    Error: Compiling transformation: Metadata file 'MyUtilities.dll' could not be found (0, 0)


    If I use this (and the assembly is there...):
    <#@ Assembly Name="c:\temp\MyUtilities.dll" #>

    Error is : Invalid assembly name c:\temp\MyUtilities

    Anyone Help this is urgent..

    Thanks

    Ales M.


    Long time no see
    Tuesday, June 17, 2008 7:53 AM

Answers

  • For a standalone application, the easiest option would be to use the command line host - TextTransform.exe, which takes -P parameters you can use to specify location of your custom assemblies. Command line host combines the path you provide in the -P parameter with the file name you specify in the <#@ assembly #> directive to resolve the assembly reference.

    If that doesn't work for your needs, the only option I can think of is to implement a custom ITextTemplatingEngineHost and provide the required logic in the ResolveAssemblyReference method.

    By the way, do you plan to redistribute the T4 with your application?
    • Marked as answer by Ales M_ Wednesday, June 18, 2008 7:21 AM
    • Marked as answer by Ales M_ Wednesday, June 18, 2008 7:21 AM
    Tuesday, June 17, 2008 5:46 PM

All replies

  • You could use the namespace as name of the assembly, like <#@ assembly name="System.Core" #> and add a reference for the assembly to your project.

    • Proposed as answer by Erik____ Tuesday, June 17, 2008 10:21 AM
    • Unproposed as answer by Ales M_ Tuesday, June 17, 2008 10:30 AM
    Tuesday, June 17, 2008 9:52 AM
  • I am interested in this issue. Would you mind letting me know the result of Erik's suggestions?

    If you need further assistance, feel free to let me know. I will be more than happy to be of assistance.

    Have a great day!

    Thanks!

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, June 17, 2008 10:21 AM
  • Hi, and thanks for quick answer...


    It didn't help. Same error :
    Error: Compiling transformation: Metadata file 'MyNamespace' could not be found (0, 0)

    and yes I do need further assistance ...

    just to mention I also installed GAX and GAT on my machine ...

    Ales M.

    Long time no see
    Tuesday, June 17, 2008 10:32 AM
  • Alex,

    Which text templating engine host are you using to transform T4 templates? Standard Visual Studio host does support all three of the options mentioned in your post and previous responses - rooted path, GAC and project reference. It is also possible to place the assembly in one of the directories listed in Reference Paths of the project options (more here).

    On the other hand, GAX host may not support these options, which can be a problem if you are transforming T4 templates as part of GAX recepies or Visual Studio project item templates.
    Tuesday, June 17, 2008 11:38 AM
  • Hi,

    I do not transfor Templates as part of GAX recipes ... (yet)...

    I am using host like this:

    ITextTemplatingEngine engine = new Microsoft.VisualStudio.TextTemplating.Engine();
    string templateCode = File.ReadAllText(Path.Combine(m_BasePath, m_T4FileName));
    TemplateHost host = new TemplateHost(m_BasePath, t4arguments);
    host.TemplateFile =
    @"";
    //host.TemplateFile = m_T4FileName;
    m_Result = engine.ProcessTemplate(templateCode, host);


     ... I noticed that I do have two Microsoft.VisualStudio.TextTemplating dll-s:

    C:\Program Files\Common Files\microsoft shared\TextTemplating\1.1\Microsoft.VisualStudio.TextTemplating.dll
    and
    C:\Program Files\Microsoft Visual Studio 2008 SDK\VisualStudioIntegration\Common\Assemblies\Microsoft.VisualStudio.TextTemplating.dll


    ...


    Long time no see
    Tuesday, June 17, 2008 11:58 AM
  • In this code example you are using TemplateHost (GAX host) explicitly, which explains why T4 could not resolve rooted assembly name. There are several techniques to get around this limitation. Could you describe what you are trying to do with this code? I.e. why do you need to use the Engine and process the template manually?

    Tuesday, June 17, 2008 1:08 PM
  • Hi and thaks for help You are providing,

    I am trying to create standalone application (this is requirement) which will be used for generation of assets. This tool will be used from time to time and there are a lot of assets that I should generate (~ 1000) and this number of course will grow..

    If you have workaround I'll be very happy :)


    also I tried to create my base class and to add using statement in it and inherits template from it ...

    using MyNamespace;
    public abstract class BaseTextTransformation : TextTransformation
    ...
    ... but no luck

    p.s.
    I also tried with Clarius T4 editor (which has TransformText command in Vs2008) with different kind of hosts (I'm setting host in property editor in design time so above code where I use GAX explicitly ... as You say ... isn't invoked at all) but no luck...

    ... and I'm getting  frustrated :) ...

    Thanks,

    Alex M.

     


    Long time no see
    Tuesday, June 17, 2008 4:02 PM
  • For a standalone application, the easiest option would be to use the command line host - TextTransform.exe, which takes -P parameters you can use to specify location of your custom assemblies. Command line host combines the path you provide in the -P parameter with the file name you specify in the <#@ assembly #> directive to resolve the assembly reference.

    If that doesn't work for your needs, the only option I can think of is to implement a custom ITextTemplatingEngineHost and provide the required logic in the ResolveAssemblyReference method.

    By the way, do you plan to redistribute the T4 with your application?
    • Marked as answer by Ales M_ Wednesday, June 18, 2008 7:21 AM
    • Marked as answer by Ales M_ Wednesday, June 18, 2008 7:21 AM
    Tuesday, June 17, 2008 5:46 PM
  • Hi Oleg,

    I managed to do the thing with custom ITextTemplatingEngineHost and custom  ResolveAssemblyReference.
    Yes I do plan to redistribute T4 with my application.


    Thanks a lot...

    Ales M.
    Long time no see
    Wednesday, June 18, 2008 7:24 AM
  • I tried the following and it worked for me.

    <#@ assembly name="$(SolutionDir)\MVCModels\bin\...dll"#>

    Wednesday, August 7, 2013 8:51 PM