-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppBootstrapper.cs
More file actions
131 lines (108 loc) · 3.78 KB
/
AppBootstrapper.cs
File metadata and controls
131 lines (108 loc) · 3.78 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using AzureSBMonitor.Shell;
using Caliburn.Micro;
using Microsoft.Extensions.Configuration;
using Serilog;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
using System.Windows;
using Lamar;
using Microsoft.Extensions.DependencyInjection;
using AzureSBMonitor.Services;
namespace AzureSBMonitor
{
public class AppBootstrapper : BootstrapperBase
{
static Serilog.ILogger Logger => new LoggerConfiguration().WriteTo.File("appstart.log", Serilog.Events.LogEventLevel.Error).CreateLogger();
IContainer serviceProvider;
IConfigurationRoot _configuration;
public AppBootstrapper()
{
BuildConfiguration();
Initialize();
Coroutine.Completed += Coroutine_Completed;
}
private void BuildConfiguration()
{
try
{
var cnf = new ConfigurationBuilder();
cnf.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
_configuration = cnf.Build();
}
catch (Exception ex)
{
Logger.Error(ex, ex.Message);
throw;
}
}
private async void Coroutine_Completed(object sender, ResultCompletionEventArgs e)
{
if (e.Error != null)
{
var dlg = serviceProvider.GetService<IDialogService>();
await dlg.ShowError(e.Error);
}
}
protected override async void OnUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
var dlg = serviceProvider.GetService<IDialogService>();
await dlg.ShowError(e.Exception);
base.OnUnhandledException(sender, e);
}
protected override void Configure()
{
try
{
serviceProvider = new Container(_ =>
{
_.IncludeRegistry<SbMonitorRegistery>();
_.AddSingleton<IConfiguration>(_configuration);
_.AddLogging(_configuration);
_.AddAppServices();
_.AddCaliburn();
});
}
catch (Exception ex)
{
Logger.Error(ex, ex.Message);
throw;
}
//LogManager.GetLog = t => new SerilogLog(serviceProvider.GetService<ILoggerFactory>().CreateLogger(typeof(AppBootstrapper)));
}
protected override object GetInstance(Type service, string key)
{
if (string.IsNullOrEmpty(key))
return serviceProvider.GetInstance(service);
else
return serviceProvider.GetInstance(service, key);
}
protected override IEnumerable<object> GetAllInstances(Type service)
{
return serviceProvider.GetAllInstances(service).OfType<object>();
}
protected override void BuildUp(object instance)
{
(serviceProvider as Container).BuildUp(instance);
}
protected override void OnStartup(object sender, StartupEventArgs e)
{
dynamic set = new ExpandoObject();
set.ResizeMode = ResizeMode.CanResize;
set.SizeToContent = SizeToContent.Manual;
set.Title = "Azure service bus queue and topics";
DisplayRootViewForAsync<ShellViewModel>(set);
}
protected override IEnumerable<Assembly> SelectAssemblies()
{
return new[] { Assembly.GetExecutingAssembly() };
}
}
}