-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProgram.cs
More file actions
123 lines (100 loc) · 4.58 KB
/
Program.cs
File metadata and controls
123 lines (100 loc) · 4.58 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
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Orleans;
using System.Linq;
using Microsoft.Azure.WebJobs;
using System.Collections.Generic;
using System.Reflection;
using System;
using System.Diagnostics;
namespace Frontend
{
public class Program
{
public static Task Main(string[] args)
{
var externalAssemblies = ActorClientService.ExternalAssemblies;
var builder = Host.CreateDefaultBuilder(args);
ConfigureOrleansWeb(builder);
builder.ConfigureWebJobs(webJobsBuilder =>
{
webJobsBuilder.AddAzureStorageCoreServices();
webJobsBuilder.AddAzureStorage();
InjectWebJobTypeLocator(webJobsBuilder, externalAssemblies);
});
builder.ConfigureLogging((context, loggingBuilder) =>
{
loggingBuilder.AddConsole();
});
builder.ConfigureServices(services =>
{
var mvcBuilder = services.AddControllers();
foreach(var assembly in externalAssemblies)
{
mvcBuilder.AddApplicationPart(assembly);
}
});
return builder.RunConsoleAsync();
}
private static void ConfigureOrleansWeb(IHostBuilder builder)
{
builder.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder
.ConfigureServices(services =>
{
services.AddSingleton<ActorClientService>();
services.AddSingleton<IHostedService>(sp => sp.GetRequiredService<ActorClientService>());
services.AddSingleton<IClusterClient>(sp => sp.GetRequiredService<ActorClientService>().Client);
services.AddSingleton<IGrainFactory>(sp => sp.GetRequiredService<IClusterClient>());
services.AddServicesForSelfHostedDashboard();
})
.Configure((ctx, app) =>
{
if (ctx.HostingEnvironment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseOrleansDashboard(new OrleansDashboard.DashboardOptions { BasePath = "/dashboard" });
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
});
});
}
private static void InjectWebJobTypeLocator(IWebJobsBuilder webJobsBuilder,
IEnumerable<Assembly> externalAssemblies)
{
Debug.Assert(webJobsBuilder != null);
Debug.Assert(externalAssemblies != null);
var locatorType = typeof(ITypeLocator).FullName;
Debug.Assert(!string.IsNullOrWhiteSpace(locatorType));
var type = externalAssemblies.SelectMany(a => a.GetTypes())
.SingleOrDefault(t => t.IsPublic && t.GetInterface(locatorType) != null);
if (type == null)
{
throw new InvalidOperationException("Unable to resolve single public implementation of type: " + locatorType);
}
var locator = (ITypeLocator) Activator.CreateInstance(type);
if (locator == null)
{
throw new InvalidOperationException("Unable to create instance of locator type: " + locatorType);
}
Debug.Assert(webJobsBuilder.Services != null);
var existingTypeResolverDescriptor = webJobsBuilder
.Services
.Where(d => d.ServiceType == typeof(ITypeLocator))
.ToArray();
Array.ForEach(existingTypeResolverDescriptor, d => webJobsBuilder.Services.Remove(d));
webJobsBuilder.Services.AddSingleton<ITypeLocator>(locator);
}
}
}