1
+ using System ;
2
+ using System . Threading . Tasks ;
3
+ using Microsoft . Data . SqlClient ;
4
+ using Microsoft . Extensions . DependencyInjection ;
5
+ using Microsoft . Extensions . Hosting ;
6
+ using NServiceBus ;
7
+ using NServiceBus . Transport . SqlServer ;
8
+
9
+ Console . Title = "Sender" ;
10
+ var host = Host . CreateDefaultBuilder ( args )
11
+ . UseNServiceBus ( x =>
12
+ {
13
+ var endpointConfiguration = new EndpointConfiguration ( "Samples.SqlOutbox.Sender" ) ;
14
+ endpointConfiguration . EnableInstallers ( ) ;
15
+ endpointConfiguration . SendFailedMessagesTo ( "error" ) ;
16
+
17
+ #region SenderConfiguration
18
+
19
+ //for local instance or SqlExpress
20
+ // string connectionString = @"Data Source=(localdb)\mssqllocaldb;Database=NsbSamplesSqlOutbox;Trusted_Connection=True;MultipleActiveResultSets=true";
21
+ const string connectionString = @"Server=localhost,1433;Initial Catalog=NsbSamplesSqlOutbox;User Id=SA;Password=yourStrong(!)Password;Max Pool Size=100;Encrypt=false" ;
22
+
23
+ var transport = new SqlServerTransport ( connectionString )
24
+ {
25
+ DefaultSchema = "sender" ,
26
+ TransportTransactionMode = TransportTransactionMode . ReceiveOnly
27
+ } ;
28
+ transport . SchemaAndCatalog . UseSchemaForQueue ( "error" , "dbo" ) ;
29
+ transport . SchemaAndCatalog . UseSchemaForQueue ( "audit" , "dbo" ) ;
30
+
31
+ endpointConfiguration . UseTransport ( transport ) ;
32
+
33
+ var persistence = endpointConfiguration . UsePersistence < SqlPersistence > ( ) ;
34
+ persistence . ConnectionBuilder (
35
+ connectionBuilder : ( ) => new SqlConnection ( connectionString )
36
+ ) ;
37
+ var dialect = persistence . SqlDialect < SqlDialect . MsSqlServer > ( ) ;
38
+ dialect . Schema ( "sender" ) ;
39
+ persistence . TablePrefix ( "" ) ;
40
+
41
+ transport . Subscriptions . DisableCaching = true ;
42
+ transport . Subscriptions . SubscriptionTableName = new SubscriptionTableName (
43
+ table : "Subscriptions" ,
44
+ schema : "dbo"
45
+ ) ;
46
+
47
+ endpointConfiguration . EnableOutbox ( ) ;
48
+
49
+ endpointConfiguration . UseSerialization < SystemJsonSerializer > ( ) ;
50
+
51
+ #endregion
52
+
53
+ SqlHelper . CreateSchema ( connectionString , "sender" ) ;
54
+ Console . WriteLine ( "Press enter to send a message" ) ;
55
+ return endpointConfiguration ;
56
+ } )
57
+ . Build ( ) ;
58
+
59
+ await host . StartAsync ( ) ;
60
+
61
+ var messageSession = host . Services . GetService < IMessageSession > ( ) ;
62
+ var random = new Random ( ) ;
63
+
64
+ while ( true )
65
+ {
66
+ if ( ! Console . KeyAvailable )
67
+ {
68
+ await Task . Delay ( 100 ) ;
69
+ continue ;
70
+ }
71
+ var key = Console . ReadKey ( ) ;
72
+ Console . WriteLine ( ) ;
73
+
74
+ if ( key . Key != ConsoleKey . Enter )
75
+ {
76
+ break ;
77
+ }
78
+
79
+ var orderSubmitted = new OrderSubmitted (
80
+ OrderId : Guid . NewGuid ( ) ,
81
+ Value : random . Next ( 100 )
82
+ ) ;
83
+
84
+ await messageSession . Publish ( orderSubmitted ) ;
85
+ }
86
+
87
+ await host . StopAsync ( ) ;
0 commit comments