-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathStartup.cs
111 lines (91 loc) · 3.86 KB
/
Startup.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Azure.SignalR.Emulator.Controllers;
using Microsoft.Azure.SignalR.Emulator.HubEmulator;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Microsoft.Azure.SignalR.Emulator
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient();
services.AddAllowAllCors();
services.AddJwtBearerAuth(Configuration);
services.AddAuthorization();
services.AddControllers().AddNewtonsoftJson().ConfigureApplicationPartManager(manager =>
{
manager.FeatureProviders.Add(new CustomControllerFeatureProvider());
});
services.Configure<UpstreamOptions>(Configuration.GetSection("UpstreamSettings"));
services.AddSignalREmulator();
services.AddLogging(services =>
{
services.AddDebug();
services.AddConsole();
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime)
{
lifetime.ApplicationStarted.Register(() =>
{
var address = new Uri(app.ServerFeatures.Get<IServerAddressesFeature>().Addresses.First());
var upstreamOptionMonitor = app.ApplicationServices.GetRequiredService<IOptionsMonitor<UpstreamOptions>>();
upstreamOptionMonitor.OnChange(s =>
{
s.Print();
});
Console.WriteLine(@$"
===================================================
The Azure SignalR Emulator was successfully started.
Press Ctrl+C to stop the Emulator.
TIPS: Use the below value inside *********** block as its ConnectionString:
***********
Endpoint={address.Scheme}://{address.Host};Port={address.Port};AccessKey={AppBuilderExtensions.AccessKey};Version=1.0;
***********
===================================================
");
upstreamOptionMonitor.CurrentValue.Print();
});
app.UseRouting();
app.UseWebSockets();
app.UseAllowAllCors();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints
.MapConnections("/client", cb => cb.UseConnectionHandler<DynamicConnectionHandler>())
.RequireAuthorization();
endpoints.MapControllers();
});
}
private sealed class CustomControllerFeatureProvider : ControllerFeatureProvider
{
protected override bool IsController(TypeInfo typeInfo)
{
var isCustomController = !typeInfo.IsAbstract && typeof(SignalRServiceEmulatorWebApi).IsAssignableFrom(typeInfo);
return isCustomController || base.IsController(typeInfo);
}
}
}
}