autofac/Autofac

IStartable in a child lifetime scope causes recompilation of factory delegates

Geschlossen

#1.461 geöffnet am 03.07.2025

 (5 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)C# (827 Forks)batch import
bughelp wanted

Repository-Metriken

Stars
 (4.327 Sterne)
PR-Merge-Metriken
 (Durchschn. Merge 3h 24m) (10 gemergte PRs in 30 T)

Beschreibung

Describe the Bug

An IStartable that depends on a Func dynamic instantiation recreates and compiles the factory delegate anew for every lifetime scope. In the Gist, FactoryGenerator.CreateGenerator is called exactly once for LifetimeWithoutStartable and a total of four times—again once, but also once per lifetime scope—in LifetimeWithStartable.

Steps to Reproduce

https://gist.github.com/Tragetaschen/0f951b32cea3aaf98c1e9b83953dfcf8

Expected Behavior

I don't see a reason why the factory delegate cannot be reused for the startable and would prefer to not pay the expression creation, compilation and native code again and again for every child scope.

Dependency Versions

Autofac: 8.3.0

Contributor Guide