dodyg / dodyg/practical-aspnetcore

Dependency Injection example 3 - CurrentDomain.GetAssemblies()

Open
#333 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
10.4k
Forks
1.4k
PR merge metrics
No merged PRs in 30d

Description

Looking at https://github.com/dodyg/practical-aspnetcore/blob/net6.0/projects/dependency-injection/dependency-injection-3/Program.cs - as far as I know this code
```
var types = System.AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(x => x.GetTypes())
.Where(p => type.IsAssignableFrom(p) && p.IsClass);
```
will find only types in assemblies loaded to *this moment*. For perf reason, .NET loads assemblies only when needed, so `GetAssemblies()` is often only a subset of all the app dependencies/assemblies.

A couple of options (not sure if there are more or if there's a way to force all assemblies to be discoverable):
1. introducing static references to all assemblies, so they are loaded when the app starts
2. scanning the folder and manually load each assembly before calculating `types` (caching the result if possible, so this is done only once or on demand)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.