주제: csharp
-
.NET 의존성 주입 관례: 라이프타임, 스코프, 그리고 captive dependency 함정
Microsoft.Extensions.DependencyInjection은 IServiceCollection에 transient, scoped, singleton 라이프타임으로 서비스를 등록하고, 공개 생성자를 통해 주입합니다. 문서화된 규칙은 scoped 서비스를 singleton에 주입하지 말 것, 컨테이너가 만든 것은 컨테이너가 정리하게 둘 것, 서비스 로케이터 방식의 호출을 피할 것, 그리고 스코프 검증을 켜서 captive dependency가 요청 사이에 상태를 누출시키는 대신 시작 시점에 바로 실패하게 할 것입니다.
-
Writing a unit test in JUnit 5 and xUnit.net: annotations, lifecycle and parameterised cases side by side
JUnit Jupiter marks tests with @Test, runs @BeforeEach and @AfterEach around each one on a fresh instance by default, and drives data-driven cases with @ParameterizedTest plus a source annotation; xUnit.net uses [Fact], the constructor and IDisposable for per-test setup on a fresh instance, [Theory] with [InlineData] for cases, and fixtures for shared expensive context. Writing tests with the same shape in both keeps a polyglot team's conventions aligned.
-
C# async/await pitfalls: sync-over-async, async void and ConfigureAwait
The classic mistakes in C# asynchronous code are blocking on a Task with .Result, .Wait() or GetAwaiter().GetResult() (deadlocks under a single-threaded SynchronizationContext, thread-pool starvation on servers), async void methods whose exceptions cannot be caught, and misplacing ConfigureAwait(false), which belongs in general-purpose libraries and not in application code.
-
Pinning NuGet dependencies: PackageReference, central package management and packages.lock.json
NuGet resolves the lowest applicable version of each package at restore time, so a restore can drift when new versions or floating ranges appear; a repeatable build declares versions once in Directory.Packages.props, enables RestorePackagesWithLockFile so packages.lock.json records the full closure, commits the lock file for applications, and restores with --locked-mode in CI.
-
Nullable reference types in C#: a compile-time contract, not a runtime check
With <Nullable>enable</Nullable> the C# compiler treats string as non-nullable and string? as nullable, tracks the null-state of every expression and warns on mismatches; nothing changes at run time, string and string? are the same type, and the null-forgiving operator ! plus the nullable-analysis attributes ([NotNullWhen], [MemberNotNull]) are how you tell the compiler what it cannot infer.
기계 판독 가능: JSON