1
+ using System . Diagnostics ;
2
+ using Aspire . Hosting . ApplicationModel ;
3
+ using Microsoft . AspNetCore . Builder ;
4
+ using Microsoft . AspNetCore . Hosting ;
5
+ using Microsoft . AspNetCore . SignalR ;
6
+ using Microsoft . Extensions . DependencyInjection ;
7
+ using Microsoft . Extensions . Logging ;
8
+
9
+ namespace CommunityToolkit . Aspire . Hosting . ProjectCommander ;
10
+
11
+ /// <summary>
12
+ ///
13
+ /// </summary>
14
+ /// <param name="name"></param>
15
+ /// <param name="options"></param>
16
+ public sealed class ProjectCommanderHubResource ( [ ResourceName ] string name , ProjectCommanderHubOptions options )
17
+ : Resource ( name ) , IResourceWithConnectionString , IAsyncDisposable
18
+ {
19
+ private WebApplication ? _web ;
20
+ private ILogger ? _logger ;
21
+
22
+ internal async Task StartHubAsync ( )
23
+ {
24
+ Hub = BuildHub ( ) ;
25
+
26
+ await ( _web ! . StartAsync ( ) ) . ConfigureAwait ( false ) ;
27
+
28
+ _logger ? . LogInformation ( "Aspire Project Commander Hub started" ) ;
29
+ }
30
+
31
+ internal void SetLogger ( ILogger logger ) => _logger = logger ;
32
+
33
+ internal IHubContext < ProjectCommanderHub > ? Hub { get ; set ; }
34
+
35
+ private IHubContext < ProjectCommanderHub > BuildHub ( )
36
+ {
37
+ // we need the logger to be set before building the hub so we can inject it
38
+ Debug . Assert ( _logger != null , "Logger must be set before building hub" ) ;
39
+
40
+ _logger ? . LogInformation ( "Building SignalR Hub" ) ;
41
+
42
+ // signalr project command host setup
43
+ var host = WebApplication . CreateBuilder ( ) ;
44
+
45
+ // proxy logging to AppHost logger
46
+ host . Services . AddSingleton ( _logger ! ) ;
47
+
48
+ host . WebHost . UseUrls ( $ "{ ( options . UseHttps ? "https" : "http" ) } ://localhost:{ options . HubPort } ") ;
49
+
50
+ host . Services . AddSignalR ( ) ;
51
+
52
+ _web = host . Build ( ) ;
53
+ _web . UseRouting ( ) ;
54
+ _web . MapGet ( "/" , ( ) => "Aspire Project Commander Host 1.0, powered by SignalR." ) ;
55
+ _web . MapHub < ProjectCommanderHub > ( options . HubPath ! ) ;
56
+
57
+ var hub = _web . Services . GetRequiredService < IHubContext < ProjectCommanderHub > > ( ) ;
58
+
59
+ _logger ? . LogInformation ( "SignalR Hub built" ) ;
60
+
61
+ return hub ;
62
+ }
63
+
64
+ /// <summary>
65
+ /// Gets the connection string expression for the SignalR Hub endpoint
66
+ /// </summary>
67
+ public ReferenceExpression ConnectionStringExpression =>
68
+ ReferenceExpression . Create (
69
+ $ "{ ( options . UseHttps ? "https" : "http" ) } ://localhost:{ options . HubPort . ToString ( ) } /{ options . HubPath ! . TrimStart ( '/' ) } ") ;
70
+
71
+ /// <summary>
72
+ /// Disposes hosted resources
73
+ /// </summary>
74
+ /// <returns></returns>
75
+ public async ValueTask DisposeAsync ( )
76
+ {
77
+ if ( _web != null ) await _web . DisposeAsync ( ) ;
78
+ }
79
+ }
0 commit comments