C# & .NET
C# types, async execution and .NET application troubleshooting.
50 troubleshooting guides · Page 1 / 3
Why does try/catch around an async void helper miss its later exception?
An async void method has no returned task carrying completion to the caller. After its first asynchronous suspension, the caller cannot await or catch it like a Task-returning operation.
Why does .Result make an ASP.NET Core endpoint stall under load?
Blocking on an asynchronous operation ties up a worker thread while its I/O remains pending. Enough blocked requests can exhaust useful thread-pool capacity even without the classic UI synchronization-context deadlock.
How do I inspect multiple failures from Task.WhenAll without losing evidence?
Several child tasks can fail, while awaiting the combined task typically throws one propagated exception. Logging only that caught exception may hide other failures needed for diagnosis.
Why does CancellationTokenSource.Cancel not stop my CPU loop?
Cancellation is cooperative. A token signals a request; it cannot forcibly terminate code that never checks it or passes it to a cooperating operation.
Why can awaiting the same ValueTask twice fail only on some execution paths?
ValueTask can wrap sources with a single-consumption contract. A synchronous-completion path may conceal misuse that appears when a pooled asynchronous source is used.
Why does returning before DisposeAsync completes leave an incomplete output?
Asynchronous cleanup can perform required flush or shutdown work. Calling DisposeAsync without awaiting it abandons that completion signal.
Why does Task.Run inside an endpoint lose work after the response ends?
An unawaited in-process task is not a durable job. Request-scoped objects may be disposed, and application shutdown can end work after the caller already received success.
Why is capturing HttpContext for a delayed task unreliable?
HttpContext belongs to a request and is not an independent durable data record. Delayed execution can access it after the request ends or from unsupported concurrent paths.
How do I use a scoped DbContext in BackgroundService without keeping it forever?
A hosted background service has a longer lifetime than a request scope. Constructor injection of a scoped dependency into that owner creates an invalid or captive lifetime.
Why does injecting a transient stateful object into a singleton effectively retain it?
Transient describes resolution behavior, not a magical maximum lifetime. When a singleton stores the one instance it received, that instance can live as long as the singleton.
Should my service dispose an IDisposable dependency resolved by the DI container?
Disposal responsibility follows ownership. A container-managed dependency can be shared or scheduled for cleanup by its scope, so disposing it from a borrowing service can break other consumers.
Why do parallel EF Core queries throw a second-operation-on-context error?
A DbContext is not a parallel query coordinator. Starting another operation before the first finishes on the same instance violates its usage contract.
Why does modifying an AsNoTracking result not update the database?
A no-tracking query deliberately avoids recording the returned entity for ordinary change detection. Editing that detached object alone does not tell the context to persist an update.
How should an EF concurrency exception be handled without overwriting another editor?
An optimistic-concurrency conflict signals that the stored version no longer matches the version being updated. Blindly retrying the same overwrite can discard another user’s changes.
Why does enumerating a returned IQueryable fail after the service method exits?
The query describes work that may execute later. If its context was disposed before enumeration, returning the query leaked a dependency on an ended lifetime.
Why does enumerating a LINQ query twice repeat expensive work?
Deferred execution means enumeration can run the pipeline again. Counting a sequence and then iterating it may repeat I/O, computation or state-sensitive filtering.
Why does changing a threshold alter an already-created LINQ query?
A deferred lambda can read the captured variable when enumeration occurs, not when the query object was created. The query is not necessarily a frozen report specification.
Why should duplicate IDs be handled before creating a Dictionary?
ToDictionary expects one value per produced key. Duplicate keys are a domain ambiguity, not merely an exception to suppress.
Why does a Dictionary<string,T> miss a key with different casing?
Dictionary uses its configured equality comparer; it does not infer that an identifier should be case-insensitive. Normalizing only some call sites leaves inconsistent lookup behavior.
Why do equal-looking arrays inside a C# record make two records unequal?
Record equality delegates to equality of its members. An array member normally retains reference-based equality, so equal element contents in distinct arrays need not produce equal records.
Why does changing a list in a record copy change the original record too?
The synthesized copy used by a record with expression is shallow for reference-valued members. Both records can still refer to the same mutable list.
Why does a Dictionary lose lookup of a key after I edit the key object?
A hash-based key must keep equality and hash-relevant state stable while stored. Changing those fields can make lookup search under a different hash from insertion.
Why does editing a struct retrieved from a list not update the stored item?
A struct is a value type, so retrieving it by value can produce a copy. Updating that local copy does not replace the list element.
Why does boxing an empty nullable value produce null rather than Nullable<T>?
Boxing a nullable value follows its presence: no value boxes to null, while a present value boxes as the underlying value type. Reflection code expecting a boxed Nullable<T> can therefore make the wrong type assumption.
Ask your own question · Sign in to post questions and answers.