forked from yhuse/FlowSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bootstrap.cs
77 lines (67 loc) · 2.78 KB
/
Bootstrap.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using Clifton.Core.Assertions;
using Clifton.Core.ExtensionMethods;
using Clifton.Core.Semantics;
using Clifton.Core.ModuleManagement;
using Clifton.Core.ServiceManagement;
namespace FlowSharp
{
static partial class Program
{
public static ServiceManager ServiceManager;
static void Bootstrap(string moduleFilename = "modules.xml")
{
ServiceManager = new ServiceManager();
ServiceManager.RegisterSingleton<IServiceModuleManager, ServiceModuleManager>();
try
{
IModuleManager moduleMgr = (IModuleManager)ServiceManager.Get<IServiceModuleManager>();
List<AssemblyFileName> modules = GetModuleList(XmlFileName.Create(moduleFilename));
moduleMgr.RegisterModules(modules);
List<Exception> exceptions = ServiceManager.FinishSingletonInitialization();
ShowAnyExceptions(exceptions);
}
catch(ReflectionTypeLoadException lex)
{
StringBuilder sb = new StringBuilder();
foreach (Exception ex in lex.LoaderExceptions)
{
sb.AppendLine(ex.Message);
}
MessageBox.Show(sb.ToString(), "Initialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace, "Initialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// Return the list of assembly names specified in the XML file so that
/// we know what assemblies are considered modules as part of the application.
/// </summary>
static private List<AssemblyFileName> GetModuleList(XmlFileName filename)
{
Assert.That(File.Exists(filename.Value), "Module definition file " + filename.Value + " does not exist.");
XDocument xdoc = XDocument.Load(filename.Value);
return GetModuleList(xdoc);
}
/// <summary>
/// Returns the list of modules specified in the XML document so we know what
/// modules to instantiate.
/// </summary>
static private List<AssemblyFileName> GetModuleList(XDocument xdoc)
{
List<AssemblyFileName> assemblies = new List<AssemblyFileName>();
(from module in xdoc.Element("Modules").Elements("Module")
select module.Attribute("AssemblyName").Value).ForEach(s => assemblies.Add(AssemblyFileName.Create(s)));
return assemblies;
}
}
}