public void Init(AppDomain domain, IPrinter printer = null) { (new PermissionSet(PermissionState.Unrestricted)).Assert(); if (_domain != null) { throw new InvalidOperationException("CleanupHelper can be used only for a single domain."); } if (domain == null) { throw new ArgumentNullException("domain"); } if (printer == null) { printer = new ErrorOnlyPrinter(); } _printer = printer; if (domain.IsDefaultAppDomain()) { //throw new InvalidOperationException("CleanupHelper will not work on the default AppDomain") printer.PrintError("We can't cleanup temp files if you use RazorEngine on the default Appdomain."); printer.PrintError("Create a new AppDomain and use RazorEngine from there!"); } printer.Print("init cleanup helper for {0} in {1} ...", domain.FriendlyName, AppDomain.CurrentDomain.FriendlyName); this._domain = domain; //_domain.DomainUnload += domain_DomainUnload; ObjectHandle handle = Activator.CreateInstanceFrom( _domain, typeof(SubscribeHelper).Assembly.ManifestModule.FullyQualifiedName, typeof(SubscribeHelper).FullName ); using (var subscribeHelper = (SubscribeHelper)handle.Unwrap()) { if (System.Runtime.Remoting.RemotingServices.IsTransparentProxy(printer)) { subscribeHelper.CheckPrinter(printer); } subscribeHelper.Subscribe(this); } }