public static CompositionError InvalidStateForRecompposition(ComposablePart part) { return CompositionError.Create( CompositionErrorId.ImportEngine_InvalidStateForRecomposition, Strings.ImportEngine_InvalidStateForRecomposition, part.ToElement().DisplayName); }
protected WebScopedComposablePart(ComposablePart composablePart) { _composablePart = composablePart; _exportDef = composablePart.ExportDefinitions.First(); _key = ((string)_exportDef.Metadata["ExportTypeIdentity"]).Replace('.', '_'); }
public void SatisfyImportsOnce(ComposablePart part) { if (this.DoNothingOnSatisfyImportsOnce) { return; } CompositionBatch batch = new CompositionBatch(); // We only want to include the standard exports and parts to compose in the first composition if (!this.alreadyComposed) { foreach (object instance in this.PartsToCompose) { batch.AddPart(instance); } foreach (Export export in this.ExportsToCompose) { batch.AddExport(export); } } if (part != null) { batch.AddPart(part); } this.container.Compose(batch); this.alreadyComposed = true; }
public static CompositionException CreateCannotGetExportedValue(ComposablePart part, ExportDefinition definition, Exception innerException) { Assumes.NotNull(part, definition, innerException); return new CompositionException( ErrorBuilder.CreateCannotGetExportedValue(part, definition, innerException)); }
public DisposableWrapperPart(ComposablePart innerPart) { if (innerPart == null) throw new ArgumentNullException(nameof(innerPart)); InnerPart = innerPart; }
/// <summary> /// Initializes a new instance of the <see cref="ProxyPart"/> class. /// </summary> /// <param name="registration">The registration</param> /// <param name="innerPart">The inner part.</param> public ProxyPart(ComponentRegistrationBase registration, ComposablePart innerPart) { implementation = GetImplementation(registration); this.registration = registration; this.innerPart = innerPart; DeterminePropertiesToInject(); }
public static CompositionError PreventedByExistingImport(ComposablePart part, ImportDefinition import) { return CompositionError.Create( CompositionErrorId.ImportEngine_PreventedByExistingImport, Strings.ImportEngine_PreventedByExistingImport, import.ToElement().DisplayName, part.ToElement().DisplayName); }
/// <summary> /// Removes a ComposablePart from the composition</summary> /// <param name="composer">IComposer</param> /// <param name="composablePart">ComposablePart to remove from the compositoi</param> public static void RemovePart(this IComposer composer, ComposablePart composablePart) { Requires.NotNull(composer, "composer"); Requires.NotNull(composablePart, "composablePart"); var batch = new CompositionBatch(Enumerable.Empty<ComposablePart>(), new ComposablePart[] { composablePart }); composer.Container.Compose(batch); }
protected override object GetExportedValueCore() { ComposablePart part = this._catalogExportProvider.GetComposablePart(this._partDefinition, this.IsSharedPart); object exportedValue = this._catalogExportProvider.GetExportedValue(part, this._definition, this.IsSharedPart); this._part = part; return exportedValue; }
private static void ComposeBatchTwo(CompositionContainer container) { var batch = new CompositionBatch(); batch.RemovePart(a); c = batch.AddPart(new PluginC()); batch.AddPart(logger); container.Compose(batch); }
public InterceptingComposablePart(ComposablePart interceptedPart, IExportedValueInterceptor valueInterceptor) { interceptedPart.ShouldNotBeNull("interceptedPart"); valueInterceptor.ShouldNotBeNull("valueInterceptor"); InterceptedPart = interceptedPart; _valueInterceptor = valueInterceptor; _values = new Dictionary<ExportDefinition, object>(); }
public InterceptingComposablePart(ComposablePart interceptedPart, IExportedValueInterceptor valueInterceptor) { if (interceptedPart == null) throw new ArgumentNullException("interceptedPart"); if (valueInterceptor == null) throw new ArgumentNullException("valueInterceptor"); InterceptedPart = interceptedPart; this.valueInterceptor = valueInterceptor; this.values = new Dictionary<ExportDefinition, object>(); }
private static void ComposeBatchOne(CompositionContainer container) { var batch = new CompositionBatch(); a = batch.AddPart(new PluginA()); b = batch.AddPart(new PluginB()); batch.AddPart(bootstrapper); container.Compose(batch); }
public static CompositionError CreatePartCannotActivate(ComposablePart part, Exception innerException) { Assumes.NotNull(part, innerException); ICompositionElement element = part.ToElement(); return CompositionError.Create( CompositionErrorId.ImportEngine_PartCannotActivate, element, innerException, Strings.ImportEngine_PartCannotActivate, element.DisplayName); }
/// <summary> /// Previews all the required imports for the given <see cref="ComposablePart"/> to /// ensure they can all be satisified. The preview does not actually set the imports /// only ensures that they exist in the source provider. If the preview succeeds then /// the <see cref="ImportEngine"/> also enforces that changes to exports in the source /// provider will not break any of the required imports. If this enforcement needs to be /// lifted for this part then <see cref="ReleaseImports"/> needs to be called for this /// <see cref="ComposablePart"/>. /// </summary> /// <param name="part"> /// The <see cref="ComposablePart"/> to preview the required imports. /// </param> /// <param name="atomicComposition"></param> /// <exception cref="CompositionException"> /// An error occurred during previewing and <paramref name="atomicComposition"/> is null. /// <see cref="CompositionException.Errors"/> will contain a collection of errors that occurred. /// The pre-existing composition is in an unknown state, depending on the errors that occured. /// </exception> /// <exception cref="ChangeRejectedException"> /// An error occurred during the previewing and <paramref name="atomicComposition"/> is not null. /// <see cref="CompositionException.Errors"/> will contain a collection of errors that occurred. /// The pre-existing composition remains in valid state. /// </exception> /// <exception cref="ObjectDisposedException"> /// The <see cref="ImportEngine"/> has been disposed of. /// </exception> public void PreviewImports(ComposablePart part, AtomicComposition atomicComposition) { this.ThrowIfDisposed(); Requires.NotNull(part, "part"); // Do not do any previewing if SilentRejection is disabled. if (this._compositionOptions.HasFlag(CompositionOptions.DisableSilentRejection)) { return; } // NOTE : this is a very intricate area threading-wise, please use caution when changing, otherwise state corruption or deadlocks will ensue // The gist of what we are doing is as follows: // We need to lock the composition, as we will proceed modifying our internal state. The tricky part is when we release the lock // Due to the fact that some actions will take place AFTER we leave this method, we need to KEEP THAT LOCK HELD until the transation is commiited or rolled back // This is the reason we CAN'T use "using here. // Instead, if the transaction is present we will queue up the release of the lock, otherwise we will release it when we exit this method // We add the "release" lock to BOTH Commit and Revert queues, because they are mutually exclusive, and we need to release the lock regardless. // This will take the lock, if necesary IDisposable compositionLockHolder = this._lock.IsThreadSafe ? this._lock.LockComposition() : null; bool compositionLockTaken = (compositionLockHolder != null); try { // revert actions are processed in the reverse order, so we have to add the "release lock" action now if (compositionLockTaken && (atomicComposition != null)) { atomicComposition.AddRevertAction(() => compositionLockHolder.Dispose()); } var partManager = GetPartManager(part, true); var result = TryPreviewImportsStateMachine(partManager, part, atomicComposition); result.ThrowOnErrors(atomicComposition); StartSatisfyingImports(partManager, atomicComposition); // Add the "release lock" to the commit actions if (compositionLockTaken && (atomicComposition != null)) { atomicComposition.AddCompleteAction(() => compositionLockHolder.Dispose()); } } finally { // We haven't updated the queues, so we can release the lock now if (compositionLockTaken && (atomicComposition == null)) { compositionLockHolder.Dispose(); } } }
public static CompositionError CreateCannotGetExportedValue(ComposablePart part, ExportDefinition definition, Exception innerException) { Assumes.NotNull(part, definition, innerException); ICompositionElement element = definition.ToElement(); return CompositionError.Create( CompositionErrorId.ImportEngine_PartCannotGetExportedValue, element, innerException, Strings.ImportEngine_PartCannotGetExportedValue, element.DisplayName, part.ToElement().DisplayName); }
public static CompositionContainer CreateAttributed(params object[] parts) { var container = new CompositionContainer(); var partsArray = new ComposablePart[parts.Length]; for (int i = 0; i < parts.Length; i++) { Assert.IsNotInstanceOfType(parts[i], typeof(Type), "You should be using CreateWithAttributedCatalog not CreateAttributed"); partsArray[i] = PartFactory.CreateAttributed(parts[i]); } return Create(partsArray); }
public void CreateGraph() { // Make sure VS has loaded before using MEF (prevents native access violations!) VSHost.EnsureSolution(@"LessDependencies\LessDependencies.sln"); // Don't create the instance using MEF, to ensure that I get a fresh graph for each test. graph = new LessDependencyGraph(WebEditor.ExportProvider.GetExport<IFileExtensionRegistryService>().Value); graph.IsEnabled = true; // Add the instance to the MEF catalog so that its IFileSaveListener is picked up graphPart = AttributedModelServices.CreatePart(graph); var cc = (CompositionContainer)WebEditor.CompositionService; cc.Compose(new CompositionBatch(new[] { graphPart }, null)); }
/// <summary> /// Will satisfy the imports on a part based on a <see cref="CompositionContainer"/> /// registered with the <see cref="CompositionHost"/>. By default if no <see cref="CompositionContainer"/> /// is registered the first time this is called it will be initialized to a catalog /// that contains all the assemblies loaded by the initial application XAP. /// </summary> /// <param name="part"> /// Part with imports that need to be satisfied. /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="instance"/> is <see langword="null"/>. /// </exception> /// <exception cref="ArgumentException"> /// <paramref name="instance"/> contains <see cref="ExportAttribute"/>s applied on its type. /// </exception> /// <exception cref="ChangeRejectedException"> /// One or more of the imports on the object instance could not be satisfied. /// </exception> /// <exception cref="CompositionException"> /// One or more of the imports on the object instance caused an error while composing. /// </exception> public static void SatisfyImports(ComposablePart part) { if (part == null) { throw new ArgumentNullException("part"); } var batch = new CompositionBatch(); batch.AddPart(part); if (part.ExportDefinitions.Any()) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.ArgumentException_TypeHasExports, part.ToString()), "part"); } CompositionContainer container = null; // Ignoring return value because we don't need to know if we created it or not CompositionHost.TryGetOrCreateContainer(_createContainer, out container); container.Compose(batch); }
private static CompositionResult<IEnumerable<Export>> TryGetExports(ExportProvider provider, ComposablePart part, ImportDefinition definition, AtomicComposition atomicComposition) { try { var exports = provider.GetExports(definition, atomicComposition).AsArray(); return new CompositionResult<IEnumerable<Export>>(exports); } catch (ImportCardinalityMismatchException ex) { // Either not enough or too many exports that match the definition CompositionException exception = new CompositionException(ErrorBuilder.CreateImportCardinalityMismatch(ex, definition)); return new CompositionResult<IEnumerable<Export>>( ErrorBuilder.CreatePartCannotSetImport(part, definition, exception)); } }
public StatusBarProgressContext( EventHandler<ProgressCompleteEventArgs> progressComplete, ComposablePart statusItemPart) { ProgressComplete = progressComplete; StatusItemPart = statusItemPart; }
public void SatisfyImportsOnce(ComposablePart part) { Requires.NotNull(part, "part"); Assumes.NotNull(this._compositionContainer); this._compositionContainer.SatisfyImportsOnce(part); }
public PartManager(ImportEngine importEngine, ComposablePart part) { this._importEngine = importEngine; this._part = part; }
/// <summary> /// Removes the specified part from the <see cref="CompositionBatch"/>. /// </summary> /// <param name="part"> /// The part. /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="part"/> is <see langword="null"/>. /// </exception> public void RemovePart(ComposablePart part) { Requires.NotNull(part, "part"); lock (this._lock) { if (this._copyNeededForRemove) { this._partsToRemove = new List<ComposablePart>(this._partsToRemove); this._readOnlyPartsToRemove = this._partsToRemove.AsReadOnly(); this._copyNeededForRemove = false; } this._partsToRemove.Add(part); } }
public static ICompositionElement ToElement(this ComposablePart part) { return(ToElementCore(part)); }
public static IEnumerable<ImportDefinition> GetAffectedImports(ComposablePart part, IEnumerable<ExportDefinition> changedExports) { return part.ImportDefinitions.Where(import => IsAffectedImport(import, changedExports)); }
/// <summary> /// Adds the specified part to the <see cref="CompositionBatch"/>. /// </summary> /// <param name="part"> /// The part. /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="part"/> is <see langword="null"/>. /// </exception> public void AddPart(ComposablePart part) { Requires.NotNull(part, "part"); lock (this._lock) { if (this._copyNeededForAdd) { this._partsToAdd = new List<ComposablePart>(this._partsToAdd); this._readOnlyPartsToAdd = this._partsToAdd.AsReadOnly(); this._copyNeededForAdd = false; } this._partsToAdd.Add(part); } }
public void RemovePart(System.ComponentModel.Composition.Primitives.ComposablePart part) { }
internal WebRequestScopedComposablePart(ComposablePart composablePart) : base(composablePart) { }
private PartManager GetPartManager(ComposablePart part, bool createIfNotpresent) { PartManager partManager = null; using (this._lock.LockStateForRead()) { if (this._partManagers.TryGetValue(part, out partManager)) { return partManager; } } if (createIfNotpresent) { using (this._lock.LockStateForWrite()) { if (!this._partManagers.TryGetValue(part, out partManager)) { partManager = new PartManager(this, part); this._partManagers.Add(part, partManager); } } } return partManager; }
public void SatisfyImportsOnce(ComposablePart part) { this._container.SatisfyImportsOnce(part); }
public static ComposablePartDefinition Create(ComposablePart part) { return Create(part.Metadata, () => part, part.ImportDefinitions, part.ExportDefinitions); }