How to handle Serialization Issue with a Custom Host Implementing ITextTemplatingEngineHost<br>As I noted in an earlier post :<a href="http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/523f51f4-065c-47cd-85cc-328125974ae3">Source Code for a Working Implementation of ITextTemplatingEngineHost</a>, I am trying to build a custom host for T4.  The answer to my query about available source code for a working custom host lead to the code in the MSDN documentation.  That code worked.  Using that code as a baseline, I was able to isolate the cause of my problem.  <br><br>My situation is that I want to expose the custom host in the template file.  Following the example in MSDN, I wrote the following code for the ProvideTemplatingAppDomain:<br><br>        public AppDomain ProvideTemplatingAppDomain(string content)<br>        {<br>            // This host will provide a new application domain each time the <br>            // engine processes a text template.<br>            // -------------------------------------------------------------<br>            return AppDomain.CreateDomain(&quot;Generation App Domain&quot;);<br>        }<br><br>This works fine if I supply a new AppDomain but do not include the &quot;hostspecific=true&quot; parameter on the template directive.  It also works just fine if I supply the current Appdomain and include the &quot;hostspecific=true&quot; parameter.  It DOES NOT WORK if I supply a new AppDomain and also include the &quot;hostspecific=true&quot; parameter.  The issue is that the Engine class is not serializable.  Apparently, it must be serializable to move across the AppDomain boundary.<br><br>I have to specify the &quot;hostspecific=true&quot; parameter to make the functionality that I am trying to provide work.  In the short term, I can work around this by re-using the AppDomain, but I wonder how that will affect Visual Studio performance.  Am I right in thinking that the custom tool will run in a separate process space?  If it does, the AppDomains will only live for the duration of the code generation process; there would be no accumulation in memory.  But the author of the MSDN sample took some effort to mention that creating a searpate AppDomain was a good idea.  Is there a way to get a new AppDomain and &quot;host specific&quot; at the same time?<br><br>Jon Stonecash<br><br>© 2009 Microsoft Corporation. All rights reserved.Mon, 03 Nov 2008 10:46:22 Z543a9fc6-0aa4-4cb9-86a1-cde6ade85867http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/543a9fc6-0aa4-4cb9-86a1-cde6ade85867#543a9fc6-0aa4-4cb9-86a1-cde6ade85867http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/543a9fc6-0aa4-4cb9-86a1-cde6ade85867#543a9fc6-0aa4-4cb9-86a1-cde6ade85867Jon Stonecashhttp://social.msdn.microsoft.com/Profile/en-US/?user=Jon%20StonecashHow to handle Serialization Issue with a Custom Host Implementing ITextTemplatingEngineHost<br>As I noted in an earlier post :<a href="http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/523f51f4-065c-47cd-85cc-328125974ae3">Source Code for a Working Implementation of ITextTemplatingEngineHost</a>, I am trying to build a custom host for T4.  The answer to my query about available source code for a working custom host lead to the code in the MSDN documentation.  That code worked.  Using that code as a baseline, I was able to isolate the cause of my problem.  <br><br>My situation is that I want to expose the custom host in the template file.  Following the example in MSDN, I wrote the following code for the ProvideTemplatingAppDomain:<br><br>        public AppDomain ProvideTemplatingAppDomain(string content)<br>        {<br>            // This host will provide a new application domain each time the <br>            // engine processes a text template.<br>            // -------------------------------------------------------------<br>            return AppDomain.CreateDomain(&quot;Generation App Domain&quot;);<br>        }<br><br>This works fine if I supply a new AppDomain but do not include the &quot;hostspecific=true&quot; parameter on the template directive.  It also works just fine if I supply the current Appdomain and include the &quot;hostspecific=true&quot; parameter.  It DOES NOT WORK if I supply a new AppDomain and also include the &quot;hostspecific=true&quot; parameter.  The issue is that the Engine class is not serializable.  Apparently, it must be serializable to move across the AppDomain boundary.<br><br>I have to specify the &quot;hostspecific=true&quot; parameter to make the functionality that I am trying to provide work.  In the short term, I can work around this by re-using the AppDomain, but I wonder how that will affect Visual Studio performance.  Am I right in thinking that the custom tool will run in a separate process space?  If it does, the AppDomains will only live for the duration of the code generation process; there would be no accumulation in memory.  But the author of the MSDN sample took some effort to mention that creating a searpate AppDomain was a good idea.  Is there a way to get a new AppDomain and &quot;host specific&quot; at the same time?<br><br>Jon Stonecash<br><br>Sat, 01 Nov 2008 22:02:06 Z2008-11-01T22:02:06Zhttp://social.msdn.microsoft.com/Forums/en-US/vsx/thread/543a9fc6-0aa4-4cb9-86a1-cde6ade85867#264f421f-dfc3-4130-99f8-f46dde7dc652http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/543a9fc6-0aa4-4cb9-86a1-cde6ade85867#264f421f-dfc3-4130-99f8-f46dde7dc652Oleg V. Sychhttp://social.msdn.microsoft.com/Profile/en-US/?user=Oleg%20V.%20SychHow to handle Serialization Issue with a Custom Host Implementing ITextTemplatingEngineHost Jon,<br><br>I think that changing your host class to inherit from MarshalByRefObject should resolve the serialization issue and allow you to access the host from a &quot;hostspecific&quot; template running in a new AppDomain.<br><br><br><hr size="1" align="left" width="25%">OlegSun, 02 Nov 2008 13:36:38 Z2008-11-02T13:36:38Zhttp://social.msdn.microsoft.com/Forums/en-US/vsx/thread/543a9fc6-0aa4-4cb9-86a1-cde6ade85867#b1233526-359e-48ad-bc71-64e9047134e0http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/543a9fc6-0aa4-4cb9-86a1-cde6ade85867#b1233526-359e-48ad-bc71-64e9047134e0Jon Stonecashhttp://social.msdn.microsoft.com/Profile/en-US/?user=Jon%20StonecashHow to handle Serialization Issue with a Custom Host Implementing ITextTemplatingEngineHost Cue sound of hand slapping forehead!<br><br>That worked.  Thanks.<br><br>Jon StonecashSun, 02 Nov 2008 21:21:54 Z2008-11-02T21:21:54Z