Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .gitignore
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
120 changes: 120 additions & 0 deletions Dev_Notes.md
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 not shown.
Binary file not shown.
Binary file not shown.
7 changes: 7 additions & 0 deletions EmailDispatcherAPI/Constant/AppConstant.cs
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";
}
}
12 changes: 12 additions & 0 deletions EmailDispatcherAPI/Constant/Enum/EmailStatus.cs
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
}
}
12 changes: 12 additions & 0 deletions EmailDispatcherAPI/Contract/IEmailRepository.cs
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);
}
}
8 changes: 8 additions & 0 deletions EmailDispatcherAPI/Contract/IEmailService.cs
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);
}
}
10 changes: 10 additions & 0 deletions EmailDispatcherAPI/Contract/IRabbitMqConnection.cs
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; }
}

}
33 changes: 33 additions & 0 deletions EmailDispatcherAPI/Data/AppDBContext.cs
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 });
}

}
}
11 changes: 11 additions & 0 deletions EmailDispatcherAPI/Dto/RabbitMQConfig.cs
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";
}

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

}

22 changes: 22 additions & 0 deletions EmailDispatcherAPI/EmailDispatcherAPI.csproj
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>
6 changes: 6 additions & 0 deletions EmailDispatcherAPI/EmailDispatcherAPI.csproj.user
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>
6 changes: 6 additions & 0 deletions EmailDispatcherAPI/EmailDispatcherAPI.http
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

###
37 changes: 37 additions & 0 deletions EmailDispatcherAPI/EmailDispatcherAPI.sln
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
19 changes: 19 additions & 0 deletions EmailDispatcherAPI/EmailDispatcherAPI.slnLaunch.user
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"
}
]
}
]
9 changes: 9 additions & 0 deletions EmailDispatcherAPI/Exception/CustomException.cs
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) {}
}
}


Loading