How to handle Serialization Issue with a Custom Host Implementing ITextTemplatingEngineHost

Answered How to handle Serialization Issue with a Custom Host Implementing ITextTemplatingEngineHost

  • Saturday, November 01, 2008 10:02 PM
     
     

    As I noted in an earlier post :Source Code for a Working Implementation of ITextTemplatingEngineHost, 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. 

    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:

            public AppDomain ProvideTemplatingAppDomain(string content)
            {
                // This host will provide a new application domain each time the
                // engine processes a text template.
                // -------------------------------------------------------------
                return AppDomain.CreateDomain("Generation App Domain");
            }

    This works fine if I supply a new AppDomain but do not include the "hostspecific=true" parameter on the template directive.  It also works just fine if I supply the current Appdomain and include the "hostspecific=true" parameter.  It DOES NOT WORK if I supply a new AppDomain and also include the "hostspecific=true" parameter.  The issue is that the Engine class is not serializable.  Apparently, it must be serializable to move across the AppDomain boundary.

    I have to specify the "hostspecific=true" 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 "host specific" at the same time?

    Jon Stonecash

All Replies

  • Sunday, November 02, 2008 1:36 PM
     
     Answered
    Jon,

    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 "hostspecific" template running in a new AppDomain.



    Oleg
    • Marked As Answer by Jon Stonecash Monday, November 03, 2008 10:46 AM
    •  
  • Sunday, November 02, 2008 9:21 PM
     
     
    Cue sound of hand slapping forehead!

    That worked.  Thanks.

    Jon Stonecash