MVFC.Aspire.Helpers.WireMock 13.1.0

dotnet add package MVFC.Aspire.Helpers.WireMock --version 13.1.0
                    
NuGet\Install-Package MVFC.Aspire.Helpers.WireMock -Version 13.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MVFC.Aspire.Helpers.WireMock" Version="13.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MVFC.Aspire.Helpers.WireMock" Version="13.1.0" />
                    
Directory.Packages.props
<PackageReference Include="MVFC.Aspire.Helpers.WireMock" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MVFC.Aspire.Helpers.WireMock --version 13.1.0
                    
#r "nuget: MVFC.Aspire.Helpers.WireMock, 13.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MVFC.Aspire.Helpers.WireMock@13.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MVFC.Aspire.Helpers.WireMock&version=13.1.0
                    
Install as a Cake Addin
#tool nuget:?package=MVFC.Aspire.Helpers.WireMock&version=13.1.0
                    
Install as a Cake Tool

MVFC.Aspire.Helpers.WireMock

🇧🇷 Leia em Português

CI codecov License Platform NuGet Version NuGet Downloads

Helpers for integrating WireMock.Net in .NET Aspire projects, facilitating API mocking for development, testing, and integration.

Motivation

Mocking HTTP APIs locally often involves:

  • Running WireMock.Net manually or as a separate console app.
  • Scattering mock configuration across JSON files or test projects.
  • No clear place to manage the mock lifecycle with the rest of your topology.

With .NET Aspire you can orchestrate resources, but you still need to:

  • Start/stop the mock along with your app.
  • Configure endpoints, methods and auth consistently.
  • Wire other projects to talk to the mock.

MVFC.Aspire.Helpers.WireMock addresses this by:

  • AddWireMock(...) to run WireMock.Net as an embedded server in Aspire.
  • A fluent API to configure endpoints, auth, headers, body types and responses.
  • WithReference(...) to inject the mock connection string; use WaitFor(wireMock) explicitly for a startup dependency.

Overview

This project allows easily adding a WireMock.Net server as a managed resource in distributed .NET Aspire applications. It simplifies provisioning, lifecycle management, and exposing mocked HTTP endpoints, while also allowing custom configuration and publishing state/log events.

WireMock helper advantages

  • Simulates external/local APIs for testing and integration.
  • Allows defining endpoints, methods, authentication, and custom responses.
  • Facilitates decoupled development and automated testing.
  • Manages the WireMock resource lifecycle within the Aspire environment.

Project Structure

Features

  • Adds a WireMock resource to the Aspire application with managed lifecycle.
  • Allows detailed configuration of mocked endpoints.
  • Support for authentication (Bearer, custom headers).
  • Configuration of body types, headers, status codes, and errors.
  • Publishes resource state and log events.

Compatible Images

  • Uses WireMock.Net as an embedded server (no Docker image required).

Installation

dotnet add package MVFC.Aspire.Helpers.WireMock

Endpoint configuration examples

You can configure mocked endpoints with different HTTP methods, body types, authentication, headers, and custom responses.

  • Bearer authentication:
server.Endpoint("/api/secure")
       .RequireBearer("mytoken", "Unauthorized", BodyType.String)
       .OnGet(() => ("Secret Data", HttpStatusCode.OK, BodyType.String));
  • Custom headers:
server.Endpoint("/api/headers")
      .WithResponseHeaders(new() { { "X-Test", ["v1", "v2"] } })
      .OnGet(() => ("Headers OK", HttpStatusCode.OK, BodyType.String));

Supported body types include String, Json, Bytes, FormUrlEncoded, etc.

Provisioning diagram

sequenceDiagram
    participant Aspire as .NET Aspire
    participant Server as Embedded WireMock.Net server
    participant App as Consumer

    Aspire->>Server: AddWireMock starts the in-process server
    Aspire->>Server: Apply configure callback
    Aspire->>Aspire: BeforeStartEvent publishes resource state and ResourceReadyEvent
    Aspire->>App: Inject HTTP connection string through native WithReference
    Note over Aspire,App: Add WaitFor(wireMock) explicitly when startup must wait

Ports and access

  • Port: defined via port parameter (e.g. 8080).
  • Access: http://localhost:<port>/api/....

Public methods

  • AddWireMock – adds the WireMock resource to the distributed application and lets you configure endpoints.
var wireMock = builder.AddWireMock("wireMock", port: 8080, configure: ...);

Complete Aspire usage example (AppHost)

using System.Net;
using System.Text.Json.Nodes;
using Aspire.Hosting;
using WireMock.Types;
using MVFC.Aspire.Helpers.WireMock;

var builder = DistributedApplication.CreateBuilder(args);

var wireMock = builder.AddWireMock("wireMock", port: 8080, configure: (server) =>
{
    server.Endpoint("/api/echo")
          .WithDefaultBodyType(BodyType.String)
          .OnPost<string, string>(body => ($"Echo: {body}", HttpStatusCode.Created, null));

    server.Endpoint("/api/test")
          .WithDefaultBodyType(BodyType.String)
          .OnGet<string>(() => ("Aspire GET OK", HttpStatusCode.OK, null));

    server.Endpoint("/api/secure")
           .RequireBearer("mytoken", "Unauthorized", BodyType.String)
           .OnGet(() => ("Secret Data", HttpStatusCode.OK, BodyType.String));

    server.Endpoint("/api/put")
           .WithDefaultBodyType(BodyType.String)
           .OnPut<string, string>(req => ($"Echo: {req}", HttpStatusCode.Accepted, BodyType.String));

    server.Endpoint("/api/customauth")
        .WithDefaultErrorStatusCode(HttpStatusCode.Forbidden)
        .RequireCustomAuth(req => (req.Headers!.ContainsKey("X-Test"), "Forbidden", BodyType.String))
        .OnGet(() => ("Authorized", HttpStatusCode.OK, BodyType.String));

    server.Endpoint("/api/headers")
        .WithResponseHeaders(new() { { "X-Test", ["v1", "v2"] } })
        .WithResponseHeader("X-Other", "v3")
        .OnGet(() => ("Headers OK", HttpStatusCode.OK, BodyType.String));

    server.Endpoint("/api/error")
        .WithRequestBodyType(BodyType.String)
        .WithDefaultErrorStatusCode((HttpStatusCode)418)
        .OnGet(() => ("I am a teapot", (HttpStatusCode)418, BodyType.String));

    server.Endpoint("/api/delete")
       .WithResponseBodyType(BodyType.String)
       .WithResponseHeader("v1", "v1")
       .WithResponseHeaders(new() { { "v1", ["v2", "v3"] } })
       .WithResponseHeader("v1", "v4")
       .OnDelete<string>(() => (null!, HttpStatusCode.NoContent, null));

    server.Endpoint("/api/form")
        .WithDefaultBodyType(BodyType.FormUrlEncoded)
        .OnPost<Dictionary<string, string>, IDictionary<string, string>>(body => (body, HttpStatusCode.OK, BodyType.FormUrlEncoded));

    server.Endpoint("/api/form-wrong")
       .WithDefaultBodyType(BodyType.FormUrlEncoded)
       .OnPost<string, string>(body => (body, HttpStatusCode.OK, BodyType.FormUrlEncoded));

    server.Endpoint("/api/patch")
        .WithDefaultBodyType(BodyType.String)
        .OnPatch<string, string>(body => ($"Patched: {body}", HttpStatusCode.OK, BodyType.String));

    server.Endpoint("/api/bytes")
        .WithDefaultBodyType(BodyType.Bytes)
        .OnPost<byte[], byte[]>(body => (body, HttpStatusCode.OK, BodyType.Bytes));

    server.Endpoint("/api/unsupported")
        .WithDefaultBodyType((BodyType)999)
        .OnPost<string, string>(_ => ("Not Supported", HttpStatusCode.NotImplemented, null));

    server.Endpoint("/api/json")
        .WithDefaultBodyType(BodyType.Json)
        .OnPost<JsonObject, JsonObject>(body => (body, HttpStatusCode.OK, BodyType.Json));
});

await builder.Build().RunAsync();

Requirements

  • .NET 9, 10 or 11
  • Aspire.Hosting 13.6.5 on .NET 9; Aspire.Hosting 17.0.0 on .NET 10/11. AppHost examples use Aspire.Hosting.Dotnet 17 and AddDotnetProject; .NET 9 AppHosts retain the ProjectResource model.
  • Docker running locally
  • WireMock.Net.minimal >= 2.35.0

License

Apache-2.0

Readiness

The lifecycle hook observes the server created by the resource. It publishes the Running state and URL before the ready event, and publishes readiness only when the server is started. A stopped server reports Error, with no active URL or ready event. The hook does not restart a stopped server.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed.  net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
13.1.0 86 11/29/2026
13.0.0 99 11/28/2026
12.3.0 86 11/27/2026
12.2.1 96 11/27/2026
12.2.0 98 11/26/2026
12.1.0 94 11/26/2026
12.0.0 111 11/25/2026
11.0.2 94 11/21/2026
11.0.1 107 11/20/2026
11.0.0 96 11/13/2026
10.1.7 108 11/8/2026
10.1.6 96 11/8/2026
10.1.4 98 11/7/2026
10.1.3 123 11/1/2026
10.1.2 105 11/1/2026
10.1.1 112 10/31/2026
10.1.0 167 8/31/2026
10.0.0 139 8/30/2026
9.0.5 132 8/28/2026
9.0.4 162 6/26/2026
Loading failed