├── .gitignore ├── README.md ├── TimeOff.Core ├── ApplicationConstants.cs ├── ConfigReader.cs ├── KafkaAvroAsyncSerializer.cs ├── KafkaAvroDeserializer.cs ├── KafkaHelper.cs └── TimeOff.Core.csproj ├── TimeOff.Employee ├── Program.cs ├── Properties │ └── launchSettings.json ├── TimeOff.Employee.csproj └── appsettings.json ├── TimeOff.Manager ├── Program.cs ├── Properties │ └── launchSettings.json ├── TimeOff.Manager.csproj └── appsettings.json ├── TimeOff.Models ├── Departments.cs ├── LeaveApplicationProcessed.avsc ├── LeaveApplicationProcessed.cs ├── LeaveApplicationReceived.avsc ├── LeaveApplicationReceived.cs └── TimeOff.Models.csproj ├── TimeOff.ResultReader ├── Program.cs ├── Properties │ └── launchSettings.json ├── TimeOff.ResultReader.csproj └── appsettings.json ├── TimeOff.sln ├── TimeOff.sln.DotSettings └── docker-compose.yaml /.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/master/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 | *.vspscc 94 | *.vssscc 95 | .builds 96 | *.pidb 97 | *.svclog 98 | *.scc 99 | 100 | # Chutzpah Test files 101 | _Chutzpah* 102 | 103 | # Visual C++ cache files 104 | ipch/ 105 | *.aps 106 | *.ncb 107 | *.opendb 108 | *.opensdf 109 | *.sdf 110 | *.cachefile 111 | *.VC.db 112 | *.VC.VC.opendb 113 | 114 | # Visual Studio profiler 115 | *.psess 116 | *.vsp 117 | *.vspx 118 | *.sap 119 | 120 | # Visual Studio Trace Files 121 | *.e2e 122 | 123 | # TFS 2012 Local Workspace 124 | $tf/ 125 | 126 | # Guidance Automation Toolkit 127 | *.gpState 128 | 129 | # ReSharper is a .NET coding add-in 130 | _ReSharper*/ 131 | *.[Rr]e[Ss]harper 132 | *.DotSettings.user 133 | 134 | # TeamCity is a build add-in 135 | _TeamCity* 136 | 137 | # DotCover is a Code Coverage Tool 138 | *.dotCover 139 | 140 | # AxoCover is a Code Coverage Tool 141 | .axoCover/* 142 | !.axoCover/settings.json 143 | 144 | # Coverlet is a free, cross platform Code Coverage Tool 145 | coverage*.json 146 | coverage*.xml 147 | coverage*.info 148 | 149 | # Visual Studio code coverage results 150 | *.coverage 151 | *.coveragexml 152 | 153 | # NCrunch 154 | _NCrunch_* 155 | .*crunch*.local.xml 156 | nCrunchTemp_* 157 | 158 | # MightyMoose 159 | *.mm.* 160 | AutoTest.Net/ 161 | 162 | # Web workbench (sass) 163 | .sass-cache/ 164 | 165 | # Installshield output folder 166 | [Ee]xpress/ 167 | 168 | # DocProject is a documentation generator add-in 169 | DocProject/buildhelp/ 170 | DocProject/Help/*.HxT 171 | DocProject/Help/*.HxC 172 | DocProject/Help/*.hhc 173 | DocProject/Help/*.hhk 174 | DocProject/Help/*.hhp 175 | DocProject/Help/Html2 176 | DocProject/Help/html 177 | 178 | # Click-Once directory 179 | publish/ 180 | 181 | # Publish Web Output 182 | *.[Pp]ublish.xml 183 | *.azurePubxml 184 | # Note: Comment the next line if you want to checkin your web deploy settings, 185 | # but database connection strings (with potential passwords) will be unencrypted 186 | *.pubxml 187 | *.publishproj 188 | 189 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 190 | # checkin your Azure Web App publish settings, but sensitive information contained 191 | # in these scripts will be unencrypted 192 | PublishScripts/ 193 | 194 | # NuGet Packages 195 | *.nupkg 196 | # NuGet Symbol Packages 197 | *.snupkg 198 | # The packages folder can be ignored because of Package Restore 199 | **/[Pp]ackages/* 200 | # except build/, which is used as an MSBuild target. 201 | !**/[Pp]ackages/build/ 202 | # Uncomment if necessary however generally it will be regenerated when needed 203 | #!**/[Pp]ackages/repositories.config 204 | # NuGet v3's project.json files produces more ignorable files 205 | *.nuget.props 206 | *.nuget.targets 207 | 208 | # Microsoft Azure Build Output 209 | csx/ 210 | *.build.csdef 211 | 212 | # Microsoft Azure Emulator 213 | ecf/ 214 | rcf/ 215 | 216 | # Windows Store app package directories and files 217 | AppPackages/ 218 | BundleArtifacts/ 219 | Package.StoreAssociation.xml 220 | _pkginfo.txt 221 | *.appx 222 | *.appxbundle 223 | *.appxupload 224 | 225 | # Visual Studio cache files 226 | # files ending in .cache can be ignored 227 | *.[Cc]ache 228 | # but keep track of directories ending in .cache 229 | !?*.[Cc]ache/ 230 | 231 | # Others 232 | ClientBin/ 233 | ~$* 234 | *~ 235 | *.dbmdl 236 | *.dbproj.schemaview 237 | *.jfm 238 | *.pfx 239 | *.publishsettings 240 | orleans.codegen.cs 241 | 242 | # Including strong name files can present a security risk 243 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 244 | #*.snk 245 | 246 | # Since there are multiple workflows, uncomment next line to ignore bower_components 247 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 248 | #bower_components/ 249 | 250 | # RIA/Silverlight projects 251 | Generated_Code/ 252 | 253 | # Backup & report files from converting an old project file 254 | # to a newer Visual Studio version. Backup files are not needed, 255 | # because we have git ;-) 256 | _UpgradeReport_Files/ 257 | Backup*/ 258 | UpgradeLog*.XML 259 | UpgradeLog*.htm 260 | ServiceFabricBackup/ 261 | *.rptproj.bak 262 | 263 | # SQL Server files 264 | *.mdf 265 | *.ldf 266 | *.ndf 267 | 268 | # Business Intelligence projects 269 | *.rdl.data 270 | *.bim.layout 271 | *.bim_*.settings 272 | *.rptproj.rsuser 273 | *- [Bb]ackup.rdl 274 | *- [Bb]ackup ([0-9]).rdl 275 | *- [Bb]ackup ([0-9][0-9]).rdl 276 | 277 | # Microsoft Fakes 278 | FakesAssemblies/ 279 | 280 | # GhostDoc plugin setting file 281 | *.GhostDoc.xml 282 | 283 | # Node.js Tools for Visual Studio 284 | .ntvs_analysis.dat 285 | node_modules/ 286 | 287 | # Visual Studio 6 build log 288 | *.plg 289 | 290 | # Visual Studio 6 workspace options file 291 | *.opt 292 | 293 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 294 | *.vbw 295 | 296 | # Visual Studio LightSwitch build output 297 | **/*.HTMLClient/GeneratedArtifacts 298 | **/*.DesktopClient/GeneratedArtifacts 299 | **/*.DesktopClient/ModelManifest.xml 300 | **/*.Server/GeneratedArtifacts 301 | **/*.Server/ModelManifest.xml 302 | _Pvt_Extensions 303 | 304 | # Paket dependency manager 305 | .paket/paket.exe 306 | paket-files/ 307 | 308 | # FAKE - F# Make 309 | .fake/ 310 | 311 | # CodeRush personal settings 312 | .cr/personal 313 | 314 | # Python Tools for Visual Studio (PTVS) 315 | __pycache__/ 316 | *.pyc 317 | 318 | # Cake - Uncomment if you are using it 319 | # tools/** 320 | # !tools/packages.config 321 | 322 | # Tabs Studio 323 | *.tss 324 | 325 | # Telerik's JustMock configuration file 326 | *.jmconfig 327 | 328 | # BizTalk build output 329 | *.btp.cs 330 | *.btm.cs 331 | *.odx.cs 332 | *.xsd.cs 333 | 334 | # OpenCover UI analysis results 335 | OpenCover/ 336 | 337 | # Azure Stream Analytics local run output 338 | ASALocalRun/ 339 | 340 | # MSBuild Binary and Structured Log 341 | *.binlog 342 | 343 | # NVidia Nsight GPU debugger configuration file 344 | *.nvuser 345 | 346 | # MFractors (Xamarin productivity tool) working folder 347 | .mfractor/ 348 | 349 | # Local History for Visual Studio 350 | .localhistory/ 351 | 352 | # BeatPulse healthcheck temp database 353 | healthchecksdb 354 | 355 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 356 | MigrationBackup/ 357 | 358 | # Ionide (cross platform F# VS Code tools) working folder 359 | .ionide/ 360 | 361 | # Fody - auto-generated XML schema 362 | FodyWeavers.xsd -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Event Driven Architecture with Apache Kafka for .NET Developers 2 | 3 | Sample application used in my blog post: https://thecloudblog.net/post/event-driven-architecture-with-apache-kafka-for-net-developers-part-1-event-producer/ 4 | -------------------------------------------------------------------------------- /TimeOff.Core/ApplicationConstants.cs: -------------------------------------------------------------------------------- 1 | namespace TimeOff.Core 2 | { 3 | public static class ApplicationConstants 4 | { 5 | public const string LeaveApplicationsTopicName = "leave-applications"; 6 | public const string LeaveApplicationResultsTopicName = "leave-applications-results"; 7 | } 8 | } -------------------------------------------------------------------------------- /TimeOff.Core/ConfigReader.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Net; 3 | using Confluent.Kafka; 4 | using Confluent.SchemaRegistry; 5 | using Microsoft.Extensions.Configuration; 6 | 7 | namespace TimeOff.Core 8 | { 9 | public class ConfigReader 10 | { 11 | private readonly IConfiguration _configuration; 12 | 13 | public ConfigReader(IConfiguration configuration) 14 | { 15 | _configuration = configuration; 16 | IsLocalEnvironment = Convert.ToBoolean(configuration[nameof(IsLocalEnvironment)]); 17 | } 18 | 19 | public bool IsLocalEnvironment { get; } 20 | 21 | public ProducerConfig GetProducerConfig() 22 | { 23 | var config = IsLocalEnvironment 24 | ? _configuration.GetSection(nameof(ProducerConfig)).Get() 25 | : _configuration.GetSection("ProducerConfigAz").Get(); 26 | 27 | config.ClientId = Dns.GetHostName(); 28 | if (IsLocalEnvironment) 29 | { 30 | return config; 31 | } 32 | 33 | config.SecurityProtocol = SecurityProtocol.SaslSsl; 34 | config.SaslMechanism = SaslMechanism.Plain; 35 | return config; 36 | } 37 | 38 | public AdminClientConfig GetAdminConfig() 39 | { 40 | return _configuration.GetSection(nameof(AdminClientConfig)).Get(); 41 | } 42 | 43 | public SchemaRegistryConfig GetSchemaRegistryConfig() 44 | { 45 | return _configuration.GetSection(nameof(SchemaRegistryConfig)).Get(); 46 | } 47 | 48 | public ConsumerConfig GetConsumerConfig() 49 | { 50 | var config = IsLocalEnvironment 51 | ? _configuration.GetSection(nameof(ConsumerConfig)).Get() 52 | : _configuration.GetSection("ConsumerConfigAz").Get(); 53 | 54 | // Read messages from start if no commit exists. 55 | config.AutoOffsetReset = AutoOffsetReset.Earliest; 56 | 57 | if (IsLocalEnvironment) 58 | { 59 | return config; 60 | } 61 | 62 | config.SecurityProtocol = SecurityProtocol.SaslSsl; 63 | config.SaslMechanism = SaslMechanism.Plain; 64 | return config; 65 | } 66 | } 67 | } -------------------------------------------------------------------------------- /TimeOff.Core/KafkaAvroAsyncSerializer.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | using System.Text; 3 | using System.Threading; 4 | using System.Threading.Tasks; 5 | using Azure.Data.SchemaRegistry; 6 | using Confluent.Kafka; 7 | using Microsoft.Azure.Data.SchemaRegistry.ApacheAvro; 8 | 9 | namespace TimeOff.Core 10 | { 11 | public class KafkaAvroAsyncSerializer : IAsyncSerializer 12 | { 13 | private readonly SchemaRegistryAvroObjectSerializer _serializer; 14 | 15 | public KafkaAvroAsyncSerializer(SchemaRegistryClient schemaRegistryClient, string schemaGroup, 16 | bool autoRegisterSchemas = true) 17 | { 18 | _serializer = new SchemaRegistryAvroObjectSerializer( 19 | schemaRegistryClient, 20 | schemaGroup, 21 | new SchemaRegistryAvroObjectSerializerOptions 22 | { 23 | AutoRegisterSchemas = autoRegisterSchemas 24 | }); 25 | } 26 | 27 | public async Task SerializeAsync(T data, SerializationContext context) 28 | { 29 | if (data == null) 30 | { 31 | return null; 32 | } 33 | 34 | // SchemaRegistryAvroObjectSerializer can only serialize GenericRecord or ISpecificRecord. 35 | if (data is string s) 36 | { 37 | return Encoding.ASCII.GetBytes(s); 38 | } 39 | 40 | await using var stream = new MemoryStream(); 41 | await _serializer.SerializeAsync(stream, data, typeof(T), CancellationToken.None); 42 | return stream.ToArray(); 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /TimeOff.Core/KafkaAvroDeserializer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Text; 4 | using System.Threading; 5 | using Azure.Data.SchemaRegistry; 6 | using Confluent.Kafka; 7 | using Microsoft.Azure.Data.SchemaRegistry.ApacheAvro; 8 | 9 | namespace TimeOff.Core 10 | { 11 | public class KafkaAvroDeserializer : IDeserializer 12 | { 13 | private readonly SchemaRegistryAvroObjectSerializer _serializer; 14 | 15 | public KafkaAvroDeserializer(SchemaRegistryClient schemaRegistryClient, string schemaGroup) 16 | { 17 | _serializer = new SchemaRegistryAvroObjectSerializer(schemaRegistryClient, schemaGroup); 18 | } 19 | 20 | public T Deserialize(ReadOnlySpan data, bool isNull, SerializationContext context) 21 | { 22 | if (data.IsEmpty) 23 | { 24 | return default; 25 | } 26 | 27 | // SchemaRegistryAvroObjectSerializer can only serialize GenericRecord or ISpecificRecord. 28 | if (typeof(T) == typeof(string)) 29 | { 30 | return (T) Convert.ChangeType(Encoding.ASCII.GetString(data.ToArray()), typeof(T)); 31 | } 32 | 33 | return (T) _serializer.Deserialize(new MemoryStream(data.ToArray()), typeof(T), CancellationToken.None); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /TimeOff.Core/KafkaHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Threading.Tasks; 4 | using Confluent.Kafka; 5 | using Confluent.Kafka.Admin; 6 | 7 | namespace TimeOff.Core 8 | { 9 | public static class KafkaHelper 10 | { 11 | public static async Task CreateTopicAsync(AdminClientConfig config, string topicName, int partitionCount) 12 | { 13 | using var adminClient = new AdminClientBuilder(config).Build(); 14 | try 15 | { 16 | await adminClient.CreateTopicsAsync(new[] 17 | { 18 | new TopicSpecification 19 | { 20 | Name = topicName, 21 | ReplicationFactor = 1, 22 | NumPartitions = partitionCount 23 | } 24 | }); 25 | } 26 | catch (CreateTopicsException e) when (e.Results.Select(r => r.Error.Code) 27 | .Any(el => el == ErrorCode.TopicAlreadyExists)) 28 | { 29 | Console.WriteLine($"Topic {e.Results[0].Topic} already exists"); 30 | } 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /TimeOff.Core/TimeOff.Core.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net5.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /TimeOff.Employee/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Globalization; 3 | using System.Threading.Tasks; 4 | using Azure.Data.SchemaRegistry; 5 | using Azure.Identity; 6 | using Confluent.Kafka; 7 | using Confluent.SchemaRegistry; 8 | using Confluent.SchemaRegistry.Serdes; 9 | using Microsoft.Extensions.Configuration; 10 | using TimeOff.Core; 11 | using TimeOff.Models; 12 | 13 | namespace TimeOff.Employee 14 | { 15 | internal class Program 16 | { 17 | private static AdminClientConfig _adminConfig; 18 | private static SchemaRegistryConfig _schemaRegistryConfig; 19 | private static ProducerConfig _producerConfig; 20 | private static ConfigReader _configReader; 21 | 22 | public static IConfiguration Configuration { get; private set; } 23 | 24 | private static async Task Main() 25 | { 26 | Console.WriteLine("TimeOff Employee Terminal\n"); 27 | 28 | Configuration = new ConfigurationBuilder() 29 | .AddJsonFile("appsettings.json", true, true) 30 | .Build(); 31 | 32 | _configReader = new ConfigReader(Configuration); 33 | 34 | // Read configs 35 | _schemaRegistryConfig = _configReader.GetSchemaRegistryConfig(); 36 | _producerConfig = _configReader.GetProducerConfig(); 37 | 38 | // Azure EH does not support Kafka Admin APIs. 39 | if (_configReader.IsLocalEnvironment) 40 | { 41 | _adminConfig = _configReader.GetAdminConfig(); 42 | await KafkaHelper.CreateTopicAsync(_adminConfig, ApplicationConstants.LeaveApplicationsTopicName, 3); 43 | } 44 | 45 | await AddMessagesAsync(); 46 | } 47 | 48 | 49 | private static async Task AddMessagesAsync() 50 | { 51 | CachedSchemaRegistryClient cachedSchemaRegistryClient = null; 52 | KafkaAvroAsyncSerializer kafkaAvroAsyncKeySerializer = null; 53 | KafkaAvroAsyncSerializer kafkaAvroAsyncValueSerializer = null; 54 | 55 | if (_configReader.IsLocalEnvironment) 56 | { 57 | cachedSchemaRegistryClient = new CachedSchemaRegistryClient(_schemaRegistryConfig); 58 | } 59 | else 60 | { 61 | var schemaRegistryClientAz = 62 | new SchemaRegistryClient(Configuration["SchemaRegistryUrlAz"], new DefaultAzureCredential()); 63 | var schemaGroupName = Configuration["SchemaRegistryGroupNameAz"]; 64 | kafkaAvroAsyncKeySerializer = 65 | new KafkaAvroAsyncSerializer(schemaRegistryClientAz, schemaGroupName); 66 | kafkaAvroAsyncValueSerializer = 67 | new KafkaAvroAsyncSerializer(schemaRegistryClientAz, schemaGroupName); 68 | } 69 | 70 | using var producer = new ProducerBuilder(_producerConfig) 71 | .SetKeySerializer(_configReader.IsLocalEnvironment 72 | ? new AvroSerializer(cachedSchemaRegistryClient) 73 | : kafkaAvroAsyncKeySerializer) 74 | .SetValueSerializer(_configReader.IsLocalEnvironment 75 | ? new AvroSerializer(cachedSchemaRegistryClient) 76 | : kafkaAvroAsyncValueSerializer) 77 | .Build(); 78 | while (true) 79 | { 80 | var empEmail = ReadLine.Read("Enter your employee Email (e.g. none@example-company.com): ", 81 | "none@example.com").ToLowerInvariant(); 82 | var empDepartment = ReadLine.Read("Enter your department code (HR, IT, OPS): ").ToUpperInvariant(); 83 | var leaveDurationInHours = 84 | int.Parse(ReadLine.Read("Enter number of hours of leave requested (e.g. 8): ", "8")); 85 | var leaveStartDate = DateTime.ParseExact(ReadLine.Read("Enter vacation start date (dd-mm-yy): ", 86 | $"{DateTime.Today:dd-mm-yy}"), "dd-mm-yy", CultureInfo.InvariantCulture); 87 | 88 | var leaveApplication = new LeaveApplicationReceived 89 | { 90 | EmpDepartment = empDepartment, 91 | EmpEmail = empEmail, 92 | LeaveDurationInHours = leaveDurationInHours, 93 | LeaveStartDateTicks = leaveStartDate.Ticks 94 | }; 95 | var partition = new TopicPartition( 96 | ApplicationConstants.LeaveApplicationsTopicName, 97 | new Partition((int) Enum.Parse(empDepartment))); 98 | var result = await producer.ProduceAsync(partition, 99 | new Message 100 | { 101 | Key = $"{empEmail}-{DateTime.UtcNow.Ticks}", 102 | Value = leaveApplication 103 | }); 104 | Console.WriteLine( 105 | $"\nMsg: Your leave request is queued at offset {result.Offset.Value} in the Topic {result.Topic}:{result.Partition.Value}\n\n"); 106 | } 107 | 108 | // ReSharper disable once FunctionNeverReturns 109 | } 110 | } 111 | } -------------------------------------------------------------------------------- /TimeOff.Employee/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "TimeOff.Employee": { 4 | "commandName": "Project", 5 | "environmentVariables": { 6 | "AZURE_CLIENT_SECRET": "CLIENT SECRET", 7 | "AZURE_CLIENT_ID": "CLIENT ID", 8 | "AZURE_TENANT_ID": "TENANT ID" 9 | } 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /TimeOff.Employee/TimeOff.Employee.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | Always 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /TimeOff.Employee/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "IsLocalEnvironment": false, 3 | "ProducerConfig": { 4 | "BootstrapServers": "127.0.0.1:9092", 5 | "EnableDeliveryReports": true 6 | }, 7 | "ProducerConfigAz": { 8 | "BootstrapServers": ".servicebus.windows.net:9093", 9 | "EnableDeliveryReports": true, 10 | "SaslUsername": "$ConnectionString", 11 | "SaslPassword": "CONNECTION STRING" 12 | }, 13 | "AdminClientConfig": { 14 | "BootstrapServers": "127.0.0.1:9092" 15 | }, 16 | "SchemaRegistryConfig": { 17 | "Url": "http://127.0.0.1:8081" 18 | }, 19 | "SchemaRegistryUrlAz": ".servicebus.windows.net", 20 | "SchemaRegistryGroupNameAz": "" 21 | } -------------------------------------------------------------------------------- /TimeOff.Manager/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text.Json; 5 | using System.Threading; 6 | using System.Threading.Tasks; 7 | using Azure.Data.SchemaRegistry; 8 | using Azure.Identity; 9 | using Confluent.Kafka; 10 | using Confluent.Kafka.SyncOverAsync; 11 | using Confluent.SchemaRegistry; 12 | using Confluent.SchemaRegistry.Serdes; 13 | using Microsoft.Extensions.Configuration; 14 | using TimeOff.Core; 15 | using TimeOff.Models; 16 | 17 | namespace TimeOff.Manager 18 | { 19 | public record KafkaMessage(string Key, int Partition, LeaveApplicationReceived Message); 20 | 21 | internal class Program 22 | { 23 | private static ConfigReader _configReader; 24 | private static AdminClientConfig _adminConfig; 25 | private static SchemaRegistryConfig _schemaRegistryConfig; 26 | private static ConsumerConfig _consumerConfig; 27 | private static ProducerConfig _producerConfig; 28 | 29 | private static Queue _leaveApplicationReceivedMessages; 30 | 31 | public static IConfiguration Configuration { get; private set; } 32 | 33 | private static async Task Main() 34 | { 35 | Console.WriteLine("TimeOff Manager Terminal\n"); 36 | 37 | Configuration = new ConfigurationBuilder() 38 | .AddJsonFile("appsettings.json", true, true) 39 | .Build(); 40 | 41 | _configReader = new ConfigReader(Configuration); 42 | 43 | // Read configs 44 | _schemaRegistryConfig = _configReader.GetSchemaRegistryConfig(); 45 | _consumerConfig = _configReader.GetConsumerConfig(); 46 | _producerConfig = _configReader.GetProducerConfig(); 47 | 48 | // Azure EH does not support Kafka Admin APIs. 49 | if (_configReader.IsLocalEnvironment) 50 | { 51 | _adminConfig = _configReader.GetAdminConfig(); 52 | await KafkaHelper.CreateTopicAsync(_adminConfig, ApplicationConstants.LeaveApplicationResultsTopicName, 53 | 1); 54 | } 55 | 56 | _leaveApplicationReceivedMessages = new Queue(); 57 | await Task.WhenAny(Task.Run(StartManagerConsumer), Task.Run(StartLeaveApplicationProcessor)); 58 | } 59 | 60 | private static async Task StartLeaveApplicationProcessor() 61 | { 62 | while (true) 63 | { 64 | if (!_leaveApplicationReceivedMessages.Any()) 65 | { 66 | Thread.Sleep(TimeSpan.FromSeconds(1)); 67 | continue; 68 | } 69 | 70 | var (key, partition, leaveApplication) = _leaveApplicationReceivedMessages.Dequeue(); 71 | Console.WriteLine( 72 | $"Received message: {key} from partition: {partition} Value: {JsonSerializer.Serialize(leaveApplication)}"); 73 | 74 | // Make decision on leave request. 75 | var isApproved = ReadLine.Read("Approve request? (Y/N): ", "Y") 76 | .Equals("Y", StringComparison.OrdinalIgnoreCase); 77 | await SendMessageToResultTopicAsync(leaveApplication, isApproved, partition); 78 | } 79 | 80 | // ReSharper disable once FunctionNeverReturns 81 | } 82 | 83 | private static Task StartManagerConsumer() 84 | { 85 | CachedSchemaRegistryClient cachedSchemaRegistryClient = null; 86 | KafkaAvroDeserializer kafkaAvroKeyDeserializer = null; 87 | KafkaAvroDeserializer kafkaAvroValueDeserializer = null; 88 | 89 | if (_configReader.IsLocalEnvironment) 90 | { 91 | cachedSchemaRegistryClient = new CachedSchemaRegistryClient(_schemaRegistryConfig); 92 | } 93 | else 94 | { 95 | var schemaRegistryClientAz = 96 | new SchemaRegistryClient(Configuration["SchemaRegistryUrlAz"], new DefaultAzureCredential()); 97 | var schemaGroupName = Configuration["SchemaRegistryGroupNameAz"]; 98 | kafkaAvroKeyDeserializer = 99 | new KafkaAvroDeserializer(schemaRegistryClientAz, schemaGroupName); 100 | kafkaAvroValueDeserializer = 101 | new KafkaAvroDeserializer(schemaRegistryClientAz, schemaGroupName); 102 | } 103 | 104 | using var consumer = new ConsumerBuilder(_consumerConfig) 105 | .SetKeyDeserializer( 106 | _configReader.IsLocalEnvironment 107 | ? new AvroDeserializer(cachedSchemaRegistryClient).AsSyncOverAsync() 108 | : kafkaAvroKeyDeserializer) 109 | .SetValueDeserializer(_configReader.IsLocalEnvironment 110 | ? new AvroDeserializer(cachedSchemaRegistryClient).AsSyncOverAsync() 111 | : kafkaAvroValueDeserializer) 112 | .SetErrorHandler((_, e) => Console.WriteLine($"Error: {e.Reason}")) 113 | .Build(); 114 | { 115 | try 116 | { 117 | consumer.Subscribe(ApplicationConstants.LeaveApplicationsTopicName); 118 | Console.WriteLine("Consumer loop started...\n"); 119 | while (true) 120 | { 121 | try 122 | { 123 | var result = 124 | consumer.Consume( 125 | TimeSpan.FromMilliseconds(_consumerConfig.MaxPollIntervalMs - 1000 ?? 250000)); 126 | var leaveRequest = result?.Message?.Value; 127 | if (leaveRequest == null) 128 | { 129 | continue; 130 | } 131 | 132 | // Adding message to a list just for the demo. 133 | // You should persist the message in database and process it later. 134 | _leaveApplicationReceivedMessages.Enqueue(new KafkaMessage(result.Message.Key, 135 | result.Partition.Value, result.Message.Value)); 136 | 137 | consumer.Commit(result); 138 | consumer.StoreOffset(result); 139 | } 140 | catch (ConsumeException e) when (!e.Error.IsFatal) 141 | { 142 | Console.WriteLine($"Non fatal error: {e}"); 143 | } 144 | } 145 | } 146 | finally 147 | { 148 | consumer.Close(); 149 | } 150 | } 151 | } 152 | 153 | private static async Task SendMessageToResultTopicAsync(LeaveApplicationReceived leaveRequest, bool isApproved, 154 | int partitionId 155 | ) 156 | { 157 | CachedSchemaRegistryClient cachedSchemaRegistryClient = null; 158 | KafkaAvroAsyncSerializer kafkaAvroAsyncKeySerializer = null; 159 | KafkaAvroAsyncSerializer kafkaAvroAsyncValueSerializer = null; 160 | 161 | if (_configReader.IsLocalEnvironment) 162 | { 163 | cachedSchemaRegistryClient = new CachedSchemaRegistryClient(_schemaRegistryConfig); 164 | } 165 | else 166 | { 167 | var schemaRegistryClientAz = 168 | new SchemaRegistryClient(Configuration["SchemaRegistryUrlAz"], new DefaultAzureCredential()); 169 | var schemaGroupName = Configuration["SchemaRegistryGroupNameAz"]; 170 | kafkaAvroAsyncKeySerializer = 171 | new KafkaAvroAsyncSerializer(schemaRegistryClientAz, schemaGroupName); 172 | kafkaAvroAsyncValueSerializer = 173 | new KafkaAvroAsyncSerializer(schemaRegistryClientAz, schemaGroupName); 174 | } 175 | 176 | using var producer = new ProducerBuilder(_producerConfig) 177 | .SetKeySerializer(_configReader.IsLocalEnvironment 178 | ? new AvroSerializer(cachedSchemaRegistryClient) 179 | : kafkaAvroAsyncKeySerializer) 180 | .SetValueSerializer(_configReader.IsLocalEnvironment 181 | ? new AvroSerializer(cachedSchemaRegistryClient) 182 | : kafkaAvroAsyncValueSerializer) 183 | .Build(); 184 | { 185 | var leaveApplicationResult = new LeaveApplicationProcessed 186 | { 187 | EmpDepartment = leaveRequest.EmpDepartment, 188 | EmpEmail = leaveRequest.EmpEmail, 189 | LeaveDurationInHours = leaveRequest.LeaveDurationInHours, 190 | LeaveStartDateTicks = leaveRequest.LeaveStartDateTicks, 191 | ProcessedBy = $"Manager #{partitionId}", 192 | Result = isApproved 193 | ? "Approved: Your leave application has been approved." 194 | : "Declined: Your leave application has been declined." 195 | }; 196 | 197 | var result = await producer.ProduceAsync(ApplicationConstants.LeaveApplicationResultsTopicName, 198 | new Message 199 | { 200 | Key = $"{leaveRequest.EmpEmail}-{DateTime.UtcNow.Ticks}", 201 | Value = leaveApplicationResult 202 | }); 203 | Console.WriteLine( 204 | $"\nMsg: Leave request processed and queued at offset {result.Offset.Value} in the Topic {result.Topic}"); 205 | } 206 | } 207 | } 208 | } -------------------------------------------------------------------------------- /TimeOff.Manager/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "TimeOff.Manager": { 4 | "commandName": "Project", 5 | "environmentVariables": { 6 | "AZURE_CLIENT_SECRET": "CLIENT SECRET", 7 | "AZURE_CLIENT_ID": "CLIENT ID", 8 | "AZURE_TENANT_ID": "TENANT ID" 9 | } 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /TimeOff.Manager/TimeOff.Manager.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | Always 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /TimeOff.Manager/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "IsLocalEnvironment": false, 3 | "SchemaRegistryConfig": { 4 | "Url": "http://127.0.0.1:8081" 5 | }, 6 | "SchemaRegistryUrlAz": ".servicebus.windows.net", 7 | "SchemaRegistryGroupNameAz": "", 8 | "ConsumerConfig": { 9 | "BootstrapServers": "127.0.0.1:9092", 10 | "GroupId": "manager", 11 | "EnableAutoCommit": false, 12 | "EnableAutoOffsetStore": false, 13 | "MaxPollIntervalMs": 300000 14 | }, 15 | "ConsumerConfigAz": { 16 | "BootstrapServers": ".servicebus.windows.net:9093", 17 | "GroupId": "manager", 18 | "EnableAutoCommit": false, 19 | "EnableAutoOffsetStore": false, 20 | "MaxPollIntervalMs": 300000, 21 | "SaslUsername": "$ConnectionString", 22 | "SaslPassword": "CONNECTION STRING" 23 | }, 24 | "AdminClientConfig": { 25 | "BootstrapServers": "127.0.0.1:9092" 26 | }, 27 | "ProducerConfig": { 28 | "BootstrapServers": "127.0.0.1:9092", 29 | "EnableDeliveryReports": true 30 | }, 31 | "ProducerConfigAz": { 32 | "BootstrapServers": ".servicebus.windows.net:9093", 33 | "EnableDeliveryReports": true, 34 | "SaslUsername": "$ConnectionString", 35 | "SaslPassword": "CONNECTION STRING" 36 | } 37 | } -------------------------------------------------------------------------------- /TimeOff.Models/Departments.cs: -------------------------------------------------------------------------------- 1 | namespace TimeOff.Models 2 | { 3 | public enum Departments : byte 4 | { 5 | HR = 0, 6 | IT = 1, 7 | OPS = 2 8 | } 9 | } -------------------------------------------------------------------------------- /TimeOff.Models/LeaveApplicationProcessed.avsc: -------------------------------------------------------------------------------- 1 | { 2 | "namespace": "TimeOff.Models", 3 | "type": "record", 4 | "name": "LeaveApplicationProcessed", 5 | "fields": [ 6 | { 7 | "name": "EmpEmail", 8 | "type": "string" 9 | }, 10 | { 11 | "name": "EmpDepartment", 12 | "type": "string" 13 | }, 14 | { 15 | "name": "LeaveDurationInHours", 16 | "type": "int" 17 | }, 18 | { 19 | "name": "LeaveStartDateTicks", 20 | "type": "long" 21 | }, 22 | { 23 | "name": "ProcessedBy", 24 | "type": "string" 25 | }, 26 | { 27 | "name": "Result", 28 | "type": "string" 29 | } 30 | ] 31 | } -------------------------------------------------------------------------------- /TimeOff.Models/LeaveApplicationProcessed.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------------------------ 2 | // 3 | // Generated by avrogen, version 1.10.0.0 4 | // Changes to this file may cause incorrect behavior and will be lost if code 5 | // is regenerated 6 | // 7 | // ------------------------------------------------------------------------------ 8 | namespace TimeOff.Models 9 | { 10 | using System; 11 | using System.Collections.Generic; 12 | using System.Text; 13 | using Avro; 14 | using Avro.Specific; 15 | 16 | public partial class LeaveApplicationProcessed : ISpecificRecord 17 | { 18 | public static Schema _SCHEMA = Avro.Schema.Parse(@"{""type"":""record"",""name"":""LeaveApplicationProcessed"",""namespace"":""TimeOff.Models"",""fields"":[{""name"":""EmpEmail"",""type"":""string""},{""name"":""EmpDepartment"",""type"":""string""},{""name"":""LeaveDurationInHours"",""type"":""int""},{""name"":""LeaveStartDateTicks"",""type"":""long""},{""name"":""ProcessedBy"",""type"":""string""},{""name"":""Result"",""type"":""string""}]}"); 19 | private string _EmpEmail; 20 | private string _EmpDepartment; 21 | private int _LeaveDurationInHours; 22 | private long _LeaveStartDateTicks; 23 | private string _ProcessedBy; 24 | private string _Result; 25 | public virtual Schema Schema 26 | { 27 | get 28 | { 29 | return LeaveApplicationProcessed._SCHEMA; 30 | } 31 | } 32 | public string EmpEmail 33 | { 34 | get 35 | { 36 | return this._EmpEmail; 37 | } 38 | set 39 | { 40 | this._EmpEmail = value; 41 | } 42 | } 43 | public string EmpDepartment 44 | { 45 | get 46 | { 47 | return this._EmpDepartment; 48 | } 49 | set 50 | { 51 | this._EmpDepartment = value; 52 | } 53 | } 54 | public int LeaveDurationInHours 55 | { 56 | get 57 | { 58 | return this._LeaveDurationInHours; 59 | } 60 | set 61 | { 62 | this._LeaveDurationInHours = value; 63 | } 64 | } 65 | public long LeaveStartDateTicks 66 | { 67 | get 68 | { 69 | return this._LeaveStartDateTicks; 70 | } 71 | set 72 | { 73 | this._LeaveStartDateTicks = value; 74 | } 75 | } 76 | public string ProcessedBy 77 | { 78 | get 79 | { 80 | return this._ProcessedBy; 81 | } 82 | set 83 | { 84 | this._ProcessedBy = value; 85 | } 86 | } 87 | public string Result 88 | { 89 | get 90 | { 91 | return this._Result; 92 | } 93 | set 94 | { 95 | this._Result = value; 96 | } 97 | } 98 | public virtual object Get(int fieldPos) 99 | { 100 | switch (fieldPos) 101 | { 102 | case 0: return this.EmpEmail; 103 | case 1: return this.EmpDepartment; 104 | case 2: return this.LeaveDurationInHours; 105 | case 3: return this.LeaveStartDateTicks; 106 | case 4: return this.ProcessedBy; 107 | case 5: return this.Result; 108 | default: throw new AvroRuntimeException("Bad index " + fieldPos + " in Get()"); 109 | }; 110 | } 111 | public virtual void Put(int fieldPos, object fieldValue) 112 | { 113 | switch (fieldPos) 114 | { 115 | case 0: this.EmpEmail = (System.String)fieldValue; break; 116 | case 1: this.EmpDepartment = (System.String)fieldValue; break; 117 | case 2: this.LeaveDurationInHours = (System.Int32)fieldValue; break; 118 | case 3: this.LeaveStartDateTicks = (System.Int64)fieldValue; break; 119 | case 4: this.ProcessedBy = (System.String)fieldValue; break; 120 | case 5: this.Result = (System.String)fieldValue; break; 121 | default: throw new AvroRuntimeException("Bad index " + fieldPos + " in Put()"); 122 | }; 123 | } 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /TimeOff.Models/LeaveApplicationReceived.avsc: -------------------------------------------------------------------------------- 1 | { 2 | "namespace": "TimeOff.Models", 3 | "type": "record", 4 | "name": "LeaveApplicationReceived", 5 | "fields": [ 6 | { 7 | "name": "EmpEmail", 8 | "type": "string" 9 | }, 10 | { 11 | "name": "EmpDepartment", 12 | "type": "string" 13 | }, 14 | { 15 | "name": "LeaveDurationInHours", 16 | "type": "int" 17 | }, 18 | { 19 | "name": "LeaveStartDateTicks", 20 | "type": "long" 21 | } 22 | ] 23 | } -------------------------------------------------------------------------------- /TimeOff.Models/LeaveApplicationReceived.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------------------------ 2 | // 3 | // Generated by avrogen, version 1.10.0.0 4 | // Changes to this file may cause incorrect behavior and will be lost if code 5 | // is regenerated 6 | // 7 | // ------------------------------------------------------------------------------ 8 | namespace TimeOff.Models 9 | { 10 | using System; 11 | using System.Collections.Generic; 12 | using System.Text; 13 | using Avro; 14 | using Avro.Specific; 15 | 16 | public partial class LeaveApplicationReceived : ISpecificRecord 17 | { 18 | public static Schema _SCHEMA = Avro.Schema.Parse(@"{""type"":""record"",""name"":""LeaveApplicationReceived"",""namespace"":""TimeOff.Models"",""fields"":[{""name"":""EmpEmail"",""type"":""string""},{""name"":""EmpDepartment"",""type"":""string""},{""name"":""LeaveDurationInHours"",""type"":""int""},{""name"":""LeaveStartDateTicks"",""type"":""long""}]}"); 19 | private string _EmpEmail; 20 | private string _EmpDepartment; 21 | private int _LeaveDurationInHours; 22 | private long _LeaveStartDateTicks; 23 | public virtual Schema Schema 24 | { 25 | get 26 | { 27 | return LeaveApplicationReceived._SCHEMA; 28 | } 29 | } 30 | public string EmpEmail 31 | { 32 | get 33 | { 34 | return this._EmpEmail; 35 | } 36 | set 37 | { 38 | this._EmpEmail = value; 39 | } 40 | } 41 | public string EmpDepartment 42 | { 43 | get 44 | { 45 | return this._EmpDepartment; 46 | } 47 | set 48 | { 49 | this._EmpDepartment = value; 50 | } 51 | } 52 | public int LeaveDurationInHours 53 | { 54 | get 55 | { 56 | return this._LeaveDurationInHours; 57 | } 58 | set 59 | { 60 | this._LeaveDurationInHours = value; 61 | } 62 | } 63 | public long LeaveStartDateTicks 64 | { 65 | get 66 | { 67 | return this._LeaveStartDateTicks; 68 | } 69 | set 70 | { 71 | this._LeaveStartDateTicks = value; 72 | } 73 | } 74 | public virtual object Get(int fieldPos) 75 | { 76 | switch (fieldPos) 77 | { 78 | case 0: return this.EmpEmail; 79 | case 1: return this.EmpDepartment; 80 | case 2: return this.LeaveDurationInHours; 81 | case 3: return this.LeaveStartDateTicks; 82 | default: throw new AvroRuntimeException("Bad index " + fieldPos + " in Get()"); 83 | }; 84 | } 85 | public virtual void Put(int fieldPos, object fieldValue) 86 | { 87 | switch (fieldPos) 88 | { 89 | case 0: this.EmpEmail = (System.String)fieldValue; break; 90 | case 1: this.EmpDepartment = (System.String)fieldValue; break; 91 | case 2: this.LeaveDurationInHours = (System.Int32)fieldValue; break; 92 | case 3: this.LeaveStartDateTicks = (System.Int64)fieldValue; break; 93 | default: throw new AvroRuntimeException("Bad index " + fieldPos + " in Put()"); 94 | }; 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /TimeOff.Models/TimeOff.Models.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net5.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /TimeOff.ResultReader/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Text.Json; 3 | using Azure.Data.SchemaRegistry; 4 | using Azure.Identity; 5 | using Confluent.Kafka; 6 | using Confluent.Kafka.SyncOverAsync; 7 | using Confluent.SchemaRegistry; 8 | using Confluent.SchemaRegistry.Serdes; 9 | using Microsoft.Extensions.Configuration; 10 | using TimeOff.Core; 11 | using TimeOff.Models; 12 | 13 | namespace TimeOff.ResultReader 14 | { 15 | internal class Program 16 | { 17 | private static void Main() 18 | { 19 | CachedSchemaRegistryClient cachedSchemaRegistryClient = null; 20 | KafkaAvroDeserializer kafkaAvroKeyDeserializer = null; 21 | KafkaAvroDeserializer kafkaAvroValueDeserializer = null; 22 | 23 | Console.WriteLine("TimeOff Results Terminal\n"); 24 | 25 | var configuration = new ConfigurationBuilder() 26 | .AddJsonFile("appsettings.json", true, true) 27 | .Build(); 28 | 29 | var configReader = new ConfigReader(configuration); 30 | 31 | var schemaRegistryConfig = configReader.GetSchemaRegistryConfig(); 32 | var consumerConfig = configReader.GetConsumerConfig(); 33 | 34 | if (configReader.IsLocalEnvironment) 35 | { 36 | cachedSchemaRegistryClient = new CachedSchemaRegistryClient(schemaRegistryConfig); 37 | } 38 | else 39 | { 40 | var schemaRegistryClientAz = 41 | new SchemaRegistryClient(configuration["SchemaRegistryUrlAz"], new DefaultAzureCredential()); 42 | var schemaGroupName = configuration["SchemaRegistryGroupNameAz"]; 43 | kafkaAvroKeyDeserializer = 44 | new KafkaAvroDeserializer(schemaRegistryClientAz, schemaGroupName); 45 | kafkaAvroValueDeserializer = 46 | new KafkaAvroDeserializer(schemaRegistryClientAz, schemaGroupName); 47 | } 48 | 49 | using var consumer = new ConsumerBuilder(consumerConfig) 50 | .SetKeyDeserializer(configReader.IsLocalEnvironment 51 | ? new AvroDeserializer(cachedSchemaRegistryClient).AsSyncOverAsync() 52 | : kafkaAvroKeyDeserializer) 53 | .SetValueDeserializer(configReader.IsLocalEnvironment 54 | ? new AvroDeserializer(cachedSchemaRegistryClient).AsSyncOverAsync() 55 | : kafkaAvroValueDeserializer) 56 | .SetErrorHandler((_, e) => Console.WriteLine($"Error: {e.Reason}")) 57 | .Build(); 58 | { 59 | try 60 | { 61 | Console.WriteLine(""); 62 | consumer.Subscribe(ApplicationConstants.LeaveApplicationResultsTopicName); 63 | while (true) 64 | { 65 | var result = consumer.Consume(); 66 | var leaveRequest = result.Message.Value; 67 | Console.WriteLine( 68 | $"Received message: {result.Message.Key} Value: {JsonSerializer.Serialize(leaveRequest)}"); 69 | consumer.Commit(result); 70 | consumer.StoreOffset(result); 71 | Console.WriteLine("\nOffset committed"); 72 | Console.WriteLine("----------\n\n"); 73 | } 74 | } 75 | catch (ConsumeException e) 76 | { 77 | Console.WriteLine($"Consume error: {e.Error.Reason}"); 78 | } 79 | finally 80 | { 81 | consumer.Close(); 82 | } 83 | } 84 | } 85 | } 86 | } -------------------------------------------------------------------------------- /TimeOff.ResultReader/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "TimeOff.ResultReader": { 4 | "commandName": "Project", 5 | "environmentVariables": { 6 | "AZURE_CLIENT_SECRET": "CLIENT SECRET", 7 | "AZURE_CLIENT_ID": "CLIENT ID", 8 | "AZURE_TENANT_ID": "TENANT ID" 9 | } 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /TimeOff.ResultReader/TimeOff.ResultReader.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | Always 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /TimeOff.ResultReader/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "IsLocalEnvironment": false, 3 | "SchemaRegistryConfig": { 4 | "Url": "http://127.0.0.1:8081" 5 | }, 6 | "SchemaRegistryUrlAz": "timeoff-eh.servicebus.windows.net", 7 | "SchemaRegistryGroupNameAz": "time-off-schema", 8 | "ConsumerConfig": { 9 | "BootstrapServers": "127.0.0.1:9092", 10 | "GroupId": "application-results", 11 | "EnableAutoCommit": false, 12 | "EnableAutoOffsetStore": false 13 | }, 14 | "ConsumerConfigAz": { 15 | "BootstrapServers": ".servicebus.windows.net:9093", 16 | "GroupId": "application-results", 17 | "EnableAutoCommit": false, 18 | "EnableAutoOffsetStore": false, 19 | "MaxPollIntervalMs": 300000, 20 | "SaslUsername": "$ConnectionString", 21 | "SaslPassword": "CONNECTION STRING" 22 | } 23 | } -------------------------------------------------------------------------------- /TimeOff.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.30804.86 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{8F007CE7-E720-4933-BF5A-83D0DDAB664D}" 7 | ProjectSection(SolutionItems) = preProject 8 | docker-compose.yaml = docker-compose.yaml 9 | README.md = README.md 10 | EndProjectSection 11 | EndProject 12 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TimeOff.Core", "TimeOff.Core\TimeOff.Core.csproj", "{32E34F77-D677-40E5-9041-72554750ECA9}" 13 | EndProject 14 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TimeOff.Employee", "TimeOff.Employee\TimeOff.Employee.csproj", "{F6C7A79A-3BEB-4796-820E-BB8C6774835A}" 15 | EndProject 16 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TimeOff.Manager", "TimeOff.Manager\TimeOff.Manager.csproj", "{50E51CD3-091B-49D3-B6AA-4D4616FF2996}" 17 | EndProject 18 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TimeOff.Models", "TimeOff.Models\TimeOff.Models.csproj", "{2C27188C-DE42-4503-8890-80D60E51DA92}" 19 | EndProject 20 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TimeOff.ResultReader", "TimeOff.ResultReader\TimeOff.ResultReader.csproj", "{0B9FF56E-7146-42FC-864F-C9E31B36F553}" 21 | EndProject 22 | Global 23 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 24 | Debug|Any CPU = Debug|Any CPU 25 | Release|Any CPU = Release|Any CPU 26 | EndGlobalSection 27 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 28 | {32E34F77-D677-40E5-9041-72554750ECA9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 29 | {32E34F77-D677-40E5-9041-72554750ECA9}.Debug|Any CPU.Build.0 = Debug|Any CPU 30 | {32E34F77-D677-40E5-9041-72554750ECA9}.Release|Any CPU.ActiveCfg = Release|Any CPU 31 | {32E34F77-D677-40E5-9041-72554750ECA9}.Release|Any CPU.Build.0 = Release|Any CPU 32 | {F6C7A79A-3BEB-4796-820E-BB8C6774835A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 33 | {F6C7A79A-3BEB-4796-820E-BB8C6774835A}.Debug|Any CPU.Build.0 = Debug|Any CPU 34 | {F6C7A79A-3BEB-4796-820E-BB8C6774835A}.Release|Any CPU.ActiveCfg = Release|Any CPU 35 | {F6C7A79A-3BEB-4796-820E-BB8C6774835A}.Release|Any CPU.Build.0 = Release|Any CPU 36 | {50E51CD3-091B-49D3-B6AA-4D4616FF2996}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 37 | {50E51CD3-091B-49D3-B6AA-4D4616FF2996}.Debug|Any CPU.Build.0 = Debug|Any CPU 38 | {50E51CD3-091B-49D3-B6AA-4D4616FF2996}.Release|Any CPU.ActiveCfg = Release|Any CPU 39 | {50E51CD3-091B-49D3-B6AA-4D4616FF2996}.Release|Any CPU.Build.0 = Release|Any CPU 40 | {2C27188C-DE42-4503-8890-80D60E51DA92}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 41 | {2C27188C-DE42-4503-8890-80D60E51DA92}.Debug|Any CPU.Build.0 = Debug|Any CPU 42 | {2C27188C-DE42-4503-8890-80D60E51DA92}.Release|Any CPU.ActiveCfg = Release|Any CPU 43 | {2C27188C-DE42-4503-8890-80D60E51DA92}.Release|Any CPU.Build.0 = Release|Any CPU 44 | {0B9FF56E-7146-42FC-864F-C9E31B36F553}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 45 | {0B9FF56E-7146-42FC-864F-C9E31B36F553}.Debug|Any CPU.Build.0 = Debug|Any CPU 46 | {0B9FF56E-7146-42FC-864F-C9E31B36F553}.Release|Any CPU.ActiveCfg = Release|Any CPU 47 | {0B9FF56E-7146-42FC-864F-C9E31B36F553}.Release|Any CPU.Build.0 = Release|Any CPU 48 | EndGlobalSection 49 | GlobalSection(SolutionProperties) = preSolution 50 | HideSolutionNode = FALSE 51 | EndGlobalSection 52 | GlobalSection(ExtensibilityGlobals) = postSolution 53 | SolutionGuid = {F42E3C4F-FDAC-4843-89AC-EF945D161977} 54 | EndGlobalSection 55 | EndGlobal 56 | -------------------------------------------------------------------------------- /TimeOff.sln.DotSettings: -------------------------------------------------------------------------------- 1 |  2 | True -------------------------------------------------------------------------------- /docker-compose.yaml: -------------------------------------------------------------------------------- 1 | version: "2" 2 | 3 | networks: 4 | kafka-net: 5 | driver: bridge 6 | 7 | services: 8 | zookeeper-server: 9 | image: bitnami/zookeeper:latest 10 | networks: 11 | - kafka-net 12 | ports: 13 | - 2181:2181 14 | environment: 15 | - ALLOW_ANONYMOUS_LOGIN=yes 16 | kafdrop: 17 | image: obsidiandynamics/kafdrop 18 | networks: 19 | - kafka-net 20 | restart: "no" 21 | ports: 22 | - 9000:9000 23 | environment: 24 | KAFKA_BROKERCONNECT: PLAINTEXT://kafka-server:29092 25 | JVM_OPTS: -Xms16M -Xmx48M -Xss180K -XX:-TieredCompilation -XX:+UseStringDeduplication -noverify 26 | SCHEMAREGISTRY_CONNECT: http://schema-registry:8081 27 | depends_on: 28 | - kafka-server 29 | kafka-server: 30 | image: bitnami/kafka:latest 31 | networks: 32 | - kafka-net 33 | ports: 34 | - 9092:9092 35 | environment: 36 | - KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper-server:2181 37 | - KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://kafka-server:29092,PLAINTEXT_HOST://127.0.0.1:9092 38 | - KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT 39 | - KAFKA_CFG_LISTENERS=PLAINTEXT://:29092,PLAINTEXT_HOST://:9092 40 | - KAFKA_CFG_INTER_BROKER_LISTENER_NAME=PLAINTEXT 41 | - ALLOW_PLAINTEXT_LISTENER=yes 42 | depends_on: 43 | - zookeeper-server 44 | schema-registry: 45 | image: confluentinc/cp-schema-registry:latest 46 | networks: 47 | - kafka-net 48 | ports: 49 | - 8081:8081 50 | environment: 51 | - SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS=PLAINTEXT://kafka-server:29092 52 | - SCHEMA_REGISTRY_HOST_NAME=localhost 53 | - SCHEMA_REGISTRY_LISTENERS=http://0.0.0.0:8081 54 | depends_on: 55 | - kafka-server --------------------------------------------------------------------------------