Skip to content

Dependency Injection example 3 - CurrentDomain.GetAssemblies() #333

@dluc

Description

@dluc

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions