Processor named ‘T4VSHost’ could not be found for the directive named ‘CleanupBehavior’

There are two scenarios in which you may see the ‘Processor named ‘T4VSHost’ could not be found for the directive named ‘CleanupBehavior’’ error after installing Visual Studio 2012 Update 1.

  • Inside Visual Studio when code is being generated from an EF model – this isn’t supposed to happen and something probably went wrong during install.
  • When running EF code generation using TextTransform.exe – you need to let the T4 tool know about a new directive we added in Update 1.

 


What Changed?

In Visual Studio 2012 Update 1 we introduced some changes to the T4 processor (and our T4 templates) to allow virtual memory used during code generation to be freed back up by Visual Studio once code generation was complete. To enable this freeing of memory, we include a new CleanupBehavior directive in our T4 templates.

 


Resolving the Issue in Visual Studio

If you are seeing the “processor named ‘T4VSHost’ could not be found for the directive named ‘CleanupBehavior’” error from within Visual Studio then you have two options:

 

Option 1 – Remove the CleanupBehavior Directive

The simplest way to resolve the issue is just to remove the CleanupBehavior directive from the EF ttinclude files. VS will hold onto a little more virtual memory… but I doubt you will notice the difference.

  1. Browse to C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes\
    (or whatever directory you installed Visual Studio in)
  2. Open each of the following files in notepad:
    • EF.Utility.CS.ttinclude
    • EF.Utility.VB.ttinclude
  3. Remove the following text from the first line of each file and save:
    <#@ CleanupBehavior Processor=”T4VSHost” CleanupAfterProcessingTemplate=”true” #>

 

Option 2 – Ensure Directive Processor is Registered

The second option is to ensure the directive processor is registered for Visual Studio.

  1. Make sure you have the following registry key:
    HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\11.0_Config\TextTemplating\DirectiveProcessors\T4VSHost
  2. They key should have the following REG_SZ values:
    • Class: Microsoft.Data.Entity.Design.VisualStudio.Directives.FallbackT4VSHostProcessor
    • CodeBase: C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Microsoft.Data.Entity.Design.dll
      (obviously you may need to update the CodeBase path depending on where you installed Visual Studio)

    RegKeys.png

 


Resolving the Issue for TextTransform.exe

TextTransform.exe is a tools that allows you to process T4 templates from the command line. When working in Visual Studio, the processor to handle the CleanupBehavior directive is automatically registered. However, when using TextTransform.exe it’s not and you will get the following message if you try to process your .tt file.

C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes\EF.Utility.CS.ttinclude(1,4) : error : A processor named 'T4VSHost' could not be found for the directive named 'CleanupBehavior'. The transformation will not be run.  The following Exception was thrown:
System.IO.FileNotFoundException: Failed to load directive processor T4VSHost.
at Microsoft.VisualStudio.TextTemplating.CommandLine.CommandLineHost.ResolveDirectiveProcessor(String processorName)
at Microsoft.VisualStudio.TextTemplating.Engine.ProcessCustomDirectives(ITextTemplatingEngineHost host, TemplateProcessingSession session, IEnumerable`1 directivesToBeProcessed)

Again, you have two options for resolving the issue.

 

Option 1 – Remove the CleanupBehavior Directive

The easiest way to resolve the issue is to follow the same steps listed under Option 1 in the previous section to remove the CleanupBehavior directive from the EF ttinclude files.

 

Option 2 – Register the Directive Processor with TextTransform.exe

You can use the –dp switch to register additional directive processors. Adding the following to your command will register the processor to deal with the new CleanupBehavior directive.

-dp T4VSHost!Microsoft.Data.Entity.Design.VisualStudio.Directives.FallbackT4VSHostProcessor!"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Microsoft.Data.Entity.Design.dll"

Here is a complete command sample:

> "C:\Program Files (x86)\Common Files\Microsoft Shared\TextTemplating\11.0\TextTransform.exe"
    -out MyModel.cs
    -I "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes"
    -a !!NamespaceHint!MyNamespace
    -dp T4VSHost!Microsoft.Data.Entity.Design.VisualStudio.Directives.FallbackT4VSHostProcessor!"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Microsoft.Data.Entity.Design.dll"
    MyModel.tt
Advertisements