[PR #1285] [MERGED] Enable support for WireMock Middleware in Hosted Services #1315

Closed
opened 2025-12-29 09:19:16 +01:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/wiremock/WireMock.Net/pull/1285
Author: @etkr
Created: 4/24/2025
Status: Merged
Merged: 4/25/2025
Merged by: @StefH

Base: masterHead: feature/wiremock-middleware-hosted-service-support


📝 Commits (1)

  • 021a03d Enable support for WireMock Middleware in Hosted Services

📊 Changes

5 files changed (+94 additions, -3 deletions)

View changed files

📝 src/WireMock.Net.AspNetCore.Middleware/HttpDelegatingHandler/WireMockDelegationHandler.cs (+16 -3)
📝 test/WireMock.Net.Middleware.Tests/IntegrationTests.cs (+1 -0)
📝 test/WireMock.Net.TestWebApplication/Program.cs (+15 -0)
test/WireMock.Net.TestWebApplication/TaskQueue.cs (+38 -0)
test/WireMock.Net.TestWebApplication/TestBackgroundService.cs (+24 -0)

📄 Description

Background

While attempting to use Wiremock Middle in an application that uses hosted services i noticed that it breaks and throws an ArgumentNullException .

See #1284

Problem

The problem is that IHttpContextAccessor.HttpContext can not be relied upon to always be available.

It would be best if WireMockDelegationHandler can fallback gracefully if HttpContext is null, which is expected in some cases.

Solution

I changed IsWireMockRedirectHeaderSetToTrue and TryGetDelayHeaderValue in WireMockDelegationHandler to default to false if HttpContext is null.

I created a test to check that it works when started in a hosted service.

Limitations

The X-WireMock-Redirect and X-WireMock-Response-Delay headers are obviously not going to work as expected when mocking a request from a background service, maybe this needs to be documented somewhere.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/wiremock/WireMock.Net/pull/1285 **Author:** [@etkr](https://github.com/etkr) **Created:** 4/24/2025 **Status:** ✅ Merged **Merged:** 4/25/2025 **Merged by:** [@StefH](https://github.com/StefH) **Base:** `master` ← **Head:** `feature/wiremock-middleware-hosted-service-support` --- ### 📝 Commits (1) - [`021a03d`](https://github.com/wiremock/WireMock.Net/commit/021a03d32017f99475f9b658c77421926bec3d01) Enable support for WireMock Middleware in Hosted Services ### 📊 Changes **5 files changed** (+94 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `src/WireMock.Net.AspNetCore.Middleware/HttpDelegatingHandler/WireMockDelegationHandler.cs` (+16 -3) 📝 `test/WireMock.Net.Middleware.Tests/IntegrationTests.cs` (+1 -0) 📝 `test/WireMock.Net.TestWebApplication/Program.cs` (+15 -0) ➕ `test/WireMock.Net.TestWebApplication/TaskQueue.cs` (+38 -0) ➕ `test/WireMock.Net.TestWebApplication/TestBackgroundService.cs` (+24 -0) </details> ### 📄 Description ## Background While attempting to use Wiremock Middle in an application that uses hosted services i noticed that it breaks and throws an `ArgumentNullException` . See #1284 # Problem The problem is that `IHttpContextAccessor.HttpContext` can not be relied upon to always be available. It would be best if `WireMockDelegationHandler` can fallback gracefully if `HttpContext` is null, which is expected in some cases. # Solution I changed `IsWireMockRedirectHeaderSetToTrue` and `TryGetDelayHeaderValue` in `WireMockDelegationHandler` to default to false if `HttpContext` is null. I created a test to check that it works when started in a hosted service. # Limitations The `X-WireMock-Redirect` and `X-WireMock-Response-Delay` headers are obviously not going to work as expected when mocking a request from a background service, maybe this needs to be documented somewhere. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2025-12-29 09:19:16 +01:00
adam closed this issue 2025-12-29 09:19:16 +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#1315