1
+ namespace Core ;
2
+
3
+ using System . Threading . Tasks ;
4
+ using NServiceBus ;
5
+
6
+ class BasicUsageOfIBus
7
+ {
8
+ async Task Send ( EndpointConfiguration endpointConfiguration )
9
+ {
10
+ #region BasicSend
11
+
12
+ var endpointInstance = await Endpoint . Start ( endpointConfiguration ) ;
13
+ var message = new MyMessage ( ) ;
14
+ await endpointInstance . Send ( message ) ;
15
+
16
+ #endregion
17
+ }
18
+
19
+ #region SendFromHandler
20
+
21
+ public class MyMessageHandler :
22
+ IHandleMessages < MyMessage >
23
+ {
24
+ public Task Handle ( MyMessage message , IMessageHandlerContext context )
25
+ {
26
+ var otherMessage = new OtherMessage ( ) ;
27
+ return context . Send ( otherMessage ) ;
28
+ }
29
+ }
30
+
31
+ #endregion
32
+
33
+ async Task SetDestination ( IEndpointInstance endpoint )
34
+ {
35
+ #region BasicSendSetDestination
36
+
37
+ var options = new SendOptions ( ) ;
38
+ options . SetDestination ( "MyDestination" ) ;
39
+ await endpoint . Send ( new MyMessage ( ) , options ) ;
40
+
41
+ #endregion
42
+ }
43
+
44
+ async Task SpecificInstance ( IEndpointInstance endpoint )
45
+ {
46
+ #region BasicSendSpecificInstance
47
+
48
+ var options = new SendOptions ( ) ;
49
+ options . RouteToSpecificInstance ( "MyInstance" ) ;
50
+ var message = new MyMessage ( ) ;
51
+ await endpoint . Send ( message , options ) ;
52
+
53
+ #endregion
54
+ }
55
+
56
+ async Task ThisEndpoint ( IEndpointInstance endpoint )
57
+ {
58
+ #region BasicSendToAnyInstance
59
+
60
+ var options = new SendOptions ( ) ;
61
+ options . RouteToThisEndpoint ( ) ;
62
+ await endpoint . Send ( new MyMessage ( ) , options ) ;
63
+ // or
64
+ await endpoint . SendLocal ( new MyMessage ( ) ) ;
65
+
66
+ #endregion
67
+ }
68
+
69
+ async Task ThisInstance ( IEndpointInstance endpoint )
70
+ {
71
+ #region BasicSendToThisInstance
72
+
73
+ var options = new SendOptions ( ) ;
74
+ options . RouteToThisInstance ( ) ;
75
+ var message = new MyMessage ( ) ;
76
+ await endpoint . Send ( message , options ) ;
77
+
78
+ #endregion
79
+ }
80
+
81
+ async Task SendReplyToThisInstance ( IEndpointInstance endpoint )
82
+ {
83
+ #region BasicSendReplyToThisInstance
84
+
85
+ var options = new SendOptions ( ) ;
86
+ options . RouteReplyToThisInstance ( ) ;
87
+ var message = new MyMessage ( ) ;
88
+ await endpoint . Send ( message , options ) ;
89
+
90
+ #endregion
91
+ }
92
+
93
+ async Task SendReplyToAnyInstance ( IEndpointInstance endpoint )
94
+ {
95
+ #region BasicSendReplyToAnyInstance
96
+
97
+ var options = new SendOptions ( ) ;
98
+ options . RouteReplyToAnyInstance ( ) ;
99
+ var message = new MyMessage ( ) ;
100
+ await endpoint . Send ( message , options ) ;
101
+
102
+ #endregion
103
+ }
104
+
105
+ async Task SendReplyTo ( IEndpointInstance endpoint )
106
+ {
107
+ #region BasicSendReplyToDestination
108
+
109
+ var options = new SendOptions ( ) ;
110
+ options . RouteReplyTo ( "MyDestination" ) ;
111
+ var message = new MyMessage ( ) ;
112
+ await endpoint . Send ( message , options ) ;
113
+
114
+ #endregion
115
+ }
116
+
117
+ async Task ReplySendReplyToThisInstance ( IMessageHandlerContext context )
118
+ {
119
+ #region BasicReplyReplyToThisInstance
120
+
121
+ var options = new ReplyOptions ( ) ;
122
+ options . RouteReplyToThisInstance ( ) ;
123
+ var myMessage = new MyMessage ( ) ;
124
+ await context . Reply ( myMessage , options ) ;
125
+
126
+ #endregion
127
+ }
128
+
129
+ async Task ReplySendReplyToAnyInstance ( IMessageHandlerContext context )
130
+ {
131
+ #region BasicReplyReplyToAnyInstance
132
+
133
+ var options = new ReplyOptions ( ) ;
134
+ options . RouteReplyToAnyInstance ( ) ;
135
+ var myMessage = new MyMessage ( ) ;
136
+ await context . Reply ( myMessage , options ) ;
137
+
138
+ #endregion
139
+ }
140
+
141
+ async Task ReplySendReplyTo ( IMessageHandlerContext context )
142
+ {
143
+ #region BasicReplyReplyToDestination
144
+
145
+ var options = new ReplyOptions ( ) ;
146
+ options . RouteReplyTo ( "MyDestination" ) ;
147
+ var myMessage = new MyMessage ( ) ;
148
+ await context . Reply ( myMessage , options ) ;
149
+
150
+ #endregion
151
+ }
152
+
153
+ public class MyMessage
154
+ {
155
+ }
156
+
157
+ public class OtherMessage
158
+ {
159
+ }
160
+ }
0 commit comments