public static void ApplyCompositionInfo(CompositionInfo info, XmlProcessingContext xmlProcessingContext) { if (info.CommandInfos == null) { return; } foreach (var commandInfo in info.CommandInfos) { if (!Runners.ContainsKey(commandInfo.GetType())) { throw new CompositionException("Provided type is not supported for applying to a component context: " + commandInfo.GetType().FullName); } var runner = Runners[commandInfo.GetType()]; try { xmlProcessingContext.EnterRunningLocation("Processing '" + commandInfo + "'"); runner(commandInfo, xmlProcessingContext); xmlProcessingContext.LeaveRunningLocation(); } catch (Exception e) { // Ignore the exception if the "IgnoreOnError" property is set. // If not, just report the exception (in case it happened in an // include command) and let it bubble up. if (!commandInfo.IgnoreOnError) { // TODO: Fix logging / tracing mechanism xmlProcessingContext.ReportError("Exception: " + e.Message); throw; } // If exception has occured, then the "LeaveRunningLocation" // method is not executed in the "try" block, so call it here. xmlProcessingContext.LeaveRunningLocation(); } } }
private static void RunRegisterComponent(CompositionCommandInfo info, XmlProcessingContext xmlProcessingContext) { if (!(info is RegisterComponentInfo registerComponentInfo)) { throw new ArgumentException("Invalid runner input type: error in static setup."); } // Extract and load contract type Type contractType = null; if (registerComponentInfo.ContractType != null) { contractType = SimpleTypeParserUtil.ParseType(registerComponentInfo.ContractType, xmlProcessingContext); if (contractType == null) { xmlProcessingContext.ReportError( $"Type '{registerComponentInfo.ContractType}' could not be loaded."); return; } } // Get contract name var contractName = registerComponentInfo.ContractName; // Build ComponentConfiguration var componentType = SimpleTypeParserUtil.ParseType(registerComponentInfo.Type, xmlProcessingContext); if (componentType == null) { xmlProcessingContext.ReportError($"Type '{registerComponentInfo.Type}' could not be loaded."); return; } IComponentFactory componentFactory; List <InitializationPointSpecification> initializationPoints; if ((componentType.IsGenericType) && (componentType.ContainsGenericParameters)) { var genericLocalComponentFactory = new GenericLocalComponentFactory(componentType); componentFactory = genericLocalComponentFactory; initializationPoints = genericLocalComponentFactory.InitializationPoints; } else { var localComponentFactory = new LocalComponentFactory(componentType); componentFactory = localComponentFactory; initializationPoints = localComponentFactory.InitializationPoints; } // Add each configured plug, into the InitializationPoints // in the component configuration. foreach (var plugInfo in registerComponentInfo.Plugs) { xmlProcessingContext.EnterRunningLocation($"Plug '{plugInfo.Name}'"); var plugRefType = SimpleTypeParserUtil.ParseType(plugInfo.RefType, xmlProcessingContext); if (plugRefType == null) { xmlProcessingContext.ReportError($"Type '{plugInfo.RefType}' could not be loaded."); xmlProcessingContext.LeaveRunningLocation(); return; } var plugRefName = plugInfo.RefName; initializationPoints.Add(new InitializationPointSpecification( plugInfo.Name, MemberTypes.All, true, new ComponentQuery(plugRefType, plugRefName))); // TODO: Add support for optional plugs in Composition XML xmlProcessingContext.LeaveRunningLocation(); } // Add each configuration point, into the InitializationPoints // in the component configuration. foreach (var configurationPointInfo in registerComponentInfo.ConfigurationPoints) { xmlProcessingContext.EnterRunningLocation($"ConfigurationPoint '{configurationPointInfo.Name}'"); var value = CreateLazyXmlValue(configurationPointInfo.XElements, configurationPointInfo.XAttributes, xmlProcessingContext); initializationPoints.Add(new InitializationPointSpecification( configurationPointInfo.Name, MemberTypes.All, true, new LazyValueQuery(value))); xmlProcessingContext.LeaveRunningLocation(); } // Register the component into the component context. if (contractType == null) { xmlProcessingContext.ComponentContext.Register(contractName, componentFactory); } else { xmlProcessingContext.ComponentContext.Register(contractType, contractName, componentFactory); } }