WireMock.Net.AspNetCore.Middleware does not work in hosted services #685

Closed
opened 2025-12-29 08:32:29 +01:00 by adam · 0 comments
Owner

Originally created by @etkr on GitHub (Apr 24, 2025).

Describe the bug

WireMock AspNetCore Middleware works great when a request is sent from within a request thread, but if a request is sent from a background service it throws this exception:

System.ArgumentNullException: Value cannot be null. (Parameter '_httpContextAccessor.HttpContext')
   at Stef.Validation.Guard.NotNull[T](T value, String parameterName)
   at WireMock.Net.AspNetCore.Middleware.HttpDelegatingHandler.WireMockDelegationHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.<SendCoreAsync>g__Core|5_0(HttpRequestMessage request, Boolean useAsync, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)

What is happening is that HttpContext is not available in a hosted service, it is a nullable property of IHttpContextAccessor since it is not always guaranteed to be available.

Expected behavior:

It should be possible to make http requests in a background service while WireMock Middleware is enabled.

Additionally it should be possbile to mock requests coming from a background service.

Test to reproduce

  • Enable WireMock AspNetCore Middleware with AddWireMockService
  • Make any http request in a hosted service
  • Oberve that ArgumentNullException is thrown from HttpClient

I created a PR #1285 with a test that reproduces this bug and a proposed solution.

Originally created by @etkr on GitHub (Apr 24, 2025). ### Describe the bug WireMock AspNetCore Middleware works great when a request is sent from within a request thread, but if a request is sent from a background service it throws this exception: ``` System.ArgumentNullException: Value cannot be null. (Parameter '_httpContextAccessor.HttpContext') at Stef.Validation.Guard.NotNull[T](T value, String parameterName) at WireMock.Net.AspNetCore.Middleware.HttpDelegatingHandler.WireMockDelegationHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.<SendCoreAsync>g__Core|5_0(HttpRequestMessage request, Boolean useAsync, CancellationToken cancellationToken) at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken) ``` What is happening is that `HttpContext` is not available in a hosted service, it is a nullable property of `IHttpContextAccessor` since it is not always guaranteed to be available. ### Expected behavior: It should be possible to make http requests in a background service while WireMock Middleware is enabled. Additionally it should be possbile to mock requests coming from a background service. ### Test to reproduce * Enable WireMock AspNetCore Middleware with `AddWireMockService` * Make any http request in a hosted service * Oberve that `ArgumentNullException` is thrown from `HttpClient` ### Other related info I created a PR #1285 with a test that reproduces this bug and a proposed solution.
adam added the bug label 2025-12-29 08:32:29 +01:00
adam closed this issue 2025-12-29 08:32:29 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#685