/// <summary> /// Initialize the aggregator host by loading the appropriate assembly as specified by the config /// </summary> /// <param name="config">Configuration string</param> public void InitializeHost(string config) { MASShared masShared = new MASShared(); string aggregatorType = masShared.GetAggregatorTypeFromConfig(config); magsource = masShared.GetMASAssembly(aggregatorType); if (magsource == null) { RaiseAggregatorHostError(string.Format("Mail aggregator with type '{0}' could not be found!", aggregatorType)); } else { magsource.AggregatorError += new RaiseMessageDelegate(magsource_AggregatorError); magsource.AggregatorWarning += new RaiseMessageDelegate(magsource_AggregatorWarning); if (Properties.Settings.Default.MessageBodyTemplate != null || Properties.Settings.Default.MessageBodyTemplate.Length > 0) { magsource.MessageBodyTemplate = Properties.Settings.Default.MessageBodyTemplate; } if (Properties.Settings.Default.MessageSeparatorTemplate != null || Properties.Settings.Default.MessageSeparatorTemplate.Length > 0) { magsource.MessageSeparatorTemplate = Properties.Settings.Default.MessageSeparatorTemplate; } if (!magsource.SetConfig(config)) { RaiseAggregatorHostError(string.Format("An error occured while configuring the aggregator with config {0}\r\n(1}", config, magsource.LastError)); magsource = null; } } }
static void Main(string[] args) { MASShared masShared = new MASShared(); foreach (var sourceConfig in Properties.Settings.Default.AggrSources) { string aggregatorType = masShared.GetAggregatorTypeFromConfig(sourceConfig); if (aggregatorType.Length > 0) { IMailAggregatorSource magsource = masShared.GetMASAssembly(aggregatorType); if (magsource == null) { Console.WriteLine("Mail aggregator with type '{0}' could not be found!", aggregatorType); } else { magsource.AggregatorError += new RaiseMessageDelegate(magsource_AggregatorError); magsource.SetConfig(sourceConfig); foreach (MailAggregatedMessage msg in magsource.GetMessages()) { Console.WriteLine("To:" + msg.ToAddress); Console.WriteLine("Subject:" + msg.Subject + (msg.MsgRepeatCounter > 1 ? " (" + msg.MsgRepeatCounter.ToString() + ")" : "")); Console.WriteLine(msg.Body.Length); Console.WriteLine(">---------------"); } } } } //foreach (var sourceConfig in Properties.Settings.Default.AggrSources) //{ // IMailAggregatorSource magsource = null; // if (sourceConfig.Contains("FlatFile")) // { // magsource = new MailAggregatorFFSource(); // magsource.AggregatorError += new RaiseMessageDelegate(magsource_AggregatorError); // } // if (magsource != null) // { // magsource.SetConfig(sourceConfig); // foreach (MailAggregatedMessage msg in magsource.GetMessages()) // { // Console.WriteLine("To:" + msg.ToAddress); // Console.WriteLine("Subject:" + msg.Subject + (msg.MsgRepeatCounter > 1 ? " (" + msg.MsgRepeatCounter.ToString() + ")" : "")); // Console.WriteLine(msg.Body.Length); // Console.WriteLine(">---------------"); // } // } //} Console.WriteLine("Done"); Console.ReadKey(); }
/// <summary> /// Look for the aggregator assembly in the local directory /// </summary> /// <param name="aggregatorName">Name (class) of the aggregator</param> /// <returns>An IMailAggregatorSource aggregator instance</returns> public IMailAggregatorSource GetMASAssembly(string aggregatorName) { string agentsAssemblyPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); StringBuilder sbExceptions = new StringBuilder(); try { foreach (string assemblyPath in System.IO.Directory.GetFiles(agentsAssemblyPath, "*.dll")) { try { foreach (string className in LoadAggregatorClasses(assemblyPath)) { string assemblyAggregatorName = className.Replace("MailAggregator.", ""); if (assemblyAggregatorName.ToUpper().StartsWith(aggregatorName.ToUpper())) { Assembly aggregatorAssembly = Assembly.LoadFile(assemblyPath); IMailAggregatorSource newaggregator = (IMailAggregatorSource)aggregatorAssembly.CreateInstance(className); return(newaggregator); } } } catch (System.Reflection.ReflectionTypeLoadException rex) { foreach (Exception lex in rex.LoaderExceptions) { sbExceptions.AppendLine(string.Format("Error in assembly '{0}' - {1}", assemblyPath, lex.Message)); } } catch (Exception innerEx) { throw new Exception(string.Format("Error loading {0}.\r\n{1}", assemblyPath, innerEx.Message)); } } } catch (Exception ex) { throw new Exception(string.Format("An error occured loading aggregators from {0}.\r\n{1}", agentsAssemblyPath, ex.Message)); } if (sbExceptions.Length > 0) { throw new Exception(string.Format("There were errors loading the assembly(s)\r\n{0}", sbExceptions.ToString())); } return(null); }