├── .gitignore
├── LICENSE
├── README.md
├── SagasDemo.sln
├── src
├── Contracts
│ ├── Configuration
│ │ └── KafkaOptions.cs
│ ├── Contracts.csproj
│ ├── CustomerValidationRequestEvent.cs
│ ├── CustomerValidationResponseEvent.cs
│ ├── Exceptions
│ │ └── NotATransientException.cs
│ ├── FaultMessageEvent.cs
│ ├── NotificationReply.cs
│ ├── OrderRequestEvent.cs
│ ├── OrderResponseEvent.cs
│ ├── TaxesCalculationRequestEvent.cs
│ └── TaxesCalculationResponseEvent.cs
├── CustomerValidationEngine
│ ├── Consumers
│ │ └── CustomerValidationConsumer.cs
│ ├── CustomerValidationEngine.csproj
│ ├── Extensions
│ │ └── KafkaRegistrationExtensions.cs
│ ├── Program.cs
│ ├── Properties
│ │ └── launchSettings.json
│ └── appsettings.json
├── OrdersManagementApi
│ ├── Controllers
│ │ └── OrdersController.cs
│ ├── Extensions
│ │ └── KafkaRegistrationExtensions.cs
│ ├── OrdersManagementApi.csproj
│ ├── Program.cs
│ ├── Properties
│ │ └── launchSettings.json
│ └── appsettings.json
├── OrdersOrchestrator
│ ├── Extensions
│ │ ├── KafkaRegistrationExtensions.cs
│ │ └── SagasRepositoryRegistrationExtensions.cs
│ ├── Middlewares
│ │ ├── FaultProcessingMiddlewareFilter.cs
│ │ └── SagaLoggingMiddlewareFilter.cs
│ ├── OrdersOrchestrator.csproj
│ ├── Program.cs
│ ├── Properties
│ │ └── launchSettings.json
│ ├── Services
│ │ ├── ApiService.cs
│ │ └── IApiService.cs
│ ├── StateMachines
│ │ ├── CustomActivities
│ │ │ ├── CustomerValidationActivity.cs
│ │ │ ├── ProcessFaultActivity.cs
│ │ │ ├── ReceiveOrderRequestActivity.cs
│ │ │ └── TaxesCalculationActivity.cs
│ │ ├── OrderRequestSagaDefinition.cs
│ │ ├── OrderRequestSagaInstance.cs
│ │ ├── OrderRequestStateMachine.cs
│ │ └── OrderRequestStateMachineSupportExtensions.cs
│ ├── Transports
│ │ └── FaultTransport.cs
│ └── appsettings.json
└── TaxesCalculationEngine
│ ├── Consumers
│ └── TaxesCalculationConsumer.cs
│ ├── Extensions
│ └── KafkaRegistrationExtensions.cs
│ ├── Program.cs
│ ├── Properties
│ └── launchSettings.json
│ ├── TaxesCalculationEngine.csproj
│ └── appsettings.json
└── static
├── sagas-flow.drawio
└── sagas-flow.png
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.rsuser
8 | *.suo
9 | *.user
10 | *.userosscache
11 | *.sln.docstates
12 |
13 | # User-specific files (MonoDevelop/Xamarin Studio)
14 | *.userprefs
15 |
16 | # Mono auto generated files
17 | mono_crash.*
18 |
19 | # Build results
20 | [Dd]ebug/
21 | [Dd]ebugPublic/
22 | [Rr]elease/
23 | [Rr]eleases/
24 | x64/
25 | x86/
26 | [Ww][Ii][Nn]32/
27 | [Aa][Rr][Mm]/
28 | [Aa][Rr][Mm]64/
29 | bld/
30 | [Bb]in/
31 | [Oo]bj/
32 | [Ll]og/
33 | [Ll]ogs/
34 |
35 | # Visual Studio 2015/2017 cache/options directory
36 | .vs/
37 | # Uncomment if you have tasks that create the project's static files in wwwroot
38 | #wwwroot/
39 |
40 | # Visual Studio 2017 auto generated files
41 | Generated\ Files/
42 |
43 | # MSTest test Results
44 | [Tt]est[Rr]esult*/
45 | [Bb]uild[Ll]og.*
46 |
47 | # NUnit
48 | *.VisualState.xml
49 | TestResult.xml
50 | nunit-*.xml
51 |
52 | # Build Results of an ATL Project
53 | [Dd]ebugPS/
54 | [Rr]eleasePS/
55 | dlldata.c
56 |
57 | # Benchmark Results
58 | BenchmarkDotNet.Artifacts/
59 |
60 | # .NET Core
61 | project.lock.json
62 | project.fragment.lock.json
63 | artifacts/
64 |
65 | # ASP.NET Scaffolding
66 | ScaffoldingReadMe.txt
67 |
68 | # StyleCop
69 | StyleCopReport.xml
70 |
71 | # Files built by Visual Studio
72 | *_i.c
73 | *_p.c
74 | *_h.h
75 | *.ilk
76 | *.meta
77 | *.obj
78 | *.iobj
79 | *.pch
80 | *.pdb
81 | *.ipdb
82 | *.pgc
83 | *.pgd
84 | *.rsp
85 | *.sbr
86 | *.tlb
87 | *.tli
88 | *.tlh
89 | *.tmp
90 | *.tmp_proj
91 | *_wpftmp.csproj
92 | *.log
93 | *.tlog
94 | *.vspscc
95 | *.vssscc
96 | .builds
97 | *.pidb
98 | *.svclog
99 | *.scc
100 |
101 | # Chutzpah Test files
102 | _Chutzpah*
103 |
104 | # Visual C++ cache files
105 | ipch/
106 | *.aps
107 | *.ncb
108 | *.opendb
109 | *.opensdf
110 | *.sdf
111 | *.cachefile
112 | *.VC.db
113 | *.VC.VC.opendb
114 |
115 | # Visual Studio profiler
116 | *.psess
117 | *.vsp
118 | *.vspx
119 | *.sap
120 |
121 | # Visual Studio Trace Files
122 | *.e2e
123 |
124 | # TFS 2012 Local Workspace
125 | $tf/
126 |
127 | # Guidance Automation Toolkit
128 | *.gpState
129 |
130 | # ReSharper is a .NET coding add-in
131 | _ReSharper*/
132 | *.[Rr]e[Ss]harper
133 | *.DotSettings.user
134 |
135 | # TeamCity is a build add-in
136 | _TeamCity*
137 |
138 | # DotCover is a Code Coverage Tool
139 | *.dotCover
140 |
141 | # AxoCover is a Code Coverage Tool
142 | .axoCover/*
143 | !.axoCover/settings.json
144 |
145 | # Coverlet is a free, cross platform Code Coverage Tool
146 | coverage*.json
147 | coverage*.xml
148 | coverage*.info
149 |
150 | # Visual Studio code coverage results
151 | *.coverage
152 | *.coveragexml
153 |
154 | # NCrunch
155 | _NCrunch_*
156 | .*crunch*.local.xml
157 | nCrunchTemp_*
158 |
159 | # MightyMoose
160 | *.mm.*
161 | AutoTest.Net/
162 |
163 | # Web workbench (sass)
164 | .sass-cache/
165 |
166 | # Installshield output folder
167 | [Ee]xpress/
168 |
169 | # DocProject is a documentation generator add-in
170 | DocProject/buildhelp/
171 | DocProject/Help/*.HxT
172 | DocProject/Help/*.HxC
173 | DocProject/Help/*.hhc
174 | DocProject/Help/*.hhk
175 | DocProject/Help/*.hhp
176 | DocProject/Help/Html2
177 | DocProject/Help/html
178 |
179 | # Click-Once directory
180 | publish/
181 |
182 | # Publish Web Output
183 | *.[Pp]ublish.xml
184 | *.azurePubxml
185 | # Note: Comment the next line if you want to checkin your web deploy settings,
186 | # but database connection strings (with potential passwords) will be unencrypted
187 | *.pubxml
188 | *.publishproj
189 |
190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
191 | # checkin your Azure Web App publish settings, but sensitive information contained
192 | # in these scripts will be unencrypted
193 | PublishScripts/
194 |
195 | # NuGet Packages
196 | *.nupkg
197 | # NuGet Symbol Packages
198 | *.snupkg
199 | # The packages folder can be ignored because of Package Restore
200 | **/[Pp]ackages/*
201 | # except build/, which is used as an MSBuild target.
202 | !**/[Pp]ackages/build/
203 | # Uncomment if necessary however generally it will be regenerated when needed
204 | #!**/[Pp]ackages/repositories.config
205 | # NuGet v3's project.json files produces more ignorable files
206 | *.nuget.props
207 | *.nuget.targets
208 |
209 | # Microsoft Azure Build Output
210 | csx/
211 | *.build.csdef
212 |
213 | # Microsoft Azure Emulator
214 | ecf/
215 | rcf/
216 |
217 | # Windows Store app package directories and files
218 | AppPackages/
219 | BundleArtifacts/
220 | Package.StoreAssociation.xml
221 | _pkginfo.txt
222 | *.appx
223 | *.appxbundle
224 | *.appxupload
225 |
226 | # Visual Studio cache files
227 | # files ending in .cache can be ignored
228 | *.[Cc]ache
229 | # but keep track of directories ending in .cache
230 | !?*.[Cc]ache/
231 |
232 | # Others
233 | ClientBin/
234 | ~$*
235 | *~
236 | *.dbmdl
237 | *.dbproj.schemaview
238 | *.jfm
239 | *.pfx
240 | *.publishsettings
241 | orleans.codegen.cs
242 |
243 | # Including strong name files can present a security risk
244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
245 | #*.snk
246 |
247 | # Since there are multiple workflows, uncomment next line to ignore bower_components
248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
249 | #bower_components/
250 |
251 | # RIA/Silverlight projects
252 | Generated_Code/
253 |
254 | # Backup & report files from converting an old project file
255 | # to a newer Visual Studio version. Backup files are not needed,
256 | # because we have git ;-)
257 | _UpgradeReport_Files/
258 | Backup*/
259 | UpgradeLog*.XML
260 | UpgradeLog*.htm
261 | ServiceFabricBackup/
262 | *.rptproj.bak
263 |
264 | # SQL Server files
265 | *.mdf
266 | *.ldf
267 | *.ndf
268 |
269 | # Business Intelligence projects
270 | *.rdl.data
271 | *.bim.layout
272 | *.bim_*.settings
273 | *.rptproj.rsuser
274 | *- [Bb]ackup.rdl
275 | *- [Bb]ackup ([0-9]).rdl
276 | *- [Bb]ackup ([0-9][0-9]).rdl
277 |
278 | # Microsoft Fakes
279 | FakesAssemblies/
280 |
281 | # GhostDoc plugin setting file
282 | *.GhostDoc.xml
283 |
284 | # Node.js Tools for Visual Studio
285 | .ntvs_analysis.dat
286 | node_modules/
287 |
288 | # Visual Studio 6 build log
289 | *.plg
290 |
291 | # Visual Studio 6 workspace options file
292 | *.opt
293 |
294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
295 | *.vbw
296 |
297 | # Visual Studio 6 auto-generated project file (contains which files were open etc.)
298 | *.vbp
299 |
300 | # Visual Studio 6 workspace and project file (working project files containing files to include in project)
301 | *.dsw
302 | *.dsp
303 |
304 | # Visual Studio 6 technical files
305 | *.ncb
306 | *.aps
307 |
308 | # Visual Studio LightSwitch build output
309 | **/*.HTMLClient/GeneratedArtifacts
310 | **/*.DesktopClient/GeneratedArtifacts
311 | **/*.DesktopClient/ModelManifest.xml
312 | **/*.Server/GeneratedArtifacts
313 | **/*.Server/ModelManifest.xml
314 | _Pvt_Extensions
315 |
316 | # Paket dependency manager
317 | .paket/paket.exe
318 | paket-files/
319 |
320 | # FAKE - F# Make
321 | .fake/
322 |
323 | # CodeRush personal settings
324 | .cr/personal
325 |
326 | # Python Tools for Visual Studio (PTVS)
327 | __pycache__/
328 | *.pyc
329 |
330 | # Cake - Uncomment if you are using it
331 | # tools/**
332 | # !tools/packages.config
333 |
334 | # Tabs Studio
335 | *.tss
336 |
337 | # Telerik's JustMock configuration file
338 | *.jmconfig
339 |
340 | # BizTalk build output
341 | *.btp.cs
342 | *.btm.cs
343 | *.odx.cs
344 | *.xsd.cs
345 |
346 | # OpenCover UI analysis results
347 | OpenCover/
348 |
349 | # Azure Stream Analytics local run output
350 | ASALocalRun/
351 |
352 | # MSBuild Binary and Structured Log
353 | *.binlog
354 |
355 | # NVidia Nsight GPU debugger configuration file
356 | *.nvuser
357 |
358 | # MFractors (Xamarin productivity tool) working folder
359 | .mfractor/
360 |
361 | # Local History for Visual Studio
362 | .localhistory/
363 |
364 | # Visual Studio History (VSHistory) files
365 | .vshistory/
366 |
367 | # BeatPulse healthcheck temp database
368 | healthchecksdb
369 |
370 | # Backup folder for Package Reference Convert tool in Visual Studio 2017
371 | MigrationBackup/
372 |
373 | # Ionide (cross platform F# VS Code tools) working folder
374 | .ionide/
375 |
376 | # Fody - auto-generated XML schema
377 | FodyWeavers.xsd
378 |
379 | # VS Code files for those working on multiple tools
380 | .vscode/*
381 | !.vscode/settings.json
382 | !.vscode/tasks.json
383 | !.vscode/launch.json
384 | !.vscode/extensions.json
385 | *.code-workspace
386 |
387 | # Local History for Visual Studio Code
388 | .history/
389 | .idea/
390 |
391 | # Windows Installer files from build outputs
392 | *.cab
393 | *.msi
394 | *.msix
395 | *.msm
396 | *.msp
397 |
398 | # JetBrains Rider
399 | *.sln.iml
400 | .DS_Store
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Kafka Sagas Demo
2 |
3 | ## Overview
4 |
5 | This was built as a POC to validate the usage of the Sagas pattern using some of the components provided by the Masstransit package. Here we tested some of the most basic Masstransit features related to the Sagas pattern implementation such as State Machines, State Machines Activities, Events Orchestration, Message Retry/Redelivery and Instance persistence to name a few. To develop this we used the [Masstransit documentation](https://masstransit.io/) and the [Kafka YouTube series](https://www.youtube.com/watch?v=CJ_srcJiIKs&list=PLx8uyNNs1ri0RJ3hqwcDze6yAkrmK1QI5 ) by Chris Patterson.
6 |
7 | ## Basic sagas flow
8 |
9 | Here we are presenting a basic flow of how all the components should work. We also implemented a fault recovery pipeline using an error topic.
10 |
11 | 
12 |
13 | ## External servers
14 |
15 | To develop this application, we used some popular servers such as [Confluent Kafka Cloud](https://www.confluent.io/), [MongoDB Atlas](https://www.mongodb.com/atlas) and [Redis on Cloud](https://redis.com/). All the mentioned resources are very easy to use and have free trial plans.
16 |
17 | ## Usage
18 |
19 | Once you have the codebase, simply adjust the configuration files with your configuration and run all the projects. To produce messages to the Kafka server, use the `OrderManagentApi`. To facilitate, we are providing the topics list below so it will be easier to replicate it on your server. As far as database configuration, MassTransit makes it very easy, requiring minimal manual configuration. So, assuming you have your database server up and running, everything should work out of the box for both MongoDB and Redis.
20 |
21 | ### Topics list
22 | - ORDER_MANAGEMENT_SYSTEM.REQUEST
23 | - ORDER_MANAGEMENT_SYSTEM.RESPONSE
24 | - TAXES_CALCULATION_ENGINE.REQUEST
25 | - TAXES_CALCULATION_ENGINE.RESPONSE
26 | - CUSTOMER_VALIDATION_ENGINE.REQUEST
27 | - CUSTOMER_VALIDATION_ENGINE.RESPONSE
28 | - ERROR
29 |
30 | ## Important
31 |
32 | You can run database and Kafka servers locally. Just make sure to adjust all the configurations within all the runnable projects.
33 | If you're having a hard time trying to run this demo application, feel free to contact me and I will try to help the best way I can.
--------------------------------------------------------------------------------
/SagasDemo.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.4.33110.190
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OrdersManagementApi", "src\OrdersManagementApi\OrdersManagementApi.csproj", "{A6EDEE5B-9D7C-4F93-8F7B-05F9FA90DACF}"
7 | EndProject
8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OrdersOrchestrator", "src\OrdersOrchestrator\OrdersOrchestrator.csproj", "{02C3961C-8C73-4607-9398-D4BAF7AE881D}"
9 | EndProject
10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TaxesCalculationEngine", "src\TaxesCalculationEngine\TaxesCalculationEngine.csproj", "{901C6735-14DF-4067-AE6D-22D42E465BEB}"
11 | EndProject
12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CustomerValidationEngine", "src\CustomerValidationEngine\CustomerValidationEngine.csproj", "{259DFD73-14B2-4500-A614-1BDA4F0CC6CD}"
13 | EndProject
14 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Contracts", "src\Contracts\Contracts.csproj", "{525D8152-B5FE-498E-8A89-DA77D1E35714}"
15 | EndProject
16 | Global
17 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
18 | Debug|Any CPU = Debug|Any CPU
19 | Release|Any CPU = Release|Any CPU
20 | EndGlobalSection
21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
22 | {A6EDEE5B-9D7C-4F93-8F7B-05F9FA90DACF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23 | {A6EDEE5B-9D7C-4F93-8F7B-05F9FA90DACF}.Debug|Any CPU.Build.0 = Debug|Any CPU
24 | {A6EDEE5B-9D7C-4F93-8F7B-05F9FA90DACF}.Release|Any CPU.ActiveCfg = Release|Any CPU
25 | {A6EDEE5B-9D7C-4F93-8F7B-05F9FA90DACF}.Release|Any CPU.Build.0 = Release|Any CPU
26 | {02C3961C-8C73-4607-9398-D4BAF7AE881D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27 | {02C3961C-8C73-4607-9398-D4BAF7AE881D}.Debug|Any CPU.Build.0 = Debug|Any CPU
28 | {02C3961C-8C73-4607-9398-D4BAF7AE881D}.Release|Any CPU.ActiveCfg = Release|Any CPU
29 | {02C3961C-8C73-4607-9398-D4BAF7AE881D}.Release|Any CPU.Build.0 = Release|Any CPU
30 | {901C6735-14DF-4067-AE6D-22D42E465BEB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
31 | {901C6735-14DF-4067-AE6D-22D42E465BEB}.Debug|Any CPU.Build.0 = Debug|Any CPU
32 | {901C6735-14DF-4067-AE6D-22D42E465BEB}.Release|Any CPU.ActiveCfg = Release|Any CPU
33 | {901C6735-14DF-4067-AE6D-22D42E465BEB}.Release|Any CPU.Build.0 = Release|Any CPU
34 | {259DFD73-14B2-4500-A614-1BDA4F0CC6CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
35 | {259DFD73-14B2-4500-A614-1BDA4F0CC6CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
36 | {259DFD73-14B2-4500-A614-1BDA4F0CC6CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
37 | {259DFD73-14B2-4500-A614-1BDA4F0CC6CD}.Release|Any CPU.Build.0 = Release|Any CPU
38 | {525D8152-B5FE-498E-8A89-DA77D1E35714}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39 | {525D8152-B5FE-498E-8A89-DA77D1E35714}.Debug|Any CPU.Build.0 = Debug|Any CPU
40 | {525D8152-B5FE-498E-8A89-DA77D1E35714}.Release|Any CPU.ActiveCfg = Release|Any CPU
41 | {525D8152-B5FE-498E-8A89-DA77D1E35714}.Release|Any CPU.Build.0 = Release|Any CPU
42 | EndGlobalSection
43 | GlobalSection(SolutionProperties) = preSolution
44 | HideSolutionNode = FALSE
45 | EndGlobalSection
46 | GlobalSection(ExtensibilityGlobals) = postSolution
47 | SolutionGuid = {3CDE34AD-0937-420D-8A56-F3D50AAFBCEA}
48 | EndGlobalSection
49 | EndGlobal
50 |
--------------------------------------------------------------------------------
/src/Contracts/Configuration/KafkaOptions.cs:
--------------------------------------------------------------------------------
1 | using Confluent.Kafka;
2 | using StackExchange.Redis;
3 |
4 | namespace Contracts.Configuration;
5 |
6 | public sealed record KafkaOptions
7 | {
8 | public string ConsumerGroup { get; set; } = default!;
9 | public Topics Topics { get; set; } = default!;
10 | public ClientConfig ClientConfig { get; set; } = default!;
11 | public MongoDb MongoDb { get; set; } = default!;
12 | public RedisDb RedisDb { get; set; } = default!;
13 | };
14 |
15 | public sealed record Topics
16 | {
17 | public string OrderManagementSystemRequest { get; set; } = default!;
18 | public string OrderManagementSystemResponse { get; set; } = default!;
19 | public string TaxesCalculationEngineRequest { get; set; } = default!;
20 | public string TaxesCalculationEngineResponse { get; set; } = default!;
21 | public string CustomerValidationEngineRequest { get; set; } = default!;
22 | public string CustomerValidationEngineResponse { get; set; } = default!;
23 | public string Error { get; set; } = default!;
24 | }
25 |
26 | public sealed record MongoDb
27 | {
28 | public string ConnectionString { get; set; } = default!;
29 | public string DatabaseName { get; set; } = default!;
30 | public string CollectionName { get; set; } = default!;
31 | }
32 |
33 | public sealed record RedisDb
34 | {
35 | public string Endpoint { get; set; } = default!;
36 | public string Password { get; set; } = default!;
37 | public string KeyPrefix { get; set; } = default!;
38 | }
--------------------------------------------------------------------------------
/src/Contracts/Contracts.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net6.0
5 | enable
6 | enable
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/src/Contracts/CustomerValidationRequestEvent.cs:
--------------------------------------------------------------------------------
1 | namespace Contracts;
2 |
3 | public record CustomerValidationRequestEvent
4 | {
5 | public string CustomerId { get; set; } = string.Empty;
6 | }
7 |
8 |
--------------------------------------------------------------------------------
/src/Contracts/CustomerValidationResponseEvent.cs:
--------------------------------------------------------------------------------
1 | namespace Contracts;
2 |
3 | public record CustomerValidationResponseEvent
4 | {
5 | public string CustomerType { get; set; } = default!;
6 | }
7 |
8 |
--------------------------------------------------------------------------------
/src/Contracts/Exceptions/NotATransientException.cs:
--------------------------------------------------------------------------------
1 | namespace Contracts.Exceptions;
2 |
3 | [Serializable]
4 | public class NotATransientException : Exception
5 | {
6 | public NotATransientException() { }
7 |
8 | public NotATransientException(string message)
9 | : base(message) { }
10 |
11 | public NotATransientException(string message, Exception inner)
12 | : base(message, inner) { }
13 | }
--------------------------------------------------------------------------------
/src/Contracts/FaultMessageEvent.cs:
--------------------------------------------------------------------------------
1 | namespace Contracts;
2 |
3 | public record FaultMessageEvent
4 | {
5 | public object? Event { get; set; } = default!;
6 | public string? ExceptionMessage { get; set; }
7 | }
--------------------------------------------------------------------------------
/src/Contracts/NotificationReply.cs:
--------------------------------------------------------------------------------
1 | namespace Contracts
2 | {
3 | public sealed class NotificationReply
4 | where TMessage: class
5 | {
6 | public bool Success { get; set; }
7 | public string? Reason { get; set; }
8 | public TMessage? Data { get; set; }
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/Contracts/OrderRequestEvent.cs:
--------------------------------------------------------------------------------
1 | namespace Contracts;
2 |
3 | public record OrderRequestEvent
4 | {
5 | public string CustomerId { get; set;} = default!;
6 | public string ItemId { get; set; } = default!;
7 | }
--------------------------------------------------------------------------------
/src/Contracts/OrderResponseEvent.cs:
--------------------------------------------------------------------------------
1 | namespace Contracts;
2 |
3 | public record OrderResponseEvent
4 | {
5 | public string CustomerId { get; set; } = default!;
6 | public string CustomerType { get; set; } = default!;
7 | public TaxesCalculationResponseEvent TaxesCalculation { get; set; } = default!;
8 | }
--------------------------------------------------------------------------------
/src/Contracts/TaxesCalculationRequestEvent.cs:
--------------------------------------------------------------------------------
1 | namespace Contracts;
2 |
3 | public record TaxesCalculationRequestEvent
4 | {
5 | public string CustomerType { get; set; } = default!;
6 | public string ItemId { get; set; } = default!;
7 | }
--------------------------------------------------------------------------------
/src/Contracts/TaxesCalculationResponseEvent.cs:
--------------------------------------------------------------------------------
1 | namespace Contracts;
2 |
3 | public record TaxesCalculationResponseEvent
4 | {
5 | public string ItemId { get; set; } = default!;
6 | public decimal TaxAAA { get; set; }
7 | public decimal TaxBBB { get; set; }
8 | public decimal TaxCCC { get; set; }
9 | }
--------------------------------------------------------------------------------
/src/CustomerValidationEngine/Consumers/CustomerValidationConsumer.cs:
--------------------------------------------------------------------------------
1 | using Contracts;
2 | using MassTransit;
3 |
4 | namespace CustomerValidationEngine.Consumers;
5 |
6 | public sealed class CustomerValidationConsumer : IConsumer
7 | {
8 | private readonly ITopicProducer producer;
9 |
10 | public CustomerValidationConsumer(ITopicProducer producer)
11 | {
12 | this.producer = producer;
13 | }
14 |
15 | async Task IConsumer
16 | .Consume(ConsumeContext context)
17 | {
18 | ArgumentNullException.ThrowIfNull(context, nameof(context));
19 |
20 | var key = Guid.NewGuid();
21 |
22 | var response = new CustomerValidationResponseEvent
23 | {
24 | CustomerType = GenerateCustomerType(),
25 | };
26 |
27 | await producer
28 | .Produce(key.ToString(), response)
29 | .ConfigureAwait(false);
30 | }
31 |
32 | private static string GenerateCustomerType()
33 | {
34 | return new Random().Next(1, 5) switch
35 | {
36 | 1 => "Regular",
37 | 2 => "Premium",
38 | 3 => "Super Premium",
39 | _ => "Unknown"
40 | };
41 | }
42 | }
--------------------------------------------------------------------------------
/src/CustomerValidationEngine/CustomerValidationEngine.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net6.0
5 | enable
6 | enable
7 | fc155dc0-4ca0-483e-89f9-75a60c8746e7
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/src/CustomerValidationEngine/Extensions/KafkaRegistrationExtensions.cs:
--------------------------------------------------------------------------------
1 | using Confluent.Kafka;
2 | using Contracts;
3 | using Contracts.Configuration;
4 | using CustomerValidationEngine.Consumers;
5 | using MassTransit;
6 |
7 | namespace CustomerValidationEngine.Extensions;
8 |
9 | public static class KafkaRegistrationExtensions
10 | {
11 | internal static IServiceCollection AddCustomKafka(this IServiceCollection services, IConfiguration configuration)
12 | {
13 | ArgumentNullException.ThrowIfNull(services, nameof(services));
14 | ArgumentNullException.ThrowIfNull(configuration, nameof(configuration));
15 |
16 | var kafkaOptions = configuration
17 | .GetSection("KafkaOptions")
18 | .Get();
19 |
20 | services.AddMassTransit(massTransit =>
21 | {
22 | massTransit.UsingInMemory((context, cfg) =>
23 | {
24 | cfg.ConfigureEndpoints(context);
25 | });
26 |
27 | massTransit.AddRider(rider =>
28 | {
29 | rider.AddProducer(kafkaOptions.Topics.CustomerValidationEngineResponse);
30 | rider.AddConsumersFromNamespaceContaining();
31 |
32 | rider.UsingKafka(kafkaOptions.ClientConfig, (riderContext, kafkaConfig) =>
33 | {
34 | kafkaConfig.TopicEndpoint(
35 | topicName: kafkaOptions.Topics.CustomerValidationEngineRequest,
36 | groupId: kafkaOptions.ConsumerGroup,
37 | configure: topicConfig =>
38 | {
39 | topicConfig.AutoOffsetReset = AutoOffsetReset.Earliest;
40 | topicConfig.ConfigureConsumer(riderContext);
41 | topicConfig.DiscardSkippedMessages();
42 | });
43 | });
44 | });
45 | });
46 |
47 | return services;
48 | }
49 | }
50 |
51 |
--------------------------------------------------------------------------------
/src/CustomerValidationEngine/Program.cs:
--------------------------------------------------------------------------------
1 | using CustomerValidationEngine.Extensions;
2 |
3 | var builder = WebApplication.CreateBuilder(args);
4 |
5 | builder.Services.AddCustomKafka(builder.Configuration);
6 | builder.Services.AddControllers();
7 | builder.Services.AddHealthChecks();
8 |
9 | var app = builder.Build();
10 |
11 | app.UseRouting();
12 | app.UseHttpsRedirection();
13 | app.UseAuthorization();
14 | app.MapControllers();
15 | app.Run();
--------------------------------------------------------------------------------
/src/CustomerValidationEngine/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "profiles": {
3 | "CustomerValidationEngine": {
4 | "commandName": "Project",
5 | "dotnetRunMessages": true,
6 | "applicationUrl": "https://localhost:7099;http://localhost:5198",
7 | "environmentVariables": {
8 | "DOTNET_ENVIRONMENT": "Development"
9 | }
10 | }
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/CustomerValidationEngine/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Information",
5 | "Microsoft.AspNetCore": "Warning"
6 | }
7 | },
8 | "KafkaOptions": {
9 | "ConsumerGroup": "CustomerValidationEngine.Group",
10 | "Topics": {
11 | "CustomerValidationEngineRequest": "CUSTOMER_VALIDATION_ENGINE.REQUEST",
12 | "CustomerValidationEngineResponse": "CUSTOMER_VALIDATION_ENGINE.RESPONSE"
13 | },
14 | "ClientConfig": {
15 | "BootstrapServers": "pkc-6ojv2.us-west4.gcp.confluent.cloud:9092",
16 | "SaslMechanism": "PLAIN",
17 | "SecurityProtocol": "SASLSSL",
18 | "SaslUsername": "",
19 | "SaslPassword": ""
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/OrdersManagementApi/Controllers/OrdersController.cs:
--------------------------------------------------------------------------------
1 | using Contracts;
2 | using MassTransit;
3 | using Microsoft.AspNetCore.Mvc;
4 |
5 | namespace OrdersManagementApi.Controllers;
6 |
7 | [ApiController]
8 | [Route("[controller]")]
9 | public sealed class OrdersController : ControllerBase
10 | {
11 | private readonly ITopicProducer _producer;
12 |
13 | public OrdersController(ITopicProducer producer)
14 | {
15 | _producer = producer;
16 | }
17 |
18 | [HttpPost(Name = "Place")]
19 | public async Task Post([FromBody] OrderRequestEvent orderRequest)
20 | {
21 | var key = Guid.NewGuid();
22 | var correlationId = Guid.NewGuid();
23 |
24 | await _producer
25 | .Produce(
26 | key.ToString(),
27 | orderRequest,
28 | Pipe.Execute(p =>
29 | {
30 | p.CorrelationId = correlationId;
31 | }))
32 | .ConfigureAwait(false);
33 |
34 | return Accepted(new
35 | {
36 | CorrelationId = correlationId,
37 | Key = key
38 | });
39 | }
40 | }
--------------------------------------------------------------------------------
/src/OrdersManagementApi/Extensions/KafkaRegistrationExtensions.cs:
--------------------------------------------------------------------------------
1 | using Contracts;
2 | using Contracts.Configuration;
3 | using MassTransit;
4 |
5 | namespace OrdersManagementApi.Extensions;
6 |
7 | internal static class KafkaExtensions
8 | {
9 | internal static IServiceCollection AddCustomKafka(
10 | this IServiceCollection services,
11 | IConfiguration configuration)
12 | {
13 | ArgumentNullException.ThrowIfNull(services, nameof(services));
14 | ArgumentNullException.ThrowIfNull(configuration, nameof(configuration));
15 |
16 | var kafkaOptions = configuration
17 | .GetSection("KafkaOptions")
18 | .Get();
19 |
20 | services.AddMassTransit(configureMassTransit =>
21 | {
22 | configureMassTransit.UsingInMemory();
23 | configureMassTransit.AddRider(configureRider =>
24 | {
25 | configureRider.AddProducer(kafkaOptions.Topics.OrderManagementSystemRequest);
26 | configureRider.UsingKafka(kafkaOptions.ClientConfig, (_, _) => { });
27 | });
28 | });
29 |
30 | return services;
31 | }
32 | }
--------------------------------------------------------------------------------
/src/OrdersManagementApi/OrdersManagementApi.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net6.0
5 | enable
6 | enable
7 | 1e06faf2-09fe-4eaa-a398-709448408328
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/src/OrdersManagementApi/Program.cs:
--------------------------------------------------------------------------------
1 | using OrdersManagementApi.Extensions;
2 |
3 | var builder = WebApplication.CreateBuilder(args);
4 |
5 | // Add services to the container.
6 | builder.Services.AddControllers();
7 |
8 | // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
9 | builder.Services.AddEndpointsApiExplorer();
10 | builder.Services.AddSwaggerGen();
11 | builder.Services.AddCustomKafka(builder.Configuration);
12 |
13 | var app = builder.Build();
14 |
15 | // Configure the HTTP request pipeline.
16 | if (app.Environment.IsDevelopment())
17 | {
18 | app.UseSwagger();
19 | app.UseSwaggerUI();
20 | }
21 |
22 | app.UseHttpsRedirection();
23 | app.UseAuthorization();
24 | app.MapControllers();
25 | app.Run();;
--------------------------------------------------------------------------------
/src/OrdersManagementApi/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "https://json.schemastore.org/launchsettings.json",
3 | "profiles": {
4 | "Producer": {
5 | "commandName": "Project",
6 | "dotnetRunMessages": true,
7 | "launchBrowser": true,
8 | "launchUrl": "swagger",
9 | "applicationUrl": "https://localhost:7059;http://localhost:5167",
10 | "environmentVariables": {
11 | "ASPNETCORE_ENVIRONMENT": "Development"
12 | }
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/OrdersManagementApi/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Information",
5 | "Microsoft.AspNetCore": "Warning"
6 | }
7 | },
8 | "KafkaOptions": {
9 | "Topics": {
10 | "OrderManagementSystemRequest": "ORDER_MANAGEMENT_SYSTEM.REQUEST"
11 | },
12 | "ClientConfig": {
13 | "BootstrapServers": "pkc-6ojv2.us-west4.gcp.confluent.cloud:9092",
14 | "SaslMechanism": "PLAIN",
15 | "SecurityProtocol": "SASLSSL",
16 | "SaslUsername": "",
17 | "SaslPassword": ""
18 | }
19 | },
20 | "AllowedHosts": "*"
21 | }
22 |
--------------------------------------------------------------------------------
/src/OrdersOrchestrator/Extensions/KafkaRegistrationExtensions.cs:
--------------------------------------------------------------------------------
1 | using Confluent.Kafka;
2 | using Contracts;
3 | using Contracts.Configuration;
4 | using MassTransit;
5 | using MassTransit.Transports;
6 | using OrdersOrchestrator.StateMachines;
7 | using OrdersOrchestrator.Transports;
8 |
9 | namespace OrdersOrchestrator.Extensions;
10 |
11 | internal static class KafkaRegistrationExtensions
12 | {
13 | internal static IServiceCollection AddCustomKafka(this IServiceCollection services, IConfiguration configuration)
14 | {
15 | ArgumentNullException.ThrowIfNull(services, nameof(services));
16 | ArgumentNullException.ThrowIfNull(configuration, nameof(configuration));
17 |
18 | var kafkaOptions = configuration
19 | .GetSection(nameof(KafkaOptions))
20 | .Get();
21 |
22 | services.AddMassTransit(massTransit =>
23 | {
24 | massTransit.UsingInMemory((context, cfg) => cfg.ConfigureEndpoints(context));
25 | massTransit.AddRider(rider =>
26 | {
27 | rider.AddTransient();
28 |
29 | rider
30 | .AddSagaStateMachine(typeof(OrderRequestSagaDefinition))
31 | .ConfigureMongoRepository(kafkaOptions.MongoDb);
32 |
33 | rider.AddProducer>(kafkaOptions.Topics.OrderManagementSystemResponse);
34 | rider.AddProducer(kafkaOptions.Topics.TaxesCalculationEngineRequest);
35 | rider.AddProducer(kafkaOptions.Topics.CustomerValidationEngineRequest);
36 | rider.AddProducer(kafkaOptions.Topics.Error);
37 | rider.AddProducer(kafkaOptions.Topics.OrderManagementSystemRequest);
38 |
39 | rider.UsingKafka(kafkaOptions.ClientConfig, (riderContext, kafkaConfig) =>
40 | {
41 | kafkaConfig.TopicEndpoint(
42 | topicName: kafkaOptions.Topics.OrderManagementSystemRequest,
43 | groupId: kafkaOptions.ConsumerGroup,
44 | configure: topicConfig =>
45 | {
46 | topicConfig.AutoOffsetReset = AutoOffsetReset.Earliest;
47 | topicConfig.DiscardSkippedMessages();
48 | topicConfig.ConfigureSaga(riderContext);
49 | topicConfig.UseInMemoryOutbox(riderContext);
50 | });
51 |
52 | kafkaConfig.TopicEndpoint(
53 | topicName: kafkaOptions.Topics.TaxesCalculationEngineResponse,
54 | groupId: kafkaOptions.ConsumerGroup,
55 | configure: topicConfig =>
56 | {
57 | topicConfig.AutoOffsetReset = AutoOffsetReset.Earliest;
58 | topicConfig.DiscardSkippedMessages();
59 | topicConfig.ConfigureSaga(riderContext);
60 | topicConfig.UseInMemoryOutbox(riderContext);
61 | });
62 |
63 | kafkaConfig.TopicEndpoint(
64 | topicName: kafkaOptions.Topics.CustomerValidationEngineResponse,
65 | groupId: kafkaOptions.ConsumerGroup,
66 | configure: topicConfig =>
67 | {
68 | topicConfig.AutoOffsetReset = AutoOffsetReset.Earliest;
69 | topicConfig.DiscardSkippedMessages();
70 | topicConfig.ConfigureSaga(riderContext);
71 | topicConfig.UseInMemoryOutbox(riderContext);
72 | });
73 |
74 | kafkaConfig.TopicEndpoint(
75 | topicName: kafkaOptions.Topics.Error,
76 | groupId: kafkaOptions.Topics.Error,
77 | configure: topicConfig =>
78 | {
79 | topicConfig.AutoOffsetReset = AutoOffsetReset.Earliest;
80 | topicConfig.DiscardSkippedMessages();
81 | topicConfig.ConfigureSaga(riderContext);
82 | topicConfig.UseInMemoryOutbox(riderContext);
83 |
84 | });
85 | });
86 | });
87 | });
88 |
89 | return services;
90 | }
91 | }
--------------------------------------------------------------------------------
/src/OrdersOrchestrator/Extensions/SagasRepositoryRegistrationExtensions.cs:
--------------------------------------------------------------------------------
1 | using Contracts.Configuration;
2 | using MassTransit;
3 | using StackExchange.Redis;
4 |
5 | namespace OrdersOrchestrator.Extensions;
6 |
7 | internal static class SagasRepositoryRegistrationExtensions
8 | {
9 | internal static ISagaRegistrationConfigurator ConfigureRedisRepository(
10 | this ISagaRegistrationConfigurator configurator,
11 | RedisDb redisOptions)
12 | where TStateMachine : class, SagaStateMachine
13 | where T : class, SagaStateMachineInstance, ISagaVersion
14 | {
15 | ArgumentNullException.ThrowIfNull(configurator, nameof(configurator));
16 | ArgumentNullException.ThrowIfNull(redisOptions, nameof(redisOptions));
17 |
18 | return configurator.RedisRepository(configure =>
19 | {
20 | var options = new ConfigurationOptions
21 | {
22 | EndPoints = { redisOptions.Endpoint },
23 | Password = redisOptions.Password
24 | };
25 |
26 | configure.ConcurrencyMode = ConcurrencyMode.Pessimistic;
27 | configure.KeyPrefix = redisOptions.KeyPrefix;
28 | configure.DatabaseConfiguration(options);
29 | });
30 | }
31 |
32 | internal static ISagaRegistrationConfigurator ConfigureMongoRepository(
33 | this ISagaRegistrationConfigurator configurator,
34 | MongoDb mongoDb)
35 | where TStateMachine : class, SagaStateMachine
36 | where T : class, SagaStateMachineInstance, ISagaVersion
37 | {
38 | ArgumentNullException.ThrowIfNull(configurator, nameof(configurator));
39 | ArgumentNullException.ThrowIfNull(mongoDb, nameof(mongoDb));
40 |
41 | return configurator.MongoDbRepository(configure =>
42 | {
43 | configure.CollectionName = mongoDb.CollectionName;
44 | configure.Connection = mongoDb.ConnectionString;
45 | configure.DatabaseName = mongoDb.DatabaseName;
46 | });
47 | }
48 | }
--------------------------------------------------------------------------------
/src/OrdersOrchestrator/Middlewares/FaultProcessingMiddlewareFilter.cs:
--------------------------------------------------------------------------------
1 | using MassTransit;
2 | using MassTransit.Transports;
3 |
4 | namespace OrdersOrchestrator.Middlewares;
5 |
6 | public sealed class FaultProcessingMiddlewareFilter
7 | : IFilter
8 | {
9 | private readonly IErrorTransport errorTransport;
10 |
11 | public FaultProcessingMiddlewareFilter(IErrorTransport errorTransport)
12 | {
13 | this.errorTransport = errorTransport;
14 | }
15 |
16 | async Task IFilter
17 | .Send(ExceptionReceiveContext context, IPipe next)
18 | {
19 | ArgumentNullException.ThrowIfNull(context, nameof(context));
20 | ArgumentNullException.ThrowIfNull(next, nameof(next));
21 |
22 | context.AddOrUpdatePayload(
23 | () => errorTransport,
24 | _ => errorTransport);
25 |
26 | await next.Send(context)
27 | .ConfigureAwait(false);
28 | }
29 |
30 | void IProbeSite.Probe(ProbeContext context)
31 | => context.CreateScope(nameof(FaultProcessingMiddlewareFilter));
32 | }
--------------------------------------------------------------------------------
/src/OrdersOrchestrator/Middlewares/SagaLoggingMiddlewareFilter.cs:
--------------------------------------------------------------------------------
1 | using MassTransit;
2 |
3 | namespace OrdersOrchestrator.Middlewares;
4 |
5 | public sealed class SagaLoggingMiddlewareFilter
6 | : IFilter>
7 | where TSaga : class, ISaga
8 | {
9 | async Task IFilter>.Send(
10 | SagaConsumeContext context,
11 | IPipe> next)
12 | {
13 | ArgumentNullException.ThrowIfNull(context, nameof(context));
14 | ArgumentNullException.ThrowIfNull(next, nameof(next));
15 |
16 | await next
17 | .Send(context)
18 | .ConfigureAwait(false);
19 | }
20 |
21 | void IProbeSite.Probe(ProbeContext context)
22 | => context.CreateScope(nameof(SagaLoggingMiddlewareFilter));
23 | }
--------------------------------------------------------------------------------
/src/OrdersOrchestrator/OrdersOrchestrator.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net6.0
5 | enable
6 | enable
7 | dotnet-Orchestrator-ACC8B0E7-C79B-46D3-85F9-30223993442F
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 | all
20 | runtime; build; native; contentfiles; analyzers; buildtransitive
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/src/OrdersOrchestrator/Program.cs:
--------------------------------------------------------------------------------
1 | using OrdersOrchestrator.Extensions;
2 | using OrdersOrchestrator.Services;
3 |
4 | var builder = WebApplication.CreateBuilder(args);
5 |
6 | builder.Services.AddCustomKafka(builder.Configuration);
7 | builder.Services.AddControllers();
8 | builder.Services.AddHealthChecks();
9 | builder.Services.AddTransient();
10 |
11 | var app = builder.Build();
12 |
13 | app.UseRouting();
14 | app.UseHttpsRedirection();
15 | app.UseAuthorization();
16 | app.MapControllers();
17 | app.Run();
--------------------------------------------------------------------------------
/src/OrdersOrchestrator/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "profiles": {
3 | "Orchestrator": {
4 | "commandName": "Project",
5 | "dotnetRunMessages": true,
6 | "applicationUrl": "https://localhost:7079;http://localhost:5187",
7 | "environmentVariables": {
8 | "ASPNETCORE_ENVIRONMENT": "Development"
9 | }
10 | }
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/OrdersOrchestrator/Services/ApiService.cs:
--------------------------------------------------------------------------------
1 | using Contracts;
2 |
3 | namespace OrdersOrchestrator.Services
4 | {
5 | public sealed class ApiService : IApiService
6 | {
7 | public Task ValidateIncomingOrderRequestAsync(OrderRequestEvent @event)
8 | => Task.FromResult(@event.CustomerId!.ToUpper() == "ERROR");
9 |
10 |
11 | public Task ValidateIncomingCustomerValidationResult(CustomerValidationResponseEvent @event)
12 | => Task.FromResult(@event.CustomerType == "Unknown");
13 |
14 |
15 | public Task ValidateIncomingTaxesCalculationResult(TaxesCalculationResponseEvent @event)
16 | => Task.FromResult(@event.ItemId!.ToUpper() == "ERROR");
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/OrdersOrchestrator/Services/IApiService.cs:
--------------------------------------------------------------------------------
1 | using Contracts;
2 |
3 | namespace OrdersOrchestrator.Services
4 | {
5 | public interface IApiService
6 | {
7 | Task ValidateIncomingOrderRequestAsync(OrderRequestEvent @event);
8 | Task ValidateIncomingCustomerValidationResult(CustomerValidationResponseEvent @event);
9 | Task ValidateIncomingTaxesCalculationResult(TaxesCalculationResponseEvent @event);
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/src/OrdersOrchestrator/StateMachines/CustomActivities/CustomerValidationActivity.cs:
--------------------------------------------------------------------------------
1 | using Contracts;
2 | using Contracts.Exceptions;
3 | using MassTransit;
4 | using OrdersOrchestrator.Services;
5 |
6 | namespace OrdersOrchestrator.StateMachines.CustomActivities;
7 |
8 | public sealed class CustomerValidationActivity
9 | : IStateMachineActivity
10 | {
11 | private readonly ITopicProducer taxesCalculationEngineProducer;
12 | private readonly IApiService apiService;
13 |
14 | public CustomerValidationActivity(
15 | ITopicProducer taxesCalculationEngineProducer,
16 | IApiService apiService)
17 | {
18 | this.taxesCalculationEngineProducer = taxesCalculationEngineProducer;
19 | this.apiService = apiService;
20 | }
21 |
22 | async Task IStateMachineActivity.Execute(
23 | BehaviorContext context,
24 | IBehavior next)
25 | {
26 | if (await apiService.ValidateIncomingCustomerValidationResult(context.Message))
27 | {
28 | throw new NotATransientException("Error during the customer validation!");
29 | }
30 |
31 | var taxesCalculationRequestEvent = new TaxesCalculationRequestEvent
32 | {
33 | ItemId = context.Saga.ItemId!,
34 | CustomerType = context.Message.CustomerType
35 | };
36 |
37 | await taxesCalculationEngineProducer
38 | .Produce(
39 | Guid.NewGuid().ToString(),
40 | taxesCalculationRequestEvent,
41 | context.CancellationToken)
42 | .ConfigureAwait(false);
43 |
44 | await next
45 | .Execute(context)
46 | .ConfigureAwait(false);
47 | }
48 |
49 | async Task IStateMachineActivity.Faulted(
50 | BehaviorExceptionContext context,
51 | IBehavior next)
52 | => await next.Faulted(context).ConfigureAwait(false);
53 |
54 | void IProbeSite.Probe(ProbeContext context) => context.CreateScope(nameof(CustomerValidationActivity));
55 | void IVisitable.Accept(StateMachineVisitor visitor) => visitor.Visit(this);
56 | }
57 |
58 |
--------------------------------------------------------------------------------
/src/OrdersOrchestrator/StateMachines/CustomActivities/ProcessFaultActivity.cs:
--------------------------------------------------------------------------------
1 | using Contracts;
2 | using MassTransit;
3 |
4 | namespace OrdersOrchestrator.StateMachines.CustomActivities;
5 |
6 | public sealed class ProcessFaultActivity
7 | : IStateMachineActivity
8 | {
9 | async Task IStateMachineActivity.Execute(
10 | BehaviorContext context,
11 | IBehavior next)
12 | {
13 | context.Saga.NotificationReply = new NotificationReply()
14 | {
15 | Reason = context.Message.ExceptionMessage
16 | };
17 |
18 | await next
19 | .Execute(context)
20 | .ConfigureAwait(false);
21 | }
22 |
23 | async Task IStateMachineActivity.Faulted(
24 | BehaviorExceptionContext context,
25 | IBehavior next)
26 | => await next.Faulted(context).ConfigureAwait(false);
27 |
28 | void IProbeSite.Probe(ProbeContext context) => context.CreateScope(nameof(ProcessFaultActivity));
29 | void IVisitable.Accept(StateMachineVisitor visitor) => visitor.Visit(this);
30 | }
--------------------------------------------------------------------------------
/src/OrdersOrchestrator/StateMachines/CustomActivities/ReceiveOrderRequestActivity.cs:
--------------------------------------------------------------------------------
1 | using Contracts;
2 | using Contracts.Exceptions;
3 | using MassTransit;
4 | using OrdersOrchestrator.Services;
5 |
6 | namespace OrdersOrchestrator.StateMachines.CustomActivities;
7 |
8 | public sealed class ReceiveOrderRequestActivity
9 | : IStateMachineActivity
10 | {
11 | private readonly IApiService apiService;
12 | private readonly ITopicProducer customerValidationEngineProducer;
13 |
14 | public ReceiveOrderRequestActivity(
15 | IApiService apiService,
16 | ITopicProducer customerValidationEngineProducer)
17 | {
18 | this.apiService = apiService;
19 | this.customerValidationEngineProducer = customerValidationEngineProducer;
20 | }
21 |
22 | async Task IStateMachineActivity.Execute(
23 | BehaviorContext context,
24 | IBehavior next)
25 | {
26 | // Some dummy validation to test the error handling flow
27 | if (await apiService
28 | .ValidateIncomingOrderRequestAsync(context.Message)
29 | .ConfigureAwait(false))
30 | {
31 | throw new NotATransientException("Error during order request validation!");
32 | }
33 |
34 | var customerValidationEvent = new
35 | {
36 | context.Message.CustomerId,
37 | };
38 |
39 | await customerValidationEngineProducer
40 | .Produce(
41 | Guid.NewGuid().ToString(),
42 | customerValidationEvent,
43 | context.CancellationToken)
44 | .ConfigureAwait(false);
45 |
46 | await next.Execute(context)
47 | .ConfigureAwait(false);
48 | }
49 |
50 | async Task IStateMachineActivity.Faulted(
51 | BehaviorExceptionContext context,
52 | IBehavior next)
53 | => await next.Faulted(context).ConfigureAwait(false);
54 |
55 | void IProbeSite.Probe(ProbeContext context) => context.CreateScope(nameof(ReceiveOrderRequestActivity));
56 | void IVisitable.Accept(StateMachineVisitor visitor) => visitor.Visit(this);
57 |
58 | }
59 |
60 |
--------------------------------------------------------------------------------
/src/OrdersOrchestrator/StateMachines/CustomActivities/TaxesCalculationActivity.cs:
--------------------------------------------------------------------------------
1 | using Contracts;
2 | using Contracts.Exceptions;
3 | using MassTransit;
4 | using OrdersOrchestrator.Services;
5 |
6 | namespace OrdersOrchestrator.StateMachines.CustomActivities;
7 |
8 | public sealed class TaxesCalculationActivity
9 | : IStateMachineActivity
10 | {
11 | private readonly IApiService apiService;
12 |
13 | public TaxesCalculationActivity(
14 | IApiService apiService)
15 | {
16 | this.apiService = apiService;
17 | }
18 |
19 | async Task IStateMachineActivity.Execute(
20 | BehaviorContext context,
21 | IBehavior next)
22 | {
23 | if (await apiService.ValidateIncomingTaxesCalculationResult(context.Message))
24 | {
25 | throw new NotATransientException("Error during order request validation!");
26 | }
27 |
28 | context.Saga.NotificationReply = new NotificationReply
29 | {
30 | Success = true,
31 | Data = new OrderResponseEvent
32 | {
33 | CustomerId = context.Saga.CustomerId!,
34 | CustomerType = context.Saga.CustomerType!,
35 | TaxesCalculation = context.Message,
36 | }
37 | };
38 |
39 | await next
40 | .Execute(context)
41 | .ConfigureAwait(false);
42 | }
43 |
44 | async Task IStateMachineActivity.Faulted(
45 | BehaviorExceptionContext context,
46 | IBehavior next)
47 | => await next.Faulted(context).ConfigureAwait(false);
48 |
49 | void IProbeSite.Probe(ProbeContext context) => context.CreateScope(nameof(ReceiveOrderRequestActivity));
50 | void IVisitable.Accept(StateMachineVisitor visitor) => visitor.Visit(this);
51 | }
--------------------------------------------------------------------------------
/src/OrdersOrchestrator/StateMachines/OrderRequestSagaDefinition.cs:
--------------------------------------------------------------------------------
1 | using Contracts.Exceptions;
2 | using MassTransit;
3 | using MassTransit.Middleware;
4 | using MassTransit.Transports;
5 | using OrdersOrchestrator.Middlewares;
6 |
7 | namespace OrdersOrchestrator.StateMachines;
8 |
9 | public class OrderRequestSagaDefinition : SagaDefinition
10 | {
11 | public OrderRequestSagaDefinition()
12 | {
13 | // Processing up to 5 messages at time
14 | ConcurrentMessageLimit = 5;
15 | }
16 |
17 | protected override void ConfigureSaga(
18 | IReceiveEndpointConfigurator endpointConfigurator,
19 | ISagaConfigurator sagaConfigurator,
20 | IRegistrationContext context)
21 | {
22 | ArgumentNullException.ThrowIfNull(endpointConfigurator, nameof(endpointConfigurator));
23 | ArgumentNullException.ThrowIfNull(sagaConfigurator, nameof(sagaConfigurator));
24 | ArgumentNullException.ThrowIfNull(context, nameof(context));
25 |
26 | // Retrying unhandled exceptions by the saga state machine
27 | sagaConfigurator.UseMessageRetry(config =>
28 | {
29 | config.Interval(3, 1000);
30 | config.Ignore();
31 | });
32 |
33 | // Configuring a filter for all the registered events in the state machine
34 | sagaConfigurator.UseFilter(new SagaLoggingMiddlewareFilter());
35 |
36 | endpointConfigurator.ConfigureError(x =>
37 | {
38 | x.UseFilters(
39 | new FaultProcessingMiddlewareFilter(context.GetRequiredService()),
40 | new ErrorTransportFilter());
41 | });
42 |
43 | base.ConfigureSaga(endpointConfigurator, sagaConfigurator, context);
44 | }
45 | }
--------------------------------------------------------------------------------
/src/OrdersOrchestrator/StateMachines/OrderRequestSagaInstance.cs:
--------------------------------------------------------------------------------
1 | using Contracts;
2 | using MassTransit;
3 |
4 | namespace OrdersOrchestrator.StateMachines;
5 |
6 | public class OrderRequestSagaInstance : SagaStateMachineInstance, ISagaVersion
7 | {
8 | public string? CurrentState { get; set; }
9 | public string? ItemId { get; set; }
10 | public string? CustomerId { get; set; }
11 | public string? CustomerType { get; set; }
12 | public NotificationReply? NotificationReply { get; set; }
13 | public DateTime CreatedAt { get; set; }
14 | public DateTime UpdatedAt { get; set; }
15 |
16 | // Default props
17 | public Guid CorrelationId { get; set; }
18 | public int Version { get; set; }
19 | }
--------------------------------------------------------------------------------
/src/OrdersOrchestrator/StateMachines/OrderRequestStateMachine.cs:
--------------------------------------------------------------------------------
1 | using Contracts;
2 | using MassTransit;
3 | using OrdersOrchestrator.StateMachines.CustomActivities;
4 |
5 | namespace OrdersOrchestrator.StateMachines;
6 |
7 | public sealed class OrderRequestStateMachine
8 | : MassTransitStateMachine
9 | {
10 | public OrderRequestStateMachine()
11 | {
12 | // Registering known states
13 | RegisterStates();
14 | // Registering and correlating events
15 | CorrelateEvents();
16 |
17 | Initially(
18 | When(OrderRequestedEvent)
19 | .InitializeSaga()
20 | .Activity(config => config.OfType())
21 | .TransitionTo(ValidatingCustomer),
22 | When(FaultEvent)
23 | .Activity(config => config.OfType())
24 | .TransitionTo(Faulted));
25 |
26 | During(ValidatingCustomer,
27 | When(CustomerValidationResponseEvent)
28 | .UpdateSaga()
29 | .Activity(config => config.OfType())
30 | .TransitionTo(CalculatingTaxes),
31 | When(FaultEvent)
32 | .Activity(config => config.OfType())
33 | .TransitionTo(Faulted));
34 |
35 | During(CalculatingTaxes,
36 | When(TaxesCalculationResponseEvent)
37 | .UpdateSaga()
38 | .Activity(config => config.OfType())
39 | .TransitionTo(NotifyingSourceSystem),
40 | When(FaultEvent)
41 | .Activity(config => config.OfType())
42 | .TransitionTo(Faulted));
43 |
44 | WhenEnter(Faulted,
45 | context => context.TransitionTo(NotifyingSourceSystem));
46 |
47 | WhenEnter(NotifyingSourceSystem,
48 | activityCallback => activityCallback
49 | .NotifySourceSystem()
50 | .Finalize());
51 |
52 | During(Final,
53 | Ignore(OrderRequestedEvent),
54 | Ignore(CustomerValidationResponseEvent),
55 | Ignore(TaxesCalculationResponseEvent),
56 | Ignore(FaultEvent));
57 |
58 | // Delete finished saga instances from the repository
59 | SetCompletedWhenFinalized();
60 | }
61 |
62 | private void CorrelateEvents()
63 | {
64 | Event(() => OrderRequestedEvent, x => x
65 | .CorrelateById(m => m.CorrelationId ?? new Guid())
66 | .SelectId(m => m.CorrelationId ?? new Guid())
67 | .OnMissingInstance(m => m.Discard()));
68 |
69 | Event(() => CustomerValidationResponseEvent, x => x
70 | .CorrelateById(m => m.CorrelationId ?? new Guid())
71 | .SelectId(m => m.CorrelationId ?? new Guid())
72 | .OnMissingInstance(m => m.Discard()));
73 |
74 | Event(() => TaxesCalculationResponseEvent, x => x
75 | .CorrelateById(m => m.CorrelationId ?? new Guid())
76 | .SelectId(m => m.CorrelationId ?? new Guid())
77 | .OnMissingInstance(m => m.Discard()));
78 |
79 | Event(() => FaultEvent, x => x
80 | .CorrelateById(m => m.CorrelationId ?? new Guid())
81 | .SelectId(m => m.CorrelationId ?? new Guid())
82 | .OnMissingInstance(m => m.Discard()));
83 | }
84 |
85 | private void RegisterStates()
86 | => InstanceState(x => x.CurrentState);
87 |
88 | public State? ValidatingCustomer { get; set; }
89 | public State? CalculatingTaxes { get; set; }
90 | public State? NotifyingSourceSystem { get; set; }
91 | public State? Faulted { get; set; }
92 | public Event? OrderRequestedEvent { get; set; }
93 | public Event? CustomerValidationResponseEvent { get; set; }
94 | public Event? TaxesCalculationResponseEvent { get; set; }
95 | public Event? FaultEvent { get; set; }
96 | }
--------------------------------------------------------------------------------
/src/OrdersOrchestrator/StateMachines/OrderRequestStateMachineSupportExtensions.cs:
--------------------------------------------------------------------------------
1 | using Contracts;
2 | using MassTransit;
3 | using MassTransit.SagaStateMachine;
4 |
5 | namespace OrdersOrchestrator.StateMachines;
6 |
7 | public static class OrderRequestStateMachineSupportExtensions
8 | {
9 | public static EventActivityBinder InitializeSaga(
10 | this EventActivityBinder binder)
11 | => binder.Then(context =>
12 | {
13 | LogContext.Info?.Log("Order requested: {0}", context.CorrelationId);
14 |
15 | context.Saga.CreatedAt = DateTime.Now;
16 | context.Saga.ItemId = context.Message.ItemId;
17 | context.Saga.CustomerId = context.Message.CustomerId;
18 | });
19 |
20 | public static EventActivityBinder UpdateSaga(
21 | this EventActivityBinder binder)
22 | => binder.Then(context =>
23 | {
24 | LogContext.Info?.Log("Customer validation: {0}", context.CorrelationId);
25 |
26 | context.Saga.CustomerType = context.Message.CustomerType;
27 | context.Saga.UpdatedAt = DateTime.Now;
28 | });
29 |
30 | public static EventActivityBinder UpdateSaga(
31 | this EventActivityBinder binder)
32 | => binder.Then(context =>
33 | {
34 | LogContext.Info?.Log("Taxes calculation: {0}", context.CorrelationId);
35 |
36 | context.Saga.UpdatedAt = DateTime.Now;
37 | });
38 |
39 | public static EventActivityBinder NotifySourceSystem(
40 | this EventActivityBinder binder)
41 | {
42 | Func, Task> asyncAction = async context =>
43 | {
44 | LogContext.Info?.Log("Notifying source system: {0}", context.CorrelationId);
45 |
46 | await context.GetServiceOrCreateInstance>>()
47 | .Produce(
48 | Guid.NewGuid().ToString(),
49 | context.Saga.NotificationReply!,
50 | context.CancellationToken)
51 | .ConfigureAwait(false);
52 | };
53 |
54 | return binder.Add(new AsyncActivity(asyncAction));
55 | }
56 | }
--------------------------------------------------------------------------------
/src/OrdersOrchestrator/Transports/FaultTransport.cs:
--------------------------------------------------------------------------------
1 | using Contracts;
2 | using MassTransit;
3 | using MassTransit.Transports;
4 |
5 | namespace OrdersOrchestrator.Transports;
6 |
7 | public sealed class FaultTransport : IErrorTransport
8 | {
9 | private readonly IServiceScopeFactory serviceScopeFactory;
10 |
11 | public FaultTransport(IServiceScopeFactory serviceScopeFactory)
12 | {
13 | this.serviceScopeFactory = serviceScopeFactory;
14 | }
15 | async Task IErrorTransport.Send(ExceptionReceiveContext context)
16 | {
17 | ArgumentNullException.ThrowIfNull(context, nameof(context));
18 |
19 | var consumeContext = context.GetPayload();
20 |
21 | using var scope = serviceScopeFactory.CreateScope();
22 | var faultTopicProducer = scope
23 | .ServiceProvider
24 | .GetRequiredService>();
25 |
26 | consumeContext.TryGetMessage