private IScriptRunner GetOrCreateScriptRunner(IParameter[] parameters, string scriptCode, ScriptingOptions Options) { var hostingType = (Options == null) ? HostingType.SharedSandboxAppDomain : Options.HostingType; AppDomain sandbox = GetOrCreateHostingDomain(hostingType); // We can return a cached script runner if // A) there is one (duh...), AND // B) It does not need to recompile for the given input arguments, AND // C) the AppDomain in which the script runner is hosted is the same that we would like to use now if (m_ScriptRunnerCacheInfo != null && !m_ScriptRunnerCacheInfo.ScriptRunner.NeedsRecompilationFor(parameters, scriptCode, Options) && m_ScriptRunnerCacheInfo.HostingDomain == sandbox) { Trace.WriteLine("Using cached script runner"); return(m_ScriptRunnerCacheInfo.ScriptRunner); } else { Trace.WriteLine("Creating new script runner"); TryUnregisterLease(); var scriptRunner = (HostedScriptRunner <TGlobals>)Activator.CreateInstance(sandbox, typeof(HostedScriptRunner <TGlobals>).Assembly.FullName, typeof(HostedScriptRunner <TGlobals>).FullName).Unwrap(); HostingHelper.ClientSponsor.Register(scriptRunner); this.m_ScriptRunnerCacheInfo = new ScriptRunnerCacheInfo <TGlobals> { HostingDomain = sandbox, ScriptRunner = scriptRunner }; return(scriptRunner); } }
/// <summary> /// Tries to unregister the lease of the cached script runner /// </summary> private void TryUnregisterLease() { if (m_ScriptRunnerCacheInfo != null && m_ScriptRunnerCacheInfo.ScriptRunner != null) { HostingHelper.ClientSponsor.Unregister(m_ScriptRunnerCacheInfo.ScriptRunner); m_ScriptRunnerCacheInfo = null; } }