Courseiva

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

    A missing await would surface as an incomplete Task or unexpected ordering, not a null _dbContext field. Unawaited calls are the classic cause of race conditions in async code, but a null injected dependency points to dependency injection registration or disposal instead.

  • ✓

    The DbContext is not registered in the dependency injection container.

    Why this is correct

    A null _dbContext at runtime indicates the dependency injection container never supplied the instance. If DbContext were registered, constructor injection would populate it; its absence means the service was not added to the container during startup configuration.

  • ✗

    The database connection string is invalid in appsettings.json.

    Why it's wrong here

    A null _dbContext indicates the dependency was never injected or constructed, not that its connection string was rejected — an invalid string would throw a configuration or SQL exception after successful instantiation. Connection-string validation belongs to configuration troubleshooting, where the context object itself exists but cannot open a database session.

  • ✗

    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

About these practice questions

One of 696 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 →

How Courseiva writes practice questions · Editorial policy

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.