views:

977

answers:

2

I'm trying to add the T4MVC templates to my project, but I'm experiencing some problems. I went to Codeplex and downloaded the latest version of T4MVC, and according to the instructions I just copied the two files T4MVC.tt and T4MVC.Settings.t4 into the root of my web application.

Immediately, I got the following errors:

From T4MVC.cs (generated file):

A namespace cannot directly contain members such as fields or methods

From T4MVC.tt (the code generating template):

Compiling transformation: The type or namespace name 'ITextTemplatingEngineHost' could not be found (are you missing a using directive or an assembly reference?)

When I open T4MVC.cs, it only contains one line:

ErrorGeneratingCode

I found this post that suggests just building again, but that solution does not solve my problem - in fact, it doesn't change a thing. What should I do?

+4  A: 

OK, I figured it out. The problem was that apparently since the last release of the T4MVC package, Microsoft changed the location of the ITextTemplateHost interface, so I needed to import another namespace. Also, the .dll files with these namespaces aren't imported in the default ASP.NET MVC template project. This is what I did to make it work:

  1. Add references to the following .dll files (search paths on my machine in brackets):

    • Microsoft.VisualStudio.TextTemplating.10.0.dll (*C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.VisualStudio.TextTemplating.10.0\v4.0_10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.10.0.dll*)

    • Microsoft.VisualStudio.TextTemplating.Interfaces.10.0.dll (*C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.VisualStudio.TextTemplating.Interfaces.10.0\v4.0_10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.TextTemplating.Interfaces.10.0.dll*)

  2. Make sure the following namespaces are both imported in T4MVC.tt (just follow the syntax that's already in the file).

    • Microsoft.VisualStudio.TextTemplating

    • Microsoft.VisualStudio.TextTemplating.Interfaces

  3. Delete all generated code files (they'll appear if you expand the T4MVC.tt in Solution Explorer).

  4. Build project. If no files are generated, open T4MVC.tt, edit something, don't save and build. That should do it!

Tomas Lycken
what a pain. took me an hour to figure out.
boomhauer
+1  A: 

Seems you got it working, but let me add a few points here.

About the need to import Microsoft.VisualStudio.TextTemplating.Interfaces, I added a comment about that at the top of T4MVC.tt. But yeah, it's easy to miss.

Now for the confusing part: while those interfaces indeed moved to that different namespace in Beta2, the T4 team had a change of heart and decided to move them back again to the original namespace to avoid confusion. So when the next RC build comes out, this namespace will be gone.

About the need to import DLLs, I'm surprised that you need this, as it works for me without it.

Final note: in step #4, it should be harmless if you save here. What happens is that T4MVC has logic that keeps itself dirty, so even if you save, you'll see it marked as unsaved (this can be turned off in the settings file).

Hope this helps!

David Ebbo
Hi David! Thanks for the heads up for the RC changes - now I know on beforehand what might cause this to fail again. About the saving part, I needed to do that to mark the file dirty the first time; note the "if no files are generated...". If I didn't, the logic marking the file dirty wouldn't run. However, if for someone it works directly, there is no need to do this for the next compilation.
Tomas Lycken