This is something that was reported on my
blog post, and that I have not been able to repro myself. Just trying to redirect the investigation to the forum.
Here is what was reported by Jeremy Brayton:
<div>
Have you tried this in a v1 Vs2008 MVC project?
This is the error I get:
Error 26 Running transformation: System.InvalidCastException: Specified cast is not valid.
at EnvDTE.Document.set_Saved(Boolean pRetval)
at GetProjectContainingT4File(DTE dte) in (Project)T4MVC.tt:line 360
at PrepareDataToRender(TextTransformation tt) in (Project)T4MVC.tt:line 318
at TransformText() in (Project)T4MVC.tt:line 36
(stripped mostly unnecessary stuff like my project location or the generated namespaces)
My workaround was pretty simple: comment the line out. Unfortunately that means generating the template every time as I don't get that cool "Hey I'm not really saved yet" hack.
As to why it all of a sudden broke? No clue. From what I was looking at it shouldn't have.
And then Andrew Gunn reported seeing the same thing:
<div>
I can reproduce the "Specified cast is not valid exception". It worked fine when I added the files to the project but threw the exception after I edited the .tt file to change some interfaces/namespaces.
My fix was to copy the files to another location, remove them VS and add them again - a bit of a pain. I'm using VS2008 SP1.
</div>
Andrew, are you able to reproduce this at will if you make this type of changes to the .tt file? Or it it somewhat random and intermittent?
I've changed how we do things slightly to get round this. I've moved T4MVC.tt into a central location and included a dummy .tt file in each MVC app to reference it:
<#@ template language="C#v3.5" debug="true" hostspecific="true" #>
<#@ Include File="C:\T4Templates\T4MVC.tt" #>
This means I can change T4MVC.tt whenever I want and it will update all associated MVC apps (which is kinda cool). I've removed the Custom Tool from each of these templates so they don't generate the .cs file(s). Instead, there is a "Before Build" event
to compile them. We found this to be easier to work with when we create new controllers/content files because we don't have to manually "Run Custom Tool" on the .tt file.
We've taken this approach in the past but it doesn't work for T4MVC.tt because it can't cast Microsoft.VisualStudio.TextTemplating.CommandLine.CommandLineHost to System.IServiceProvider. Do you know a way round this? I've tried updating the cast but then
it complains about not being able to find CommandLineHost.
Would you be able to include the exact steps that you take that lead to this original error? e.g.
Create new MVC app (or does it require a fancier app?)
Drop T4MVC in it
Make a specific change in T4MVC (please specify exactly what you're changing)
Rerun it and it blows up
I have not been able to reproduce this, but maybe I just don't know what to change in step 3.
For the second issue, I assume you're using TextTransform.exe? Unfortunately, this generally will not work with any T4 template that uses the VS object model (EnvDTE.DTE), because the command line tool just cannot provide all that information.
I like the idea of including T4MVC from a central location!
The steps you described are correct, but at step 3, I could simply add a space and I'd get the error!? I do have a T4 editor installed which I haven't used before so that could be causing the problem - http://t4-editor.tangible-engineering.com/T4-Editor-Visual-T4-Editing.html.
Having it in a central location makes it more re-usbable as you update it. Oh and thanks for the heads up on TextTransform.exe, we kinda figured as much. I guess we'll have to look at putting it back into Visual Studio but we didn't want the hastle of Re-running
it every time controllers/content change. We didn't want to store the generated .cs file(s) in source control either. Any ideas what we could do to satisfy those needs?
Can I make a few suggestions:
Make the multiple output files optional - I can see this becoming an issue with source control because new .cs files will get added and SourceSafe/TFS will try and check them in.
Separate the .tt file into separate units. It just seems bloated and personally, I'd like to build custom .tt files that sit on top of this base logic. This would mean I could easily update certain files when a new release comes out.
I think we'll have to do another of T4MVS that simply looks at the assembly to get the controllers (and doesn't concern itself with content like scripts, css and images). We just want to generate the strongly-typed helpers. Hope that makes sense, but I'm
happy to talk about it further it it doesn't!
Yes, I guess the culprit might be the tangible editor. I'll have to try it. Hopefully it repros with their free version.
Note that the multi-file output is optional. Look for SplitIntoMultipleFiles in T4MVC.settings.t4.
The idea of a T4MVC-light that doesn't do anything that requires using the VS host (and hence could run outside) might be ineteresting. Some features would of course not work (like the logic that makes things virtual if they're not).
If you don't mind, let's try to open new threads to discuss those other ideas, so we can keep this one strictly focused on the InvalidCast issue. Since others might run into it (we know of one other!), they can hopefully read through this
thread for info without getting lost in different topics :)
As the Jeremy initially reporting, I wanted to give my similarities which might help others in the future.
I am in fact running the free version of the tangible editor. I believe that is the single point of failure but exactly how? No clue. I am using a particularly older version and it has bugged to the point that I literally can't view any .tt file in its editor/colored
goodness but then again I *am* only looking at these specific .tt files (I had forked an earlier T4MVC file to hack together areas).
What I do find interesting was the fix of readding them to VS. My approach has always been to simply drop the files in and make my changes as necessary. Something about readding them must trigger some sort of reinit in the editor.
davidebb
Contributor
7006 Points
1366 Posts
Microsoft
Getting InvalidCastException during T4MVC execution.
Nov 25, 2009 07:46 PM|LINK
This is something that was reported on my blog post, and that I have not been able to repro myself. Just trying to redirect the investigation to the forum.
Here is what was reported by Jeremy Brayton:
<div>Have you tried this in a v1 Vs2008 MVC project?
This is the error I get:
Error 26 Running transformation: System.InvalidCastException: Specified cast is not valid.
at EnvDTE.Document.set_Saved(Boolean pRetval)
at GetProjectContainingT4File(DTE dte) in (Project)T4MVC.tt:line 360
at PrepareDataToRender(TextTransformation tt) in (Project)T4MVC.tt:line 318
at TransformText() in (Project)T4MVC.tt:line 36
(stripped mostly unnecessary stuff like my project location or the generated namespaces)
My workaround was pretty simple: comment the line out. Unfortunately that means generating the template every time as I don't get that cool "Hey I'm not really saved yet" hack.
As to why it all of a sudden broke? No clue. From what I was looking at it shouldn't have.
And then Andrew Gunn reported seeing the same thing:
<div>I can reproduce the "Specified cast is not valid exception". It worked fine when I added the files to the project but threw the exception after I edited the .tt file to change some interfaces/namespaces.
My fix was to copy the files to another location, remove them VS and add them again - a bit of a pain. I'm using VS2008 SP1.
</div>Andrew, are you able to reproduce this at will if you make this type of changes to the .tt file? Or it it somewhat random and intermittent?
thanks,
David
</div>andrewgunn
Member
4 Points
2 Posts
Re: Getting InvalidCastException during T4MVC execution.
Nov 26, 2009 07:39 AM|LINK
I can reproduce it at will.
I've changed how we do things slightly to get round this. I've moved T4MVC.tt into a central location and included a dummy .tt file in each MVC app to reference it:
<#@ template language="C#v3.5" debug="true" hostspecific="true" #>
<#@ Include File="C:\T4Templates\T4MVC.tt" #>
This means I can change T4MVC.tt whenever I want and it will update all associated MVC apps (which is kinda cool). I've removed the Custom Tool from each of these templates so they don't generate the .cs file(s). Instead, there is a "Before Build" event to compile them. We found this to be easier to work with when we create new controllers/content files because we don't have to manually "Run Custom Tool" on the .tt file.
We've taken this approach in the past but it doesn't work for T4MVC.tt because it can't cast Microsoft.VisualStudio.TextTemplating.CommandLine.CommandLineHost to System.IServiceProvider. Do you know a way round this? I've tried updating the cast but then it complains about not being able to find CommandLineHost.
Any ideas?
davidebb
Contributor
7006 Points
1366 Posts
Microsoft
Re: Getting InvalidCastException during T4MVC execution.
Nov 26, 2009 03:59 PM|LINK
Hi Andrew,
Would you be able to include the exact steps that you take that lead to this original error? e.g.
I have not been able to reproduce this, but maybe I just don't know what to change in step 3.
For the second issue, I assume you're using TextTransform.exe? Unfortunately, this generally will not work with any T4 template that uses the VS object model (EnvDTE.DTE), because the command line tool just cannot provide all that information.
I like the idea of including T4MVC from a central location!
David
andrewgunn
Member
4 Points
2 Posts
Re: Getting InvalidCastException during T4MVC execution.
Nov 27, 2009 07:35 AM|LINK
The steps you described are correct, but at step 3, I could simply add a space and I'd get the error!? I do have a T4 editor installed which I haven't used before so that could be causing the problem - http://t4-editor.tangible-engineering.com/T4-Editor-Visual-T4-Editing.html.
Having it in a central location makes it more re-usbable as you update it. Oh and thanks for the heads up on TextTransform.exe, we kinda figured as much. I guess we'll have to look at putting it back into Visual Studio but we didn't want the hastle of Re-running it every time controllers/content change. We didn't want to store the generated .cs file(s) in source control either. Any ideas what we could do to satisfy those needs?
Can I make a few suggestions:
I think we'll have to do another of T4MVS that simply looks at the assembly to get the controllers (and doesn't concern itself with content like scripts, css and images). We just want to generate the strongly-typed helpers. Hope that makes sense, but I'm happy to talk about it further it it doesn't!
Anyways, keep up the good work!
davidebb
Contributor
7006 Points
1366 Posts
Microsoft
Re: Getting InvalidCastException during T4MVC execution.
Nov 27, 2009 05:58 PM|LINK
Yes, I guess the culprit might be the tangible editor. I'll have to try it. Hopefully it repros with their free version.
Note that the multi-file output is optional. Look for SplitIntoMultipleFiles in T4MVC.settings.t4.
The idea of a T4MVC-light that doesn't do anything that requires using the VS host (and hence could run outside) might be ineteresting. Some features would of course not work (like the logic that makes things virtual if they're not).
If you don't mind, let's try to open new threads to discuss those other ideas, so we can keep this one strictly focused on the InvalidCast issue. Since others might run into it (we know of one other!), they can hopefully read through this thread for info without getting lost in different topics :)
thanks!
David
jeremybrayto...
Member
2 Points
1 Post
Re: Getting InvalidCastException during T4MVC execution.
Jan 21, 2010 02:24 PM|LINK
As the Jeremy initially reporting, I wanted to give my similarities which might help others in the future.
I am in fact running the free version of the tangible editor. I believe that is the single point of failure but exactly how? No clue. I am using a particularly older version and it has bugged to the point that I literally can't view any .tt file in its editor/colored goodness but then again I *am* only looking at these specific .tt files (I had forked an earlier T4MVC file to hack together areas).
What I do find interesting was the fix of readding them to VS. My approach has always been to simply drop the files in and make my changes as necessary. Something about readding them must trigger some sort of reinit in the editor.
davidebb
Contributor
7006 Points
1366 Posts
Microsoft
Re: Getting InvalidCastException during T4MVC execution.
Jan 21, 2010 03:15 PM|LINK
Thanks Jeremy. Hopefully, newer versions of the Tangible editor will be more stable and this issue will disappear.
David
tfischer_tan...
Member
16 Points
3 Posts
Re: Getting InvalidCastException during T4MVC execution.
Jan 31, 2010 02:05 PM|LINK
Hi here,
we have a new build tangible T4 Editor VS2008 Build that support T4 MVC now!
http://tangibleengineering.blogspot.com/2010/01/tangible-t4-editor-v16-for-vs2008.html
Best wishes
Tim