VSPackage for VS 2012

Jun 13, 2013 at 12:49 PM
I am trying to make VS Package for Code Generation in VS 2012 using latest t4 toolbox. I would like to start code generation process from package similar to right clicking on .tt file and invoking RunCustomTool. I made method as you suggested in some previous discussions.
private void GenerateCode()
    var textTemplating = (ITextTemplating)serviceProvider.

    var templatingComponents = (ITextTemplatingComponents)textTemplating;

    if (!String.IsNullOrEmpty(this.txtFile)
        && !String.IsNullOrEmpty(this.txtContent))
            this.lblSolutionName.Content = 
        catch (Exception ex)
This is txt file that I load and from which i read content. MyCodeGenerator class inherits T4Toolbox.Template
<#@ template language="C#" debug="true" hostspecific="true" #>
<#@ include file="T4Toolbox.tt" #>
<#@ assembly name="MyCodeGeneration.dll" #>
<#@ import namespace="MyCodeGeneration" #>

    var myCodeGenerator = new MyCodeGenerator();
catch(TransformationException transEx) { }
catch(Exception ex) { }
and I get Exception:
An exception of type 'System.NullReferenceException' occurred in mscorlib.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object.
and in VS Error List:
Errors were generated when initializing the transformation object. The transformation will not be run.  The following Exception was thrown:
System.NullReferenceException: Object reference not set to an instance of an object.

Server stack trace: 
   at T4Toolbox.VisualStudio.TransformationContextProvider.T4Toolbox.ITransformationContextProvider.GetMetadataValue(Object hierarchyObject, String fileName, String metadataName)
   at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Object[]& outArgs)
   at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at T4Toolbox.ITransformationContextProvider.GetMetadataValue(Object hierarchy, String fileName, String metadataName)
   at T4Toolbox.TransformationContext.GetMetadataValue(String metadataName)
   at T4Toolbox.TransformationContext.InitializeParameters()
   at T4Toolbox.TransformationContext..ctor(TextTransformation transformation, StringBuilder generationEnvironment)
   at T4Toolbox.TransformationContext.Initialize(TextTransformation transformation, StringBuilder generationEnvironment)
   at Microsoft.VisualStudio.TextTemplating33278AD85342564FFC3FDDCDE1AE9710257F30A10A7B73F37A9864BF84BBDC5C02567C6A9D94B0352CB008231EB459127A0C7C0693024A90324C8FCDAC282FFB.GeneratedTextTransformation.Initialize() in d:\temp\testingCodeGen_file.txt:line 16 
   at CallSite.Target(Closure , CallSite , Object )
   at Microsoft.VisualStudio.TextTemplating.TransformationRunner.PerformTransformation()
Same exception occurs even if i leave only this 2 lines in my txt file!
<#@ template language="C#" debug="true" hostspecific="true" #>
<#@ include file="T4Toolbox.tt" #>
I also tried to implement my ITextTemplatingCallback class and I get same result.
I even tried to play a bit with implementation of my ITextTemplatingEngineHost but did not figure out how to resolve ResolveDirectiveProcessor method to return right DirectiveProccessor type.

I read in previous discussions that someone added full path to T4Toolbox.tt file and it worked for his VS Add-in, but that did not not help me.

When I try to import assembly T4Toolbox.dll in my txt file like this:
<#@ assembly name="C:\Users\user_name\AppData\Local\Microsoft\VisualStudio\11.0\Extensions\gjjitlzr.erb\T4Toolbox.dll" #>
<#@ import namespace="T4Toolbox" #>
my code starts executing, but first time when Render() method of T4Toolbox.Template is called I get TransformationException:
"T4Toolbox.TransformationException: T4 Toolbox transformation context was not properly initialized. Add <#@ include file=\"T4Toolbox.tt\" #> directive to your text template.
As you explained in that discussion it is probably case when ModelingTextTransformation overrides the Session property used by T4 toolbox and leaves it unassigned by default.

I would like to avoid adding EnvDTE.ProjectItem to solution and calling RunCustomTool method on VSProjectItem from my package if possible.

So I am pretty stuck right now and any help would be much appreciated.
Jun 13, 2013 at 6:15 PM
This forum is for the CodePlex website itself. You might want to post this on the discussion forum of the corresponding project.