Showing posts with label .Net Framework. Show all posts
Showing posts with label .Net Framework. Show all posts

Friday, November 7, 2025

Dotnet Core - CoreCLR Vs NativeAOT Runtimes

When you compile with NativeAOT (PublishAot=true), your application does not use CoreCLR at all at runtime.

Instead, it uses a minimal, statically linked runtime that’s fundamentally different from CoreCLR, that is a lightweight static runtime called the NativeAOT Runtime (derived from the old CoreRT project)


๐Ÿ” How it works conceptually

When you publish a .NET app normally:

Standard .NET (JIT/CoreCLR path):

Your C# code ↓ (compiled by Roslyn) Intermediate Language (IL) ↓ (executed by CoreCLR at runtime) CoreCLR JIT compiles IL → Native code (on the fly)

When you publish using NativeAOT, the process changes drastically:

NativeAOT (AOT path):

Your C# code ↓ (compiled by Roslyn) Intermediate Language (IL) ↓ (compiled AOT by crossgen2) Native Machine Code (.exe) ↓ Executed directly by OS — No CoreCLR, no JIT





⚙️ What replaces CoreCLR in NativeAOT?

Instead of the CoreCLR runtime, NativeAOT uses a lightweight static runtime called the NativeAOT Runtime (derived from the old CoreRT project).

This runtime:

  • Includes a minimal GC, type system, and exception handling system.
  • Does not include JIT compilation logic.
  • Does not support full reflection or dynamic assembly loading.

It’s statically linked into the final executable, so the entire runtime becomes part of your .exe file.


๐Ÿง  Deeper Comparison: CoreCLR vs NativeAOT Runtime

FeatureCoreCLRNativeAOT Runtime
JIT Compiler✅ Present (RyuJIT)❌ Not included
GC (Garbage Collector)✅ Full-featured, generational✅ Simplified GC (same core concepts, smaller)
Type Loader✅ Dynamic✅ Static (known at build time)
Reflection✅ Full⚠️ Limited (metadata stripped)
Dynamic Assembly Load✅ Supported❌ Not supported
Code Generation at Runtime (Emit)✅ Supported❌ Not supported
Startup Time⚡ Moderate⚡⚡ Super fast
Binary Size๐Ÿงฑ Larger๐Ÿ’ก Smaller
Runtime Required.NET runtime (CoreCLR)None — self-contained
Use CasesGeneral-purpose appsCloud, microservices, serverless, CLI tools

๐Ÿงฎ Example Build Flow

๐Ÿ”ธ CoreCLR-based publish:

dotnet publish -r linux-x64 -c Release

Produces:

  • app.dll

  • dotnet host loads CoreCLR runtime

  • JIT compiles IL on startup

๐Ÿ”ธ NativeAOT publish:

dotnet publish -r linux-x64 -c Release /p:PublishAot=true

Produces:

  • A single app executable

  • Contains native code + stripped-down runtime

  • Runs directly via ./app (no dotnet needed)


๐Ÿงฉ Why this matters

✅ Advantages:
  • Blazing-fast cold starts (critical for AWS Lambda, Azure Functions, CLI tools)
  • No runtime dependency (great for container deployment)
  • Reduced attack surface (no JIT, less metadata)
⚠️ Trade-offs:
  • No runtime code generation (System.Reflection.Emit)
  • Limited dynamic loading
  • Reflection-heavy frameworks (like some ORMs or JSON serializers) need trimming-safe versions

๐Ÿ’ก Internal Implementation Hint

In .NET 7/8, the AOT toolchain (crossgen2) builds an object file (.obj) that includes:

  • Compiled IL → native code

  • Static GC and metadata tables

  • Lightweight runtime startup code (from NativeAOT runtime)

Then it uses the system’s C++ linker to produce a final executable:

App.obj + NativeAOT runtime libs → FinalApp.exe

So the “runtime” is baked in, not “loaded dynamically” like CoreCLR.


๐Ÿ”ง Analogy

ModeAnalogy
CoreCLR (JIT)Like a chef who cooks each dish when ordered — flexible, but slower startup.
R2R (Partial AOT)Chef preps ingredients ahead of time — faster serving.
NativeAOT (Full AOT)Meals fully cooked, sealed, and ready to serve — instant serving, no kitchen needed.

๐Ÿงพ Summary

AspectCoreCLR (.NET Runtime)NativeAOT
Uses JIT?✅ Yes❌ No
Runtime EngineCoreCLRNativeAOT runtime (based on CoreRT)
Startup Speed⚡ Moderate⚡⚡ Instant
Reflection Support✅ Full⚠️ Limited
Runtime Size๐Ÿงฑ Larger๐Ÿ’ก Small and static
OutputIL + runtimeNative binary
Target ScenariosGeneral apps, APIsCloud-native, microservices, serverless, tools

Monday, October 27, 2025

.Net Framework Vs .Net Core


1. What They Are

PlatformDescription
.NET FrameworkThe original .NET platform released by Microsoft in 2002. It runs only on Windows and is used for building desktop (WPF, WinForms) and web (ASP.NET) apps.
.NET CoreA modern, cross-platform, open-source reimplementation of .NET, introduced in 2016. Runs on Windows, Linux, and macOS, designed for cloud and container environments.

2. Platform Support

Feature.NET Framework.NET Core
OS SupportWindows onlyCross-platform (Windows, Linux, macOS)
DeploymentInstalled system-wide (via Windows Update)Can be installed per app (self-contained)
Container SupportLimitedFully supported (Docker/Kubernetes friendly)
Mobile / IoT / CloudNot supported directlySupported via .NET (Core → 5/6/7/8) ecosystem

Summary: .NET Core is built for modern, cloud-native and cross-platform development.


3. Runtime & Architecture

Area.NET Framework.NET Core
RuntimeCommon Language Runtime (CLR)CoreCLR (lightweight, modular)
Base Class Library (BCL)Windows-specific APIsModular libraries (System.* NuGet packages)
CompilationJIT (Just-In-Time) onlyJIT + AOT (Ahead-of-Time) with ReadyToRun support
PerformanceHeavier, slower startupOptimized for speed and scalability
App IsolationShares runtimeEach app can have its own runtime version

Summary: .NET Core uses a modular, faster, and isolated runtime.


4. Application Models Supported

Application Type.NET Framework.NET Core
ASP.NET Web Forms✅ Yes❌ No
ASP.NET MVC / Web API✅ Yes✅ Rebuilt as ASP.NET Core
WPF / Windows Forms✅ Yes✅ Yes (Windows-only starting .NET Core 3.0+)
Console Apps✅ Yes✅ Yes
Windows Services✅ Yes✅ Yes (.NET 6+)
Cross-platform (Linux/Mac)❌ No✅ Yes
Blazor / MAUI / Minimal APIs❌ No✅ Yes

Summary: .NET Core adds support for new app models and modern architectures like microservices and serverless.


5. Package & Dependency Management

Feature.NET Framework.NET Core
Package ManagerGAC (Global Assembly Cache) + NuGetNuGet only
DeploymentShared assemblies (can cause version conflicts)Self-contained or framework-dependent
VersioningMachine-wideApp-local, side-by-side

Summary: .NET Core eliminates the old “DLL Hell” problem — each app can carry its own dependencies.


6. CLI Tools & Development Experience

Feature.NET Framework.NET Core
Command-line ToolsLimiteddotnet CLI powerful toolchain
Build SystemMSBuild onlyMSBuild + cross-platform CLI
Project Format.csproj (verbose XML)Simplified .csproj (SDK-style)
IDE SupportVisual Studio (Windows only)VS, VS Code, Rider (cross-platform)

Summary: .NET Core offers a modern, developer-friendly toolchain with CLI-first design.


7. Open Source & Community

Feature.NET Framework.NET Core
Open SourcePartially (reference source only)Fully open source (MIT License)
DevelopmentMicrosoft-onlyMicrosoft + .NET Foundation community
Source CodeNot on GitHub100% on Github

Summary: .NET Core is open, transparent, and community-driven.


8. Performance

Area.NET Framework.NET Core
StartupSlowerFaster
ThroughputModerateOptimized
Memory FootprintHigherLower
Async & ParallelismLegacy supportNative async-friendly design
Container StartupPoorExcellent

Summary: .NET Core’s runtime and libraries are optimized for microservices and high-performance workloads.


9. Future & Support Lifecycle

Feature.NET Framework.NET Core / .NET 5+
Active DevelopmentMaintenance mode onlyOngoing (current: .NET 8, .NET 9 soon)
New Features❌ No new features✅ Continuous updates
End of Life4.8 is the last major versionUnified platform (Core → 5 → 6 → 7 → 8...)

Summary:
➡️ .NET Framework = Legacy (maintenance only)
➡️ .NET Core / .NET 5+ = Future of .NET


10. Real-world Example

ScenarioUse .NET FrameworkUse .NET Core / .NET 6+
Maintaining an old enterprise ASP.NET Web Forms app
Building a new cross-platform Web API
Deploying to Linux or Docker
Creating a modern microservices architecture
Integrating with legacy Windows-only components❌ (or limited)

Summary Table

Feature.NET Framework.NET Core / .NET 6+
PlatformWindows onlyCross-platform
Open SourceLimitedFully open source
PerformanceModerateHigh performance
DeploymentSystem-wideSelf-contained
FutureLegacyActive development
App ModelsLimitedModern (Blazor, MAUI, etc.)

Final Takeaway

  • .NET Framework → Best for existing legacy apps that run on Windows only.

  • .NET Core / .NET 6+ → The future of .NET: cross-platform, open-source, high-performance, and cloud-ready.

Tuesday, April 6, 2021

Weak & Strong Reference

Weak Reference: If we references of some other dlls/assemblies in our program and those assemblies are not signed then the reference to those assembly called Weak Reference.
Why? : A same name assembly with same namespace and class names can be replaced with original one. Our program can't identify the Fake assembly.
If we sign the assembly then our program also check the signed key of the reference assembly.

Strong Reference: Reference a signed assembly in our program is Strong Reference.

Tuesday, October 20, 2015

Garbage Collector

What is Garbage Collection?
Garbage collection is a service to freeing up memory space occupied by dead objects or objects not in use from a long time.
It is performed by 'Garbage Collector' that is nothing but a background thread. Garbage Collector is part of CLR.
Garbage Collector maintain a heap of created object for your application, it periodically checks and de-allocate memory occupied by objects no longer required (dead or not used by long time)

When it perform collection?
  1. Periodically
  2. There is predefined threshold(memory limit) for heap of your application, if memory occupied cross that limit  
  3. When physical memory become low
What are Generations?
GC maintain a heap of objects for your application in that it categories objects in three generations,
generation 0, generation 1 & generation 2.

Newly created objects marked with generation 0. Big size newly created objects placed in Generation 1 & 2 with the assumption that they will have long life.

GC performs collection on generation 0, it releases memory from dead objects & moves alive objects to generation 1.
GC performs collection on generation 1, it releases memory from dead objects & moves alive objects to generation 2
GC performs collection on generation 2, it releases memory from dead objects.

0 Generation objects are short life object
1 Generation objects are middle life object
2 Generation objects are long life object
All short live objects resides in 0th generation so GC performs collection on 0th generation more than 1st generation, and so on..

Sunday, October 18, 2015

Managed Vs Unmanaged Code

Managed Code: Code written in any .net language run under CLR environment is managed code.

CLR provides it's services to manged code
  1. Garbage Collection
  2. Error Handling
  3. CTS (Common Type System)
  4. CAS (Code Access Security)
  5. Code Verification 
  6. Performance Improvement: Decides which JIT to use & assigns to JIT. JIT compiles IL to native m\c code.
Unmanaged Code: Code that does not target CLR environment for execution is UnManaged code.

Saturday, October 17, 2015

CLR, CLS, MSIL, CTS & JIT

MSIL (Microsoft Intermediate Language): An intermediate language generated by .net compilers (C#,VB,VC++...) when compiles source code.

CLR (Common Language Runtime): CLR is runtime environment of .net responsible to compile & manage .net code. Verifies the code for security, provide code access security means restrict code to access restricted area & resources of system, provides garbage collection service.

CLS (Common Lanuage Specifications): There is a defined set of specifications that should be followed by every compiler who is targeting to communicate with .net languages.

CTS (Common Type System): There is a defined set of Types, every .net language compiler compiles code with these common data types, so that all .net languages can communicate.
After compilation IL code of every .net language code, have common types.

JIT (Just In time Compiler): JIT is responsible to compile MSIL code into native machine code and save it in memory.

Types of JIT compiler
  1. Pre JIT  - Compiles whole code in a single cycle.
  2. Econo JIT - Compiles code part by part when required. It means compiles methods those called at runtime, and then remove those compiled code when not required. That means called code compiles, serves and then frees.
  3. Normal JIT - Compiles code part by part as Econo but only once when any code part called first time, then it put that code to cache and if required later, then serves from cache that part.
    Normal JIT = Econo JIT + Caching


CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...