-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServiceContainerSetup.cs
92 lines (77 loc) · 3.24 KB
/
ServiceContainerSetup.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using CommonServiceLocator;
using ReactiveHMI.M2MCommunication.Core.CommonTypes;
using ReactiveHMI.M2MCommunication.Core.Interfaces;
using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.IO;
using System.Reflection;
namespace ReactiveHMI.M2MCommunication.Services
{
public class ServiceContainerSetup : IDisposable
{
private readonly UaLibrarySettings _uaLibrarySettings;
private readonly ILogger _logger;
private ILoggerContainer loggerContainer;
internal IServiceLocator DisposableServiceLocator { get; private set; }
public ServiceContainerSetup(UaLibrarySettings settings, ILogger logger)
{
_uaLibrarySettings = settings;
_logger = logger;
}
public ServiceContainerSetup Initialise()
{
_logger?.LogInfo("Initialising Managed Extensibility Framework container");
AggregateCatalog AggregateCatalog = new AggregateCatalog(
new DirectoryCatalog(
Path.Combine(Path.GetDirectoryName(Assembly.GetCallingAssembly().Location), _uaLibrarySettings.LibraryDirectory)
)
);
CompositionContainer Container = new CompositionContainer(AggregateCatalog);
_logger?.LogInfo("Composing configuration file name and logger instance");
Container.ComposeExportedValue(UaContractNames.ConfigurationFileNameContract, Path.Combine(
Directory.GetCurrentDirectory(),
_uaLibrarySettings.ResourcesDirectory,
_uaLibrarySettings.LibraryDirectory,
_uaLibrarySettings.ConsumerConfigurationFile)
);
Container.ComposeExportedValue(_logger);
_logger?.LogInfo("Setting a service locator");
DisposableServiceLocator = new UaooiServiceLocator(Container);
ServiceLocator.SetLocatorProvider(() => DisposableServiceLocator);
if (shouldComposeLoggers)
{
_logger?.LogInfo("Composing a common logger for all components");
loggerContainer = Container.GetExportedValue<ILoggerContainer>().EnableLoggers();
}
return this;
}
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
(DisposableServiceLocator as IDisposable)?.Dispose();
(loggerContainer as IDisposable)?.Dispose();
}
DisposableServiceLocator = null;
loggerContainer = null;
ServiceLocator.SetLocatorProvider(() => null);
disposedValue = true;
}
}
// This code added to correctly implement the disposable pattern.
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
}
#endregion
#region Unit test workaround
private readonly bool shouldComposeLoggers = true;
#endregion
}
}