Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Category: Asp.Net Core

Asp.Net Core / C#

ASP.NET Core “IOptions Snapshot Turns Slow” — The Startup Misuse

- 09.12.25 - ErcanOPAK comment on ASP.NET Core “IOptions Snapshot Turns Slow” — The Startup Misuse

Calling IOptionsSnapshot in tight loops = performance disasterbecause it reconstructs objects every request. ✔ Fix Use IOptionsMonitor for high-frequency reads. public MyService(IOptionsMonitor<MyConfig> cfg) { _cfg = cfg; } Monitor is cached, Snapshot is per-request.

Read More
Asp.Net Core / C#

ASP.NET Core “IHostedService Never Stops” — Fixing Broken Graceful Shutdowns

- 08.12.25 - ErcanOPAK comment on ASP.NET Core “IHostedService Never Stops” — Fixing Broken Graceful Shutdowns

Many background workers never stop on SIGTERM. ✔ Real Fix Bind cancellation token: public Task StartAsync(CancellationToken ct) { _ = DoWorkAsync(ct); return Task.CompletedTask; } 💡 Super Tip In Program.cs: builder.Services.AddHostedService<MyWorker>(); builder.Services.Configure<HostOptions>(o => { o.ShutdownTimeout = TimeSpan.FromSeconds(20); }); Most devs don’t know this exists → massively improves controlled shutdown.

Read More
Asp.Net Core / C#

.NET Core Dependency Injection “Multiple Registrations” Bug

- 08.12.25 - ErcanOPAK comment on .NET Core Dependency Injection “Multiple Registrations” Bug

This bug causes random services to switch implementations unexpectedly. ⚠ Problem Registering twice: services.AddSingleton<IMailer, SmtpMailer>(); services.AddSingleton<IMailer, FakeMailer>(); ASP.NET Core uses the last registration → extremely hard-to-debug issues. ✔ Fix Remove duplicates OR name your registrations: services.AddSingleton<IMailer, SmtpMailer>(“smtp”); 💡 Hidden Debug Trick Log all registrations: services.ToList().ForEach(x => Console.WriteLine(x.ServiceType));  

Read More
Asp.Net Core / C#

Rate Limiting in .NET — Fix ‘Random 429 Errors’ the Right Way

- 07.12.25 | 15.02.26 - ErcanOPAK comment on Rate Limiting in .NET — Fix ‘Random 429 Errors’ the Right Way

The Trap: Most developers dutifully add a Rate Limiter to their API to prevent abuse. However, they often make a critical mistake: they set a Global Limit. If you set a limit of “1000 requests per minute” globally, a single malicious bot can use up all 1000 requests in 5 seconds. The result? Your legitimate […]

Read More
Asp.Net Core / C#

ASP.NET Core “ValidateOnStart” — Stop Production Crashes at Runtime

- 07.12.25 - ErcanOPAK comment on ASP.NET Core “ValidateOnStart” — Stop Production Crashes at Runtime

One of the least known but most powerful features. ✔ Use: builder.Services.AddOptions<MySettings>() .Bind(configuration.GetSection(“MySettings”)) .ValidateDataAnnotations() .ValidateOnStart(); Now, invalid configs fail at startup, not during production traffic. 💡 Saves Hours Prevents: null reference crashes broken API keys missing secrets

Read More
Asp.Net Core / C#

.NET Core JSON Serializer Pitfalls — Why Your Properties Don’t Serialize

- 06.12.25 - ErcanOPAK comment on .NET Core JSON Serializer Pitfalls — Why Your Properties Don’t Serialize

System.Text.Json is fast……but very strict. Common Pain Points Missing getters Private setters PascalCase vs camelCase mismatch Ignored fields without [JsonInclude] Cycles now break serialization ✔ Life-Saving Fix Add: options.PropertyNameCaseInsensitive = true; options.ReferenceHandler = ReferenceHandler.IgnoreCycles; ✔ Hidden Trick For private setters: [JsonInclude] public string Name { get; private set; } This is rarely mentioned but solves […]

Read More
Asp.Net Core / C#

ASP.NET Core Middleware Order — Why Your App Breaks for No Reason

- 06.12.25 - ErcanOPAK comment on ASP.NET Core Middleware Order — Why Your App Breaks for No Reason

The #1 misunderstood part of ASP.NET Core is middleware order. ❌ Bad Order Example app.UseAuthentication(); app.UseRouting(); app.UseAuthorization(); Authentication runs BEFORE routing → user can’t be authorized → random 401s. ✔ Correct Order app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(…); 💡 Life-Saving Insight Routing must ALWAYS come first.Authorization must ALWAYS come after Authentication. Confusing ordering makes APIs randomly break.

Read More
Asp.Net Core / C#

Dependency Injection Performance — “AddTransient Everywhere” Is Not the Answer

- 06.12.25 - ErcanOPAK comment on Dependency Injection Performance — “AddTransient Everywhere” Is Not the Answer

Many APIs slow down because lifetime choices are wrong. ❌ Common Mistake Using Transient for heavy services: services.AddTransient<EmailService>(); ✔ Correct Use Singleton for stateless services: services.AddSingleton<IEmailSender, EmailService>(); 💡 Why? Fewer allocations Less GC pressure Faster resolving More consistent performance

Read More
Asp.Net Core

ASP.NET Core Kestrel Limits — Avoid Flooding & Keep APIs Stable

- 06.12.25 - ErcanOPAK comment on ASP.NET Core Kestrel Limits — Avoid Flooding & Keep APIs Stable

Kestrel has built-in protection against floods, slow clients, and excessive headers. Key Settings in appsettings.json: “Kestrel”: { “Limits”: { “MaxRequestBodySize”: 1048576, “KeepAliveTimeout”: “00:00:30”, “RequestHeadersTimeout”: “00:00:30” } } 💡 Why This Matters Stops slow-loris attacks Reduces memory pressure Prevents API overload Ensures predictable scaling Bonus Enable HTTP/2 for better concurrency.

Read More
Asp.Net Core / C#

.NET Core Connection Pooling — The Hidden Performance Giant

- 06.12.25 - ErcanOPAK comment on .NET Core Connection Pooling — The Hidden Performance Giant

Most API latency problems = opening too many SQL connections. 🚀 Fix Use a single pooled client: builder.Services.AddDbContext<AppDb>(options => options.UseSqlServer(connString, o => o.EnableRetryOnFailure())); ✨ Benefits ⚡ Fast connection acquisition 📉 Lower CPU 🧵 Stable high-load behavior 💡 BonusUse Max Pool Size=200 in connection string for heavy systems.

Read More
Asp.Net Core / C#

Minimal APIs Filters — Middleware Without the Middleware Cost

- 06.12.25 | 06.12.25 - ErcanOPAK comment on Minimal APIs Filters — Middleware Without the Middleware Cost

Filters allow cross-cutting logic without writing a full middleware. builder.Services.AddEndpointFilter<ValidationFilter>(); ⚡ Why They’re Great ⚙ Less overhead 🎯 Scoped to specific endpoints 🧽 Cleaner architectures 🔍 Perfect for validation, auth, logging

Read More
Asp.Net Core / C#

Clean Dependency Injection — Lifetimes That Every Dev Must Master

- 05.12.25 - ErcanOPAK comment on Clean Dependency Injection — Lifetimes That Every Dev Must Master

⏳ The 3 Lifetimes 🔵 Singleton → stateless global services 🟢 Scoped → per-request (web default) 🟠 Transient → lightweight, multi-instance ❌ Absolute Rule Never inject a Scoped service into a Singleton. Bad: builder.Services.AddSingleton<MyService>(); // depends on DbContext ❌ ✔ Correct Use factory: builder.Services.AddSingleton<MyService>(sp => new MyService(sp.GetRequiredService<IDbContextFactory<AppDb>>())); 🧠 Why? Prevents stale data Eliminates threading issues […]

Read More
Asp.Net Core / C#

EF Core: ExecuteUpdate & ExecuteDelete — Why You Should Stop Looping

- 05.12.25 | 05.12.25 - ErcanOPAK comment on EF Core: ExecuteUpdate & ExecuteDelete — Why You Should Stop Looping

The old way: foreach (var user in users) user.IsActive = false; await ctx.SaveChangesAsync(); The modern way: await ctx.Users.Where(x => x.IsActive) .ExecuteUpdateAsync(set => set.SetProperty(p => p.IsActive, false)); ⚡ Benefits 🚀 Single SQL statement 🧼 No tracking overhead 💾 Saves CPU + DB roundtrips 🧩 Perfect for batch jobs + cron workers

Read More
Asp.Net Core / C#

C# Records — Why They Still Beat Classes for Domain Models

- 05.12.25 | 05.12.25 - ErcanOPAK comment on C# Records — Why They Still Beat Classes for Domain Models

Records are still the cleanest way to model immutable domain data. public record Order(int Id, decimal Total, string Status); ❤️ Why Devs Love Records 🔐 Built-in immutability 📝 Value-based equality ✂ Boilerplate goes to zero 🔄 Perfect for event-driven + DDD ⚡ Extra Tip Use with expressions to clone deeply but safely.

Read More
Asp.Net Core / C#

.NET 9 Rate Limiting — Easy API Protection

- 05.12.25 | 05.12.25 - ErcanOPAK comment on .NET 9 Rate Limiting — Easy API Protection

Rate limiting used to be hard… now it’s literally one line. app.UseRateLimiter(new() { GlobalLimiter = PartitionedRateLimiter.CreateChained( PartitionedRateLimiter.CreateFixedWindow(10, TimeSpan.FromSeconds(5)) ) }); 🔒 Why You Need It 🛡 Stops brute-force 📉 Protects API throughput ⚙ Cloud-native resilience 🌍 Zero-config distributed support (Redis optional) 🧠 Bonus Pair with Minimal APIs for super-light API services.

Read More
Asp.Net Core / C#

Async Streams in C# — Clean, Fast, Real-Time Data

- 05.12.25 | 05.12.25 - ErcanOPAK comment on Async Streams in C# — Clean, Fast, Real-Time Data

🌀 Why Async Streams Shine Async Streams (IAsyncEnumerable<T>) are perfect for streaming data without memory blows. await foreach (var log in service.GetLogsAsync()) { Console.WriteLine(log); } ✨ What They Solve 🔁 Handling live logs 📡 Streamed API responses 🧵 Reducing memory spikes 🌊 Backpressure-friendly async flow 💡 Pro Tip Combine with channels for ultra-responsive pipelines.

Read More
.NET / Asp.Net Core / C#

EF Core 8 ExecuteUpdate – Batch Updates Without Loops

- 04.12.25 | 04.12.25 - ErcanOPAK

ExecuteUpdate allows batch updates without tracking overhead. await ctx.Users .Where(u => u.IsActive == false) .ExecuteUpdateAsync(p => p.SetProperty(x => x.IsDeleted, true));   It’s fast, clean, and ideal for big-data operations.

Read More
.NET / Asp.Net Core / C#

Cleaner .NET APIs Using IEndpointFilter for Validation

- 04.12.25 | 04.12.25 - ErcanOPAK

Endpoint filters reduce validation noise in Minimal APIs. public class ValidateFilter : IEndpointFilter { public Task<object?> InvokeAsync(EndpointFilterInvocationContext ctx, EndpointFilterDelegate next) { var model = ctx.GetArgument(0); Validator.ValidateObject(model, new()); return next(ctx); } } They keep endpoints clean, reusable, and testable.

Read More
.NET / Asp.Net Core / C#

Build Clean Background Services with IHostedService in .NET

- 04.12.25 | 04.12.25 - ErcanOPAK

BackgroundService enables clean, scalable background processing. protected override async Task ExecuteAsync(CancellationToken ct) { while (!ct.IsCancellationRequested) { await DoWork(); await Task.Delay(1000, ct); } } It works perfectly in cloud-native environments.

Read More
.NET / Asp.Net Core / C# / Entity Framework

Stop Calling ToList() Too Early – Improve LINQ Performance

- 04.12.25 | 04.12.25 - ErcanOPAK

Calling ToList() too early forces premature execution. // Bad var items = db.Users.ToList().Where(…); // Good var items = db.Users.Where(…).ToList();   Let the database handle filtering and execution.

Read More
.NET / Asp.Net Core / C#

Never Write Retry Logic Again – Use Polly in .NET

- 04.12.25 | 04.12.25 - ErcanOPAK

Polly provides retry, timeout, fallback, and circuit-breaker patterns. var result = await Policy .Handle() .RetryAsync(3) .ExecuteAsync(() => CallApi()); It’s the gold standard for resilience in .NET microservices.

Read More
.NET / Asp.Net Core / C#

Clean Minimal APIs in .NET – Automatic DTO Binding Explained

- 04.12.25 | 04.12.25 - ErcanOPAK

Minimal APIs provide automatic DTO binding, eliminating unnecessary boilerplate. app.MapPost(“/login”, (LoginRequest req) => { return Results.Ok($”Welcome {req.Email}”); }); This improves readability, reduces errors, and works perfectly with endpoint filters.

Read More
.NET / Asp.Net Core / C#

Write Cleaner Business Logic Using C# Switch Expressions

- 04.12.25 | 04.12.25 - ErcanOPAK

Switch expressions simplify complex branching logic and make code more readable. var price = plan switch { “free” => 0, “pro” => 19, “biz” => 49, _ => throw new Exception(“Unknown plan”) }; They are ideal for pricing models, rules engines, and state transitions.

Read More
.NET / Asp.Net Core / C#

EF Core Performance Boost: Use AsNoTracking() When Reading Data

- 04.12.25 | 04.12.25 - ErcanOPAK

For read-heavy queries, AsNoTracking() provides massive performance improvements. var users = await ctx.Users.AsNoTracking().ToListAsync(); It avoids adding entities to the change tracker, improving speed by 30–50%.

Read More
.NET / Asp.Net Core / C#

Use IAsyncEnumerable for Streaming Data in .NET

- 04.12.25 | 04.12.25 - ErcanOPAK

IAsyncEnumerable allows true async streaming, reducing memory usage. await foreach (var user in repo.GetAllAsync()) Console.WriteLine(user.Name); Perfect for large datasets and high-throughput APIs.

Read More
.NET / Asp.Net Core / C#

Why C# Record Types Are Perfect for Modern DTOs

- 04.12.25 | 04.12.25 - ErcanOPAK

Records offer immutability, value semantics, and minimal syntax. public record UserDto(int Id, string Name, string Email); They are ideal for APIs, events, and data-transfer layers.

Read More
.NET / Asp.Net Core / C#

Improve .NET Logging with Structured Log Messages

- 04.12.25 | 04.12.25 - ErcanOPAK

Structured logs allow smarter querying and cleaner search results. logger.LogInformation(“User {UserId} logged in”, id); They improve observability, diagnostics, and production monitoring.

Read More
.NET / Asp.Net Core / C#

Boost Async Performance in .NET with ConfigureAwait(false)

- 04.12.25 | 04.12.25 - ErcanOPAK

Using ConfigureAwait(false) can significantly reduce context-switching overhead in non-UI .NET applications. This is essential for APIs, background services, and any high-throughput async pipeline. await http.SendAsync(req).ConfigureAwait(false); It lowers CPU usage, avoids deadlocks, and improves overall async performance.

Read More
.NET / Asp.Net Core / C#

Lightning-Fast Lookups in .NET Using MemoryCache

- 04.12.25 | 04.12.25 - ErcanOPAK

MemoryCache provides a simple and fast in-memory caching solution. var item = cache.GetOrCreate(“users”, entry => { entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10); return repo.GetUsers(); }); Ideal for configuration, lookups, and session data.

Read More
.NET / Asp.Net Core / C#

Every Async Method Should Accept CancellationToken

- 04.12.25 | 04.12.25 - ErcanOPAK

CancellationToken prevents zombie tasks and ensures graceful shutdown. public Task RunAsync(CancellationToken ct) It is a must-have for cloud-native workloads.

Read More
Page 6 of 7
« Previous 1 2 3 4 5 6 7 Next »

Posts navigation

Older posts
Newer posts
April 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
27282930  
« Mar    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (858)
  • Get the First and Last Word from a String or Sentence in SQL (836)
  • How to select distinct rows in a datatable in C# (805)
  • How to make theater mode the default for Youtube (754)
  • Add Constraint to SQL Table to ensure email contains @ (578)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (564)
  • Average of all values in a column that are not zero in SQL (531)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (489)
  • Find numbers with more than two decimal places in SQL (447)

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries
  • SQL: Use Window Functions for Advanced Analytical Queries
  • .NET Core: Use Background Services for Long-Running Tasks
  • .NET Core: Use Minimal APIs for Lightweight HTTP Services
  • Git: Use Cherry-Pick to Apply Specific Commits Across Branches
  • Git: Use Interactive Rebase to Clean Up Commit History Before Merge

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (858)
  • Get the First and Last Word from a String or Sentence in SQL (836)
  • How to select distinct rows in a datatable in C# (805)
  • How to make theater mode the default for Youtube (754)

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com