-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
95 lines (85 loc) · 3.71 KB
/
Copy pathProgram.cs
File metadata and controls
95 lines (85 loc) · 3.71 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
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Threading;
using Micro.Net.Abstractions;
using Micro.Net.Abstractions.Hosting;
using Micro.Net.Core.Configuration;
using Micro.Net.Core.Pipeline;
using Micro.Net.Dispatch;
using Micro.Net.Exceptions;
using Micro.Net.Handling;
using Micro.Net.Receive;
using Micro.Net.Storage.FileSystem;
using Newtonsoft.Json;
using JsonSerializer = Micro.Net.Serializing.JsonSerializer;
using Micro.Net.Transport.Http;
using Micro.Net.Transport.Feather;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
namespace Micro.Net
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
MicroHostConfiguration hostConfig = hostContext.Configuration.GetSection("Host").Get<MicroHostConfiguration>();
Assembly assembly = Assembly.LoadFrom(hostConfig.Assembly);
IEnumerable<Type> types = assembly.GetExportedTypes().Where(t => t.IsAssignableTo(typeof(IMicroserviceConfigurable)));
IMicroserviceConfigurable cfgbl = default;
if (!types.Any())
{
throw new MicroHostException("No configurable types found!", 1401);
}
if (string.IsNullOrWhiteSpace(hostConfig.ConfigClass))
{
if (types.Count() > 1)
{
throw new MicroHostException("More than one configurable type found, but none were specified! Designate a configurable type as 'Host:ConfigClass'.", 1402);
}
else
{
cfgbl = (IMicroserviceConfigurable)Activator.CreateInstance(types.Single());
}
}
else
{
types = types.Where(t =>
t.Name.Equals(hostConfig.ConfigClass) || t.FullName.Equals(hostConfig.ConfigClass) ||
t.AssemblyQualifiedName.Equals(hostConfig.ConfigClass));
if (types.Count() > 1)
{
throw new MicroHostException("Multiple configurable types found matching designated configurable! Use FullName or AssemblyQualifiedName instead.", 1403);
}
else if (!types.Any())
{
throw new MicroHostException($"No configurable types found with name '{hostConfig.ConfigClass}'!", 1404);
}
else
{
cfgbl = (IMicroserviceConfigurable)Activator.CreateInstance(types.Single());
}
}
services.UseMicroNet(mcfg => cfgbl.Configure(mcfg, hostContext.Configuration));
if (services.All(s => s.ServiceType != typeof(ILoggerFactory)))
{
services.AddLogging(conf =>
{
conf.AddConsole();
});
}
});
}
}