/// <summary> /// Adds a package to the manager, enabling it for use. /// </summary> /// <param name="component">The package component.</param> /// <param name="name">Optional name (can be <see langword="null"/>) /// that must match the package name.</param> /// <remarks> /// This method is also called to add to the container services that /// inherit from <see cref="IComponent"/>, so the type of the component /// is not enforced. Just special logic is applied to package component types. /// </remarks> public override void Add(IComponent component, string name) { if (component == null) { throw new ArgumentNullException("component"); } if (component is GuidancePackage) { GuidancePackage package = (GuidancePackage)component; if (name != null && name != package.Configuration.Name) { throw new ArgumentException(String.Format( System.Globalization.CultureInfo.CurrentCulture, Properties.Resources.RecipeManager_NameDoesntMatch, name, package.Configuration.Caption)); } // If there's a host listening, query for name to see if they match. IHostService host = GetService <IHostService>(); if (host != null && host.HostName != package.Configuration.Host) { throw new ArgumentException(String.Format( System.Globalization.CultureInfo.CurrentCulture, Properties.Resources.RecipeManager_PackageHostMismatch, package.Configuration.Caption, package.Configuration.Host, host.HostName)); } try { // Force the package to be sited at Enabling event time also. base.Add(component, package.Configuration.Name); bool executebinding = true; if (EnablingPackage != null) { CancelPackageEventArgs args = new CancelPackageEventArgs( package, false); EnablingPackage(this, args); if (args.Cancel) { base.Remove(component); return; } executebinding = args.ExecuteBindingRecipe; } if (executebinding && package.Configuration.BindingRecipe != null && package.Configuration.BindingRecipe.Length > 0) { package.TurnOnOutput(); package.Execute(package.Configuration.BindingRecipe); package.TurnOffOutput(); } if (EnabledPackage != null) { EnabledPackage(this, new PackageEventArgs(package)); } } catch { IPersistenceService persistanceService = package.GetService <IPersistenceService>(true); // Notify host that we're disabling. if (DisablingPackage != null) { DisablingPackage(this, new CancelPackageEventArgs(package, false)); } // Don't let the package be enabled if an exception happened. base.Remove(component); if (DisabledPackage != null) { DisabledPackage(this, new PackageEventArgs(package)); } persistanceService.ClearState(package.Configuration.Name); throw; } } else { // Just add the component so it gets sited. // Services that implement IComponent will arrive here. base.Add(component, name); } }