Bug 3493 - Unable to cast transparent proxy to type 'Mono.TextTemplating.CompiledTemplate'.
Summary: Unable to cast transparent proxy to type 'Mono.TextTemplating.CompiledTemplate'.
Alias: None
Product: Xamarin Studio
Classification: Desktop
Component: T4 ()
Version: 2.8.6
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Mikayla Hutchinson [MSFT]
Depends on:
Reported: 2012-02-16 07:34 UTC by Zauberertz
Modified: 2012-02-20 17:42 UTC (History)
1 user (show)

Is this bug a regression?: ---
Last known good build:

T4 file (127 bytes, application/octet-stream)
2012-02-16 07:34 UTC, Zauberertz

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Zauberertz 2012-02-16 07:34:40 UTC
Created attachment 1366 [details]
T4 file

The attached T4 file gives me following error on Windows:

Der Code Generator 'TextTemplatingFileGenerator' ist abgestürzt. Unable to cast transparent proxy to type 'Mono.TextTemplating.CompiledTemplate'.

The error only happens if you add the T4 file to a existing project.
After closing and reopening the project the T4 works as expected.
Comment 1 Mikayla Hutchinson [MSFT] 2012-02-17 15:58:25 UTC
That's very strange, I could have sworn I fixed this issue in this commit over a year ago: https://github.com/mono/monodevelop/commit/2aeb6a00be6fe25251e08e30ede1b49f0fd39281

You can run the T4 file correctly after reloading the project? Is this a problem for all new T4 files, or just the first?
Comment 2 Mikayla Hutchinson [MSFT] 2012-02-18 21:53:16 UTC
I can repro this. I think the problem is still load contexts: http://msdn.microsoft.com/en-us/library/dd153782.aspx

Since MD loads addins (including MonoDevelop.TextTemplating) using Assembly.LoadFrom, it'll be in the load-from context. But the appdomain and the T4 engine are is using the default load context, so .NET thinks the types aren't the same when remoting and unwrapping them.
Comment 3 Mikayla Hutchinson [MSFT] 2012-02-18 23:07:54 UTC
This is looking pretty hard to fix. It's really hard to debug because the only tool for tracking the load contexts is fuslogvw, which is really unreliable - it only shows a small subset of events, for some reason.

AFAICT problem is
1) appdomain serialization and proxy casts use Load calls, not LoadFrom calls. This affects e.g. objects passed as parameters to CreateInstanceFrom.
2) if Load can find the dll, it'll load it into the load context and use it, even if LoadFrom had already loaded it
3) Only if Load can't find the dll, can you can use an AssemblyResolve event handler. This handler can return a dll that's been loaded into the load-from context.
4) The AssemblyResolve handler has to be sent over to the appdomain in the first place, so it must be Load-able from in the appdomain's codebase.
5) MonoDevelop.TextTemplating must therefore be in the appdomain's codebase directory to implement and attach an AssemblyResolve handler directly, so it will be in the appdomain's Load context.
6) But MD loads MonoDevelop.TextTemplating using LoadFrom, because addins aren't in MD's CodeBase paths.

=> MonoDevelop.TextTemplating is always in mismatched contexts