Microsoft 开发人员网络 > 论坛主页 > Visual Studio Extensibility > How to handle Serialization Issue with a Custom Host Implementing ITextTemplatingEngineHost
提出问题提出问题
 

已答复How to handle Serialization Issue with a Custom Host Implementing ITextTemplatingEngineHost

  • 2008年11月1日 22:02Jon Stonecash 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    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

答案

  • 2008年11月2日 13:36Oleg V. SychMVP用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    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

全部回复