-
Notifications
You must be signed in to change notification settings - Fork 2
Complete implementation - Email dispatcher #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
arulkailasam
wants to merge
2
commits into
Codomycel:dev
Choose a base branch
from
arulkailasam:dev
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| ################################################################################ | ||
| # This .gitignore file was automatically created by Microsoft(R) Visual Studio. | ||
| ################################################################################ | ||
|
|
||
| /EmailDispatcherAPI/bin | ||
| /EmailDispatcherAPI/.vs/EmailDispatcherAPI | ||
| /EmailDispatcherAPI/obj | ||
| /EmailWorker/bin | ||
| /EmailWorker/obj | ||
| /EmailDispatcherAPI/appsettings.json | ||
| /EmailDispatcherAPI/appsettings.Development.json | ||
| /EmailWorker/appsettings.json | ||
| /EmailWorker/appsettings.Development.json | ||
| /EmailWorker/appsettings.json | ||
| /EmailWorker/appsettings.Development.json | ||
| /EmailRetryScheduler/appsettings.json | ||
| /EmailRetryScheduler/appsettings.Development.json | ||
| /EmailRetryScheduler/bin | ||
| /EmailRetryScheduler/obj | ||
| /EmailRetryScheduler/obj | ||
| /EmailRetryScheduler/obj/Debug | ||
| /EmailRetryScheduler/obj/Debug/net9.0 | ||
| /EmailRetryScheduler/appsettings.json | ||
| /EmailRetryScheduler/appsettings.Development.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| ## Stack | ||
| - RabbitMQ | ||
| - .NET Worker Service | ||
| - Database (SQL Server) | ||
| - SMTP / Email Provider | ||
|
|
||
| ## .Net Technical terms used : | ||
| - WEB API | ||
| - Entity Framework | ||
| - Minimal Api | ||
| - Global exception handling and developer exception page | ||
| - DB First approach | ||
| - Repository pattern and DI | ||
| - Asynchronous Initialization via Hosted Service | ||
|
|
||
| ## RabbitMQ docker setup command | ||
| # latest RabbitMQ 4.x | ||
| - docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:4-management | ||
|
|
||
| ## RabbitMQ client | ||
| - Should have single connection for whole application(singleton) | ||
| - Can create multiple channel as required | ||
|
|
||
| ## Connection: Physical link to broker | ||
|
|
||
| - Channel: Lightweight AMQP session | ||
|
|
||
| - Exchange: Routes messages | ||
|
|
||
| - Routing key: Address label | ||
|
|
||
| - Queue: Stores messages | ||
|
|
||
| - Binding: Routing rule | ||
|
|
||
| - Consumer: Processes messages | ||
|
|
||
|
|
||
| ## 📨 RabbitMQ Publish Flow | ||
| ## 1️⃣ Connection | ||
|
|
||
| CreateConnectionAsync() opens a TCP connection | ||
|
|
||
| Heavy operation | ||
|
|
||
| Should be reused, not created per message | ||
|
|
||
| Thread-safe | ||
|
|
||
| ## 2️⃣ Channel | ||
|
|
||
| CreateChannelAsync() creates an AMQP channel | ||
|
|
||
| Lightweight | ||
|
|
||
| Used for publish/consume operations | ||
|
|
||
| Multiple channels can share one connection | ||
|
|
||
| ## 3️⃣ Queue Declaration | ||
|
|
||
| QueueDeclareAsync() ensures queue exists | ||
|
|
||
| Idempotent (safe to call multiple times) | ||
|
|
||
| Fails if queue exists with different settings | ||
|
|
||
| Flags | ||
|
|
||
| durable → survives broker restart | ||
|
|
||
| exclusive → only one connection can use it | ||
|
|
||
| autoDelete → deleted when last consumer disconnects | ||
|
|
||
| ## 4️⃣ Message Serialization | ||
|
|
||
| Messages are sent as byte[] | ||
|
|
||
| Common format: JSON → UTF-8 bytes | ||
|
|
||
| ## 5️⃣ Publishing Message | ||
|
|
||
| BasicPublishAsync() sends message to exchange | ||
|
|
||
| Default exchange ("") routes by queue name | ||
|
|
||
| Publish ≠ delivered | ||
|
|
||
| Publish ≠ persisted | ||
|
|
||
| ## Dependecy Issue for windows services | ||
|
|
||
| While it seems intuitive to make everything a Singleton because a Windows Service runs continuously, you are running into a common dependency injection issue called a Scoped Leak. | ||
|
|
||
| The Problem: Capturing a Scoped Service | ||
| In Entity Framework Core, a DbContext is registered as Scoped by default. This means it is designed to be created and destroyed within a short lifetime (like a single HTTP request or a single loop of a worker). | ||
|
|
||
| If your EmailRepository is a Singleton, it will be created once when the service starts. Because it depends on AppDBContext, it will "capture" that context and hold onto it forever. | ||
|
|
||
| This leads to several issues: | ||
|
|
||
| Memory Leaks: The DbContext keeps track of every entity it ever loads. Over days or weeks, your memory usage will climb indefinitely. | ||
|
|
||
| Concurrency Crashes: A DbContext is not thread-safe. If your worker tries to do two things at once using the same singleton context, it will throw an exception. | ||
|
|
||
| Stale Data: You won't see updates made to the database by other processes because the singleton context keeps its own internal cache. | ||
|
|
||
| The Solution: Use a Service Scope | ||
| The correct pattern for a continuous Worker (Windows Service) is to create a Scope manually inside your background loop. This ensures the DbContext is fresh for every "pulse" of work. | ||
|
|
||
|
|
||
| ## C# Rules to remeber | ||
| - All interface members are implicitly public This is a hard C# rule. | ||
| - A public Classes cannot expose a less-accessible type like internal classes. So when you inject internal into public services means it show error. | ||
|
|
||
|
|
||
| ## RabbitMq | ||
|
|
||
| When Send message Quename and Routing key should be same |
Binary file added
BIN
+2.21 KB
EmailDispatcherAPI/.vs/ProjectEvaluation/emaildispatcherapi.metadata.v9.bin
Binary file not shown.
Binary file added
BIN
+142 KB
EmailDispatcherAPI/.vs/ProjectEvaluation/emaildispatcherapi.projects.v9.bin
Binary file not shown.
Binary file added
BIN
+180 KB
EmailDispatcherAPI/.vs/ProjectEvaluation/emaildispatcherapi.strings.v9.bin
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| namespace EmailDispatcherAPI.Constant | ||
| { | ||
| internal class AppConstant | ||
| { | ||
| public const string QueueName = "email.dispatcher.send"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| namespace EmailDispatcherAPI.Constant.Enum | ||
| { | ||
| public enum EmailStatus | ||
| { | ||
| Pending = 1, | ||
| Scheduled = 2, | ||
| Sent = 3, | ||
| Failed = 4, | ||
| RetryQueued = 5, | ||
| Dead = 6 | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| using EmailDispatcherAPI.Modal; | ||
|
|
||
| namespace EmailDispatcherAPI.Contract | ||
| { | ||
| public interface IEmailRepository | ||
| { | ||
| Task<EmailIdempotency?> GetEmailIdempotencyAsync(string idempotencyKey); | ||
| Task<EmailLog> CreateEmailLog(EmailLog emailLog); | ||
| Task<EmailIdempotency> CreateEmailIdempotency(EmailIdempotency emailIdempotency); | ||
| Task<bool> MarkEmailIdempotencyAsPublishedAsync(int id); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| namespace EmailDispatcherAPI.Contract | ||
| { | ||
| public interface IEmailService | ||
| { | ||
| Task<bool> IsValidEmail(string email); | ||
| Task SendEmail(string mailTo,int entityId); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| using RabbitMQ.Client; | ||
|
|
||
| namespace EmailDispatcherAPI.Contract | ||
| { | ||
| public interface IRabbitMqConnection | ||
| { | ||
| IConnection Connection { get; } | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| using EmailDispatcherAPI.Modal; | ||
| using Microsoft.EntityFrameworkCore; | ||
|
|
||
| namespace EmailDispatcherAPI.Data | ||
| { | ||
| public class AppDBContext : DbContext | ||
| { | ||
| protected readonly IConfiguration Configuration; | ||
| public AppDBContext(IConfiguration configuration) | ||
| { | ||
| Configuration = configuration; | ||
| } | ||
| protected override void OnConfiguring(DbContextOptionsBuilder options) | ||
| { | ||
| options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")); | ||
| } | ||
| public DbSet<EmailLog> EmailLog { get; set; } | ||
| public DbSet<EmailIdempotency> EmailIdempotency { get; set; } | ||
| public DbSet<EmailStatus> EmailStatus { get; set; } | ||
| public DbSet<EmailActionLog> EmailActionLog { get; set; } | ||
|
|
||
| protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
| { | ||
| modelBuilder.Entity<EmailIdempotency>() | ||
| .HasIndex(e => e.MessageKey) | ||
| .IsUnique(); | ||
|
|
||
| modelBuilder.Entity<EmailLog>() | ||
| .HasIndex(e => new { e.EmailStatusId, e.NextAttemptAt }); | ||
| } | ||
|
|
||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| namespace EmailDispatcherAPI.Dto | ||
| { | ||
| public class RabbitMQConfig | ||
| { | ||
| public string HostName { get; set; } = "localhost"; | ||
| public int Port { get; set; } = 5672; | ||
| public string UserName { get; set; } = "guest"; | ||
| public string Password { get; set; } = "guest"; | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" /> | ||
| <PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.1" /> | ||
| <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.1" /> | ||
| <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.1"> | ||
| <PrivateAssets>all</PrivateAssets> | ||
| <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
| </PackageReference> | ||
| <PackageReference Include="Newtonsoft.Json" Version="13.0.4" /> | ||
| <PackageReference Include="RabbitMQ.Client" Version="7.2.0" /> | ||
| <PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.1" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
| <PropertyGroup> | ||
| <ActiveDebugProfile>https</ActiveDebugProfile> | ||
| </PropertyGroup> | ||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| @EmailDispatcherAPI_HostAddress = http://localhost:5213 | ||
|
|
||
| GET {{EmailDispatcherAPI_HostAddress}}/weatherforecast/ | ||
| Accept: application/json | ||
|
|
||
| ### |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| # Visual Studio Version 17 | ||
| VisualStudioVersion = 17.6.33801.468 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EmailDispatcherAPI", "EmailDispatcherAPI.csproj", "{46D2A6BB-80B6-41F1-AD62-599EFB8799EE}" | ||
| EndProject | ||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EmailWorker", "..\EmailWorker\EmailWorker.csproj", "{F70C08CC-2CDD-4EC4-BDDA-C99C994D2D40}" | ||
| EndProject | ||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EmailRetryScheduler", "..\EmailRetryScheduler\EmailRetryScheduler.csproj", "{C666BCBF-D1C1-470A-BE3F-EA19BAEC5370}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {46D2A6BB-80B6-41F1-AD62-599EFB8799EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {46D2A6BB-80B6-41F1-AD62-599EFB8799EE}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {46D2A6BB-80B6-41F1-AD62-599EFB8799EE}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {46D2A6BB-80B6-41F1-AD62-599EFB8799EE}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {F70C08CC-2CDD-4EC4-BDDA-C99C994D2D40}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {F70C08CC-2CDD-4EC4-BDDA-C99C994D2D40}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {F70C08CC-2CDD-4EC4-BDDA-C99C994D2D40}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {F70C08CC-2CDD-4EC4-BDDA-C99C994D2D40}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {C666BCBF-D1C1-470A-BE3F-EA19BAEC5370}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {C666BCBF-D1C1-470A-BE3F-EA19BAEC5370}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {C666BCBF-D1C1-470A-BE3F-EA19BAEC5370}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {C666BCBF-D1C1-470A-BE3F-EA19BAEC5370}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {6CD7770A-E955-4903-AB51-0582BCF4B728} | ||
| EndGlobalSection | ||
| EndGlobal |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| [ | ||
| { | ||
| "Name": "New Profile", | ||
| "Projects": [ | ||
| { | ||
| "Path": "EmailDispatcherAPI.csproj", | ||
| "Action": "Start" | ||
| }, | ||
| { | ||
| "Path": "..\\EmailWorker\\EmailWorker.csproj", | ||
| "Action": "Start" | ||
| }, | ||
| { | ||
| "Path": "..\\EmailRetryScheduler\\EmailRetryScheduler.csproj", | ||
| "Action": "Start" | ||
| } | ||
| ] | ||
| } | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| namespace EmailDispatcherAPI.Exception | ||
| { | ||
| public sealed class ResourceAlreadyExistsException : System.Exception | ||
| { | ||
| public ResourceAlreadyExistsException(string message) : base(message) {} | ||
| } | ||
| } | ||
|
|
||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion : configuration file can be move out from the Dto folder