AZ-400 Dependency Injection in ASP.NET Core Practice Question
Exhibit
Refer to the exhibit.
```
Application Insights Snapshot Debugger
Snapshot 1:
Thread: 1234
Exception: NullReferenceException
Stack:
Contoso.Web.Pages.Index.OnGet() line 42
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeHandlerMethod()
...
Local variables:
_dbContext: null (Contoso.Data.AppDbContext)
```You are debugging a production issue using Application Insights Snapshot Debugger. The exhibit shows a snapshot from a NullReferenceException. The variable _dbContext is null. What is the most likely root cause?
⚠ Common exam trap
Candidates may mistakenly choose Option D (missing null check) because it directly prevents the exception, but the root cause is the missing DI registration.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
The DbContext is not registered in the dependency injection container.
In ASP.NET Core, when a constructor-injected service like a DbContext is null at runtime, the most likely root cause is that the service (the DbContext) has not been registered in the dependency injection container. Page models themselves do not require registration. The missing null check (Option D) is a symptom, not the root cause, and the other options are unrelated.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
The call to the database is not awaited, causing a race condition.
Why it's wrong here
Incorrect. A race condition from an unawaited database call would not cause _dbContext to be null; it would cause unexpected behavior or exceptions on the database operation itself.
- ✓
The DbContext is not registered in the dependency injection container.
Why this is correct
Incorrect. In Razor Pages, the page model (IndexModel) is automatically instantiated by the framework. It does not need to be registered in the DI container for construction injection to work.
- ✗
The database connection string is invalid in appsettings.json.
Why it's wrong here
Incorrect. An invalid database connection string would cause a failure when trying to open a connection (e.g., SqlException), not a NullReferenceException on the DbContext instance.
- ✗
The OnGet method is missing a null check for _dbContext before usage.
Why it's wrong here
Incorrect. A missing null check is a coding oversight that would allow the NullReferenceException to be thrown, but it is not the root cause. The root cause is that _dbContext is null because the DbContext was never injected.
Go deeper
Related to this question
Learn chapter
Managing Infrastructure as Code Using Azure
Key term
Check
A Check in Azure DevOps is a gating mechanism that evaluates predefined conditions before allowing a pipeline deployment to proceed to a specific environment.
Key term
Application Insights
Application Insights is an Azure monitoring service that helps developers detect, diagnose, and understand issues in live web applications by collecting telemetry data like requests, exceptions, and performance counters.
About these practice questions
One of 823 original AZ-400 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This AZ-400 practice question is part of Courseiva's free Microsoft certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the AZ-400 exam.