public object Create(object parent, object configContext, System.Xml.XmlNode section) { if (section == null) { throw new InvalidOperationException("Can't find configuration section"); } SectionName = section.LocalName; XmlNodeList classifications = section.SelectNodes("./*[local-name() = 'messageHandler']"); XmlNodeList actions = section.SelectNodes("./*[local-name() = 'action']"); Receiver = new Dictionary <string, IMessageReceiver>(); Actions = new Dictionary <string, string>(); // Load all assemblies this app needs foreach (XmlNode nd in section.SelectNodes("./*[local-name() = 'assembly']")) { if (nd.Attributes["name"] == null) { throw new Exception("You must specify a 'name' attribute when using the 'assembly' element"); } // Load the assembly Assembly.Load(new AssemblyName(nd.Attributes["name"].Value)); } // XmlNode if (actions != null) { foreach (XmlNode xn in actions) { if (xn.Attributes["type"] == null || xn.Attributes["action"] == null) { throw new Exception("You must specify both the 'type' and 'action' to the action element"); } string action = xn.Attributes["action"].Value, type = xn.Attributes["type"].Value; Actions.Add(type, action); } } // XmlNode if (classifications != null) { foreach (XmlNode xn in classifications) { if (xn.Attributes["type"] == null || xn.Attributes["classifier"] == null) { throw new Exception("You must specify both the 'classifier' and 'type' to the messageHandler element"); } // Find the assembly that contains the receiver Type handlerType = Type.GetType(xn.Attributes["type"].Value); // Was the type found quickly? If not, do a slower scan if (handlerType == null) { Assembly tasm = Array.Find <Assembly>(AppDomain.CurrentDomain.GetAssemblies(), a => a.GetType(xn.Attributes["type"].Value) != null); if (tasm != null) { handlerType = tasm.GetType(xn.Attributes["type"].Value); } else { throw new ConfigurationErrorsException(string.Format("Can't find classification handler '{0}'", xn.Attributes["type"].Value)); } } // Find a type in the app domain that matches the type specified IMessageReceiver receiver = (IMessageReceiver)handlerType.Assembly.CreateInstance(handlerType.FullName); Receiver.Add(xn.Attributes["classifier"].Value, receiver); } } // Process Aides List <IStructureFormatter> aides = new List <IStructureFormatter>(); if (section.SelectNodes("./@aide") != null) { foreach (XmlNode nd in section.SelectNodes("./@aide")) { Type t = Type.GetType(nd.Value); if (t == null) { throw new ConfigurationErrorsException(String.Format("Cannot find the type '{0}'", nd.Value)); } // Get ctor ConstructorInfo ci = t.GetConstructor(Type.EmptyTypes); if (ci == null) { throw new ConfigurationErrorsException(String.Format("Cannot find a parameterless constructor on '{0}'", nd.Value)); } // Construct and cast IStructureFormatter isf = ci.Invoke(null) as IStructureFormatter; if (isf == null) { throw new ConfigurationErrorsException(String.Format("The type '{0}' does not implement IStructureFormatter", nd.Value)); } aides.Add(isf); } } // Add formatter XmlNode formatter = section.SelectSingleNode("./@formatter"); if (formatter == null) { throw new Exception("You must supply the 'formatter' attribute to the WcfConnector configuration element"); } //Assembly asmFindb = Array.Find<Assembly>(AppDomain.CurrentDomain.GetAssemblies(), a => a.GetType(formatter.Value) != null); // Get the formatter type Type fmtType = Type.GetType(formatter.Value); Formatter = (IStructureFormatter)fmtType.Assembly.CreateInstance(fmtType.FullName); if (!(Formatter is IXmlStructureFormatter)) { throw new ArgumentException("The formatter supplied MUST be implement IXmlStructureFormatter"); } // Assign aides Formatter.GraphAides = aides; return(this); }
public bool Start() { this.Starting?.Invoke(this, EventArgs.Empty); // Start each of the listeners for each of the ITS(es) foreach (var itsConfig in m_configuration.Revisions) { ConstructorInfo ciFormatter = itsConfig.Formatter.GetConstructor(Type.EmptyTypes); if (ciFormatter == null) { Trace.TraceError("Cannot create listener for {0} as the formatter has no default constructor", itsConfig.Formatter.FullName); continue; } // Now we want to create the formatter IStructureFormatter formatter = ciFormatter.Invoke(null) as IStructureFormatter; if (itsConfig.GraphAide != null) { ciFormatter = itsConfig.GraphAide.GetConstructor(Type.EmptyTypes); if (ciFormatter == null) { Trace.TraceError("Cannot create listener for {0} as the formatter has no default constructor", itsConfig.GraphAide.FullName); continue; } var aide = ciFormatter.Invoke(null) as IStructureFormatter; if (aide is IValidatingStructureFormatter) { (aide as IValidatingStructureFormatter).ValidateConformance = itsConfig.ValidateInstances; } formatter.GraphAides.Add(aide); } // Build the type cache if (formatter is ICodeDomStructureFormatter && itsConfig.CacheTypes.Count > 0) { Trace.TraceInformation("Creating type cache for {0} ({1} types)", itsConfig.Formatter, itsConfig.CacheTypes.Count); (formatter as ICodeDomStructureFormatter).BuildCache(itsConfig.CacheTypes.ToArray()); Trace.TraceInformation("{0} CLR types created", (formatter as ICodeDomStructureFormatter).GeneratedAssemblies[0].GetTypes().Length); } if (formatter is IValidatingStructureFormatter) { (formatter as IValidatingStructureFormatter).ValidateConformance = itsConfig.ValidateInstances; } // Iterate through listeners and start em up foreach (var listenerConfig in itsConfig.Listeners) { ConstructorInfo ciListener = listenerConfig.ConnectorType.GetConstructor(Type.EmptyTypes); if (ciListener == null) // sanity checks { Trace.TraceError("Cannot create listener for {0} as the connector {1} has no default constructor", itsConfig.Formatter.FullName, listenerConfig.ConnectorType.FullName); continue; } else if (listenerConfig.ConnectorType.GetInterface(typeof(IFormattedConnector).FullName) == null) { Trace.TraceError("Cannot append formatter {0} to connector {1} as the connector doesn't implement IFormattedConnector", itsConfig.Formatter.FullName, listenerConfig.ConnectorType.FullName); continue; } else if (listenerConfig.ConnectorType.GetInterface(typeof(IListenWaitConnector).FullName) == null) { Trace.TraceError("Cannot append formatter {0} to connector {1} as the connector doesn't implement IListenWaitConnector", itsConfig.Formatter.FullName, listenerConfig.ConnectorType.FullName); continue; } // Now create the connector IListenWaitConnector connector = ciListener.Invoke(null) as IListenWaitConnector; try { // Set the connector up (connector as IFormattedConnector).Formatter = formatter; connector.ConnectionString = listenerConfig.ConnectionString; connector.MessageAvailable += new EventHandler <UnsolicitedDataEventArgs>(connector_MessageAvailable); Trace.TraceInformation("Starting listener {0}", connector.ConnectionString); connector.Open(); connector.Start(); m_activeConnectors.Add(connector); } catch (ConnectorException e) { Trace.TraceError("Could not start listener {0}", connector.ConnectionString); Trace.Indent(); Trace.WriteLine(e.Message); Trace.Indent(); foreach (var detail in e.Details ?? new IResultDetail[0]) { Trace.WriteLine(String.Format("{0}: {1}", detail.Type, detail.Message)); } Trace.Unindent(); Trace.Unindent(); } catch (System.Exception e) { Trace.TraceError("Could not start listener {0}", connector.ConnectionString); Trace.Indent(); Trace.WriteLine(e.ToString()); Trace.Unindent(); } } } if (this.m_activeConnectors.Count > 0) { this.Started?.Invoke(this, EventArgs.Empty); } return(m_activeConnectors.Count > 0); }