1
1
using System ;
2
+ using System . Threading ;
3
+ using System . Threading . Tasks ;
2
4
using Microsoft . Extensions . DependencyInjection ;
3
5
using Microsoft . Extensions . Hosting ;
4
6
using NServiceBus ;
5
7
6
8
Console . Title = "RegularEndpoint" ;
7
9
8
10
var builder = Host . CreateApplicationBuilder ( args ) ;
9
- builder . Services . AddHostedService < InputLoopService > ( ) ;
10
11
11
12
var endpointConfiguration = new EndpointConfiguration ( "RegularEndpoint" ) ;
12
-
13
13
endpointConfiguration . UseSerialization < SystemJsonSerializer > ( ) ;
14
14
endpointConfiguration . UseTransport < SqsTransport > ( ) ;
15
15
16
-
17
16
Console . WriteLine ( "Press any key, the application is starting" ) ;
18
17
Console . ReadKey ( ) ;
19
18
Console . WriteLine ( "Starting..." ) ;
20
19
21
20
builder . UseNServiceBus ( endpointConfiguration ) ;
22
- await builder . Build ( ) . RunAsync ( ) ;
21
+
22
+ var host = builder . Build ( ) ;
23
+ await host . StartAsync ( ) ;
24
+
25
+ // Get the required services
26
+ var messageSession = host . Services . GetRequiredService < IMessageSession > ( ) ;
27
+ // Register a cancellation token to gracefully handle application shutdown
28
+ var ct = host . Services . GetRequiredService < IHostApplicationLifetime > ( ) . ApplicationStopping ;
29
+
30
+ Console . WriteLine ( "Press [ENTER] to send a message to the serverless endpoint queue." ) ;
31
+ Console . WriteLine ( "Press Ctrl+C to shut down." ) ;
32
+
33
+ // Wait for user input to publish messages
34
+ while ( ! ct . IsCancellationRequested )
35
+ {
36
+ if ( ! Console . KeyAvailable )
37
+ {
38
+ // If no key is pressed, wait for a short time before checking again
39
+ await Task . Delay ( 100 , CancellationToken . None ) ;
40
+ continue ;
41
+ }
42
+
43
+ var key = Console . ReadKey ( ) ;
44
+ Console . WriteLine ( ) ;
45
+
46
+ if ( key . Key == ConsoleKey . Enter )
47
+ {
48
+ await messageSession . Send ( "ServerlessEndpoint" , new TriggerMessage ( ) ) ;
49
+ Console . WriteLine ( "Message sent to the serverless endpoint queue." ) ;
50
+ }
51
+ }
52
+
53
+ // Wait for the host to stop gracefully
54
+ await host . StopAsync ( ) ;
0 commit comments