Skip to content

3.6 — Assemblies, Load Contexts, NuGet Packaging

← Roadmap · Section: 3. .NET Runtime and Platform Prev: 3.5 Memory leaks · Next: 3.7 csproj and target frameworks

Markers: — · Confidence: 🔴 · Last reviewed: —


TL;DR

An assembly is a single compiled .dll or .exe. It is the unit .NET actually loads, versions and ships — one project produces exactly one assembly.

  • A NuGet package is a zip file containing one or more assemblies plus metadata about its dependencies.
  • The runtime finds assemblies at startup using a generated file called deps.json, not by searching folders.
  • Two different versions of the same package in one app cause a diamond dependency conflict, which .NET resolves by picking the highest version.

Definitions

Term What it is What it means
Assembly a .dll or .exe file The compiled output of one project. Contains IL plus metadata. The unit of deployment and versioning.
Metadata data inside the assembly A full description of every type, method and field. Reflection reads this.
Manifest part of the metadata The assembly's own identity: name, version, culture, and what it depends on.
NuGet package a .nupkg file A zip containing assemblies, a .nuspec describing them, and a list of dependencies.
PackageReference an entry in your .csproj How you declare a NuGet dependency in modern .NET.
deps.json a generated file Sits next to your app and lists every assembly it needs. The runtime reads this to find them.
runtimeconfig.json a generated file Says which .NET version the app needs and carries runtime settings like GC mode.
Strong name a signature An assembly signed with a key pair. Mostly a .NET Framework concern; largely irrelevant now.
AssemblyLoadContext an abstract class An isolated container for loading assemblies. Lets you load two versions of the same library, or unload one.
Diamond dependency a conflict Two of your dependencies each need a different version of the same third package.
Transitive dependency an indirect dependency A package your package depends on. You get it without asking.
InternalsVisibleTo an attribute Opens internal types to a named assembly, usually a test project.

The concept

What an assembly actually is

When you build a project, you get one assembly — a .dll (a library) or .exe (a runnable app). It is not just compiled code. Inside are two things:

  1. IL — your compiled code (3.2).
  2. Metadata — a complete description of every type, method, property and field.

That metadata is why .NET can do things other platforms find hard. Reflection reads it. The debugger reads it. IntelliSense reads it when you reference a package. A decompiler reads it to reconstruct readable C#.

An assembly is also the unit of three things:

  • Deployment — you ship .dll files.
  • Versioning — each assembly has its own version number.
  • Access control — internal means "visible inside this assembly", so the boundary is real (1.4).

The difference between an assembly and a package

People use these interchangeably and they are not the same.

An assembly is one file. Newtonsoft.Json.dll.

A NuGet package is a container. Newtonsoft.Json.13.0.3.nupkg — a zip file holding assemblies for several target frameworks, plus a description of what it depends on.

A picture: the assembly is the book; the package is the box it ships in. One box may hold several editions of the book — one for .NET 8, one for .NET Standard 2.0 — and a note saying which other boxes you also need.


How it works

The simple version — referencing things

Modern .NET uses PackageReference directly in the .csproj:

<ItemGroup>
  <PackageReference Include="Serilog.AspNetCore" Version="8.0.0" />
  <ProjectReference Include="../MyApp.Core/MyApp.Core.csproj" />
</ItemGroup>

Two kinds of reference:

  • PackageReference — a NuGet package, downloaded from a feed.
  • ProjectReference — another project in your solution, built alongside yours.

Transitive dependencies come along automatically. Referencing Serilog.AspNetCore also gives you Serilog itself and several sinks, without you naming them. That is convenient and occasionally surprising — dotnet list package --include-transitive shows the full picture, and is worth running when you are auditing what your app actually pulls in.

How it actually works — how assemblies get found at runtime

This surprises people who remember .NET Framework. The runtime does not search folders.

When you build, the SDK generates a deps.json next to your app listing every assembly it needs and where to find it:

{
  "targets": {
    ".NETCoreApp,Version=v10.0": {
      "MyApp/1.0.0": {
        "dependencies": { "Serilog": "4.0.0" },
        "runtime": { "MyApp.dll": {} }
      }
    }
  }
}

At startup the host reads that file and resolves everything from it. So:

  • Dropping a .dll into the output folder does nothing. If it is not in deps.json, the runtime will not load it.
  • A missing entry gives you FileNotFoundException at runtime, not at build.

Alongside it sits runtimeconfig.json, which says which .NET version to run on and carries runtime settings:

{
  "runtimeOptions": {
    "tfm": "net10.0",
    "framework": { "name": "Microsoft.NETCore.App", "version": "10.0.0" },
    "configProperties": {
      "System.GC.Server": true
    }
  }
}

That System.GC.Server line is the Server GC setting from 3.4 — it ends up here.

Version conflicts — the diamond problem

The most common real problem with packages:

Your app
├── PackageA  →  needs Newtonsoft.Json 12.0.0
└── PackageB  →  needs Newtonsoft.Json 13.0.0

Only one version can be loaded, because assembly identity includes the version. .NET resolves this by picking the highest — 13.0.0 — and hoping PackageA still works.

Usually it does, because most libraries are backwards compatible. When it does not, you get MethodNotFoundException or TypeLoadException at runtime, which is a confusing failure because the code compiled fine.

You can force a version explicitly:

<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />

And <CentralPackageManagement> lets you pin versions once for a whole solution, which is worth doing on anything larger than a couple of projects.

The deeper detail — AssemblyLoadContext

Normally every assembly loads into one shared context and stays for the life of the process. That is fine for an application.

AssemblyLoadContext gives you an isolated container instead. It solves two problems:

Loading two versions of the same assembly. Impossible in the default context, since identity includes version. Separate contexts each get their own copy.

Unloading. A collectible context can be released, and its assemblies unloaded, which is how plugin systems avoid growing forever.

var context = new AssemblyLoadContext("PluginContext", isCollectible: true);

var assembly = context.LoadFromAssemblyPath(pluginPath);
var type = assembly.GetType("Plugin.Entry");
var plugin = (IPlugin)Activator.CreateInstance(type)!;

plugin.Run();

context.Unload();      // assemblies become eligible for collection

⚠️ Unloading only works if nothing outside the context still references anything inside it. One lingering reference — a cached type, an event subscription, a static field — and the whole context stays loaded. This is a memory leak that is genuinely hard to diagnose, and it is why plugin unloading has a reputation for being fiddly. See 3.5.

This replaced app domains, which .NET Framework used for isolation and which do not exist in modern .NET (3.1). App domains offered stronger isolation — including separate security boundaries — and AssemblyLoadContext deliberately does not. If you need real isolation now, the answer is a separate process or container.

Strong naming

A strong name is a cryptographic signature giving an assembly a unique identity. In .NET Framework it was required for the shared Global Assembly Cache.

In modern .NET it is mostly irrelevant. There is no GAC, assemblies are app-local, and strong naming provides no security guarantee — the signature proves the key holder built it, not that the code is safe. You still see it on Microsoft's own libraries for compatibility. For your own code, ignore it.


Code

Producing a NuGet package from a project:

<PropertyGroup>
  <TargetFramework>net10.0</TargetFramework>
  <PackageId>MyCompany.OrderClient</PackageId>
  <Version>1.2.0</Version>
  <Authors>Your Name</Authors>
  <Description>Client library for the Orders API.</Description>
  <PackageLicenseExpression>MIT</PackageLicenseExpression>
  <RepositoryUrl>https://github.com/you/orderclient</RepositoryUrl>

  <!-- Ship the XML doc file so consumers get IntelliSense on your API -->
  <GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
dotnet pack -c Release                      # produces bin/Release/*.nupkg
dotnet nuget push MyCompany.OrderClient.1.2.0.nupkg \
    --source https://api.nuget.org/v3/index.json \
    --api-key $NUGET_KEY

Useful commands when a dependency is misbehaving:

dotnet list package --include-transitive     # everything you actually depend on
dotnet list package --outdated               # what has newer versions
dotnet list package --vulnerable             # known CVEs — run this in CI
dotnet nuget locals all --clear              # nuke the cache when it lies to you

--vulnerable is worth wiring into your build pipeline. It is the cheapest security check available in .NET and catches known vulnerabilities in transitive dependencies you never chose directly. See 17.7 and 24.7.


Interview questions

Q: What is an assembly?

It is the compiled output of a single project — a .dll or .exe. It contains the IL your code compiled to, plus metadata fully describing every type and member inside. That metadata is what reflection, the debugger and IntelliSense all read. The assembly is also the unit of deployment, of versioning, and of access control — internal means visible within one assembly, so the boundary is real rather than conventional.

Q: What is the difference between an assembly and a NuGet package?

An assembly is a single file. A NuGet package is a zip container that holds one or more assemblies — often several builds of the same library for different target frameworks — plus metadata describing what else it depends on. So one package can deliver several assemblies, and installing it also pulls in its transitive dependencies automatically.

Q: How does the runtime find assemblies at startup?

Through the deps.json file the SDK generates next to your application, which lists every assembly the app needs. The host reads that and resolves from it — it does not scan folders the way .NET Framework did. A practical consequence is that dropping a .dll into the output directory achieves nothing if it is not listed there, and a missing entry surfaces as a FileNotFoundException at runtime rather than a build error.

Q: What happens when two packages need different versions of the same dependency?

That is a diamond dependency. Only one version can load, because assembly identity includes the version number, so .NET picks the highest and assumes backwards compatibility. Usually that works. When it does not you get a MethodNotFoundException or TypeLoadException at runtime, which is confusing because everything compiled. You can force a specific version with an explicit PackageReference, and on a larger solution central package management lets you pin versions in one place.

Q: What is AssemblyLoadContext for?

It is an isolated container for loading assemblies, and it solves two problems the default context cannot. It lets you load two different versions of the same assembly side by side, which is otherwise impossible because identity includes version. And a collectible context can be unloaded, so plugin systems can release code rather than accumulating it. The catch is that unloading only succeeds if nothing outside the context still holds a reference to anything inside it — one cached type or lingering event subscription keeps the whole thing loaded, which is a hard leak to track down.


Traps ⚠️

  • Copying a .dll into the output folder does nothing unless it is in deps.json.
  • Version conflicts fail at runtime, not build time. The code compiles, then throws MethodNotFoundException.
  • Transitive dependencies arrive without you asking. Run dotnet list package --include-transitive to see what you actually ship.
  • AssemblyLoadContext.Unload silently does nothing if any outside reference survives.
  • Strong naming provides no security. It proves who built it, not that it is safe.
  • App domains do not exist in modern .NET. Code expecting them will not port.
  • internal is per-assembly. Splitting one project into two changes what is visible.
  • Not running dotnet list package --vulnerable in CI means shipping known CVEs in packages you never chose.


Sources