Start Debugging

dotnet-11

354 posts · Page 1 of 36

How does Microsoft.Extensions.DependencyInjection resolve a service internally, from AddSingleton to the instance?

AddSingleton only appends a ServiceDescriptor to a list. BuildServiceProvider indexes those descriptors, and the first GetService builds a call-site tree with reflection, caches it, and wraps it in an accessor delegate. Singletons are stored on the call site itself, scoped instances in a per-scope dictionary, and after the second resolve of a non-singleton the container compiles an IL DynamicMethod on the thread pool. Walked through the .NET 11 source and observed with reflection, EventSource events and BenchmarkDotNet.

Why can't I cast IDisplayVM<Derived> to IDisplayVM<Base> in C#, and when does out variance fix it?

A class that implements IDisplayVM<DiseaseDTO> does not implement IDisplayVM<IDTO>, so the assignment fails with CS0266, 'as' returns null and an explicit cast throws InvalidCastException. Marking T as 'out' fixes it only if T never appears in an input position; with ICollection<T> or a setter you get CS1961. Reproduced on .NET 11 with the four designs that actually work, benchmarked.

Why does EF Core forbid concurrent reads on one DbContext, even with AsNoTracking?

A read is not read-only inside a DbContext: every query mutates the shared connection, its open counter, the command and reader state, and for tracking queries the identity map. With thread-safety checks turned off on EF Core 11.0.0, parallel AsNoTracking queries on one context still failed up to 57 times in 1,000 rounds, and tracking queries left a corrupted change tracker in all but one of the rounds that finished.
Next