var setup = new AppDomainSetup
{
ApplicationName = $"{Name} {Id}",
ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
DisallowCodeDownload = true,
DisallowPublisherPolicy = true,
DisallowBindingRedirects = true
};
var scriptDomain = AppDomain.CreateDomain(setup.ApplicationName, null, setup, new PermissionSet(PermissionState.Unrestricted));
ScriptProvider<TScript> scriptProvider;
try
{
var scriptProviderHandle = Activator.CreateInstanceFrom(scriptDomain,
typeof(ScriptProvider<TScript>).Assembly.ManifestModule.FullyQualifiedName,
typeof(ScriptProvider<TScript>).FullName);
scriptProvider = (ScriptProvider<TScript>)scriptProviderHandle.Unwrap();
scriptProvider.Initialize(assemblyPath, ScriptTypeName);
}
catch
{
AppDomain.Unload(scriptDomain);
throw;
}
if (appDomain != null)
{
Debug.Print($"{nameof(Scripting)}: Unloading domain {appDomain.FriendlyName}");
AppDomain.Unload(appDomain);
}
appDomain = scriptDomain;
return scriptProvider;