/// <summary> /// Run the hosted runtime, blocking the calling thread. /// </summary> /// <returns>True if the worker stopped as planned (e.g. due to updated assemblies)</returns> public bool Run() { var settings = RoleConfigurationSettings.LoadFromRoleEnvironment(); // The trick is to load this same assembly in another domain, then // instantiate this same class and invoke Run var domain = AppDomain.CreateDomain("WorkerDomain", null, AppDomain.CurrentDomain.SetupInformation); bool restartForAssemblyUpdate; try { _isolatedInstance = (SingleRuntimeHost)domain.CreateInstanceAndUnwrap( Assembly.GetExecutingAssembly().FullName, typeof(SingleRuntimeHost).FullName); // This never throws, unless something went wrong with IoC setup and that's fine // because it is not possible to execute the worker restartForAssemblyUpdate = _isolatedInstance.Run(settings); } finally { _isolatedInstance = null; // If this throws, it's because something went wrong when unloading the AppDomain // The exception correctly pulls down the entire worker process so that no AppDomains are // left in memory AppDomain.Unload(domain); } return restartForAssemblyUpdate; }
/// <summary> /// Run the hosted runtime, blocking the calling thread. /// </summary> /// <returns>True if the worker stopped as planned (e.g. due to updated assemblies)</returns> public bool Run() { var settings = new CloudConfigurationSettings { DataConnectionString = RoleEnvironment.GetConfigurationSettingValue("DataConnectionString"), SelfManagementSubscriptionId = RoleEnvironment.GetConfigurationSettingValue("SelfManagementSubscriptionId"), SelfManagementCertificateThumbprint = RoleEnvironment.GetConfigurationSettingValue("SelfManagementCertificateThumbprint") }; // The trick is to load this same assembly in another domain, then // instantiate this same class and invoke Run var domain = AppDomain.CreateDomain("WorkerDomain", null, AppDomain.CurrentDomain.SetupInformation); bool restartForAssemblyUpdate; try { _isolatedInstance = (SingleRuntimeHost)domain.CreateInstanceAndUnwrap( Assembly.GetExecutingAssembly().FullName, typeof(SingleRuntimeHost).FullName); // This never throws, unless something went wrong with IoC setup and that's fine // because it is not possible to execute the worker restartForAssemblyUpdate = _isolatedInstance.Run(settings); } finally { _isolatedInstance = null; // If this throws, it's because something went wrong when unloading the AppDomain // The exception correctly pulls down the entire worker process so that no AppDomains are // left in memory AppDomain.Unload(domain); } return(restartForAssemblyUpdate); }