← All languages

Servirtium for

.NET

Ships a built package on the GitHub release. Release v2.0.0-alpha.20.

FFI mechanism
P/Invoke (NativeLibrary). One C# assembly P/Invokes the aether_vcr_embed_* C ABI; the nupkg carries the engine under runtimes//native/ for eight RIDs (linux x64/arm64 glibc and musl, osx x64/arm64, win x64/arm64) and targets net8.0 and net48. C# and F# share it.
Toolchain
.NET 8 SDK or newer for net8.0 (verified here with the .NET 10 SDK, nupkg consumed on net10.0); .NET Framework 4.8 via the net48 target (64-bit only; SDK-style or packages.config projects)
Distribution
Ships: Servirtium.Vcr.2.0.0-alpha.20.nupkg (net8.0 + net48, eight RIDs); use the release directory as a local NuGet feed
Finding the library
Deployed from the package's runtimes/<rid>/native/ (NuGet, or build/net48/Servirtium.Vcr.targets on .NET Framework); no SERVIRTIUM_VCR_LIB. SERVIRTIUM_VCR_LIB wins over the packaged copy and is needed only for FreeBSD (no RID in the package).
On GitHub
dotnet/ · README

Getting it

This language has a built artifact on the release: no source to vendor.

  1. Put the release package in a local NuGet feed (checksum-verified) and declare it in a nuget.config. It is not on nuget.org yet. Run from your own project directory.
    set -eu
    VER=2.0.0-alpha.20
    REL=https://github.com/servirtium/servirtium-vcr/releases/download/v2.0.0-alpha.20
    mkdir -p nupkgs
    curl -fsSL -o nupkgs/Servirtium.Vcr.$VER.nupkg $REL/Servirtium.Vcr.$VER.nupkg
    curl -fsSL -o nupkgs/Servirtium.Vcr.$VER.nupkg.sha256 $REL/Servirtium.Vcr.$VER.nupkg.sha256
    ( cd nupkgs && { sha256sum -c Servirtium.Vcr.$VER.nupkg.sha256 2>/dev/null || shasum -a 256 -c Servirtium.Vcr.$VER.nupkg.sha256; } )
    [ -f nuget.config ] || dotnet new nugetconfig
    dotnet nuget add source "$PWD/nupkgs" --name servirtium-local --configfile nuget.config
  2. Add the dependency. A prerelease needs the explicit version.
    dotnet add package Servirtium.Vcr --version 2.0.0-alpha.20
    # or in the .csproj:
    # <PackageReference Include="Servirtium.Vcr" Version="2.0.0-alpha.20" />
  3. Check it: replay a recorded tape (the engine is deployed from the package).
    mkdir -p tapes
    curl -fsSL -o tapes/single_get.md https://raw.githubusercontent.com/servirtium/servirtium-vcr/v2.0.0-alpha.20/dotnet/example/tapes/single_get.md
    cat > Program.cs <<'CS'
    using Servirtium.Vcr;
    using var vcr = Vcr.Playback("tapes/single_get.md").Port(0).Start();
    using var http = new HttpClient();
    Console.WriteLine((await http.GetStringAsync(vcr.BaseUrl + "/ok")).Trim() + " " + vcr.LastKind);   // -> ok-body Ok
    CS
    dotnet run
  4. .NET Framework 4.8: the same package serves it (lib/net48/) and its build targets copy the engine (see notes). Target a 64-bit process.
    <PropertyGroup>
      <TargetFramework>net48</TargetFramework>
      <PlatformTarget>x64</PlatformTarget>          <!-- or ARM64; AnyCPU libraries/test projects get both engines -->
      <Prefer32Bit>false</Prefer32Bit>              <!-- for an exe -->
    </PropertyGroup>

Record

Point your client at the VCR's base URL; the VCR forwards to the real service and hands back the real response. Nothing is written until you commit.

using Servirtium.Vcr;

using var vcr = Vcr.Record("tapes/climate_api.md", "https://climatedataapi.worldbank.org")
    .RemoveTransportHeaders()
    .RedactCredentials()
    .Port(0)
    .Start();
using var client = new HttpClient { BaseAddress = new Uri(vcr.BaseUrl) };
try
{
    await client.GetStringAsync("/api/v1/countries");
    vcr.Commit();      // writes the tape; disposing with neither Commit() nor Discard() throws
}
catch
{
    vcr.Discard();     // a failing test drops the recording
    throw;
}

Playback

The same tape, replayed with no network.

using Servirtium.Vcr;

using var vcr = Vcr.Playback("tapes/climate_api.md").Port(0).Start();
using var client = new HttpClient { BaseAddress = new Uri(vcr.BaseUrl) };

var body = await client.GetStringAsync("/api/v1/countries");
Assert.Equal(VcrOutcome.Ok, vcr.LastKind);   // optional: assert a clean match
vcr.AssertNoMismatches();                     // nothing was refused
vcr.AssertAllConsumed();                      // every recorded interaction was replayed

Idiomatic extras

Volatile values

Typed scopes (VolatileScope); the string overload is gone. Captured values become {{tokens}} in the tape; playback binds what no request carries.

Vcr.Record(tape, upstream)
    .PrettyXml(2)
    .VolatileRule(VolatileScope.RequestBody, "<a:MessageID>urn:uuid:{{messageId}}</a:MessageID>")
    .VolatileRule(VolatileScope.ResponseHeaders, "Date: {{serverDate}}")
    .Start();

using var vcr = Vcr.Playback(tape).PrettyXml(2)
    .Bind("serverDate", DateTime.UtcNow.ToString("R"))
    .Start();
Console.WriteLine(vcr.VolatileReport());   // rules, binds, bindings, warnings

Rewrite the upstream's own URLs and scrub secrets

RewriteUpstreamBase keeps the client on the VCR when the service sends absolute links back; Redact scrubs a field at commit time while the live test still sees real bytes.

Vcr.Record(tape, "http://tfs.example:8080/tfs")
    .RemoveTransportHeaders()
    .RedactCredentials()
    .RewriteUpstreamBase()
    .Redact(VcrField.ResponseBody, "Bearer abc123", "Bearer REDACTED")
    .Start();

Unredact on playback

Rewrites what the tape expects to receive (request side only) so a scrubbed tape still matches the live request; Substitute is the response-side counterpart and takes VcrResponseField (ResponseBody or ResponseHeaders).

Vcr.Playback(tape)
    .StrictHeaders()
    .Unredact(VcrField.RequestHeaders, "Bearer REDACTED", realToken)
    .Start();

Gotchas

  • There is no 32-bit engine. PlatformTarget x86, RuntimeIdentifier win-x86, or an AnyCPU exe with Prefer32Bit (the classic console template's default) fails the build with SVCR001; set PlatformTarget x64 (or ARM64), Prefer32Bit false for an exe, and a 64-bit test host.
  • .NET Framework 4.8: an SDK-style PackageReference with a RuntimeIdentifier gets the engine from NuGet; otherwise the package's build/net48/Servirtium.Vcr.targets copies it (x64 or ARM64 PlatformTarget: that engine beside the assembly; AnyCPU: both under runtimes\win-x64\native\ and runtimes\win-arm64\native\, and the binding loads the one for the process). A project reaching the package through a ProjectReference gets it via buildTransitive/net48/; packages.config projects get the import added on install.
  • ServirtiumVcrCopyNative=false turns the engine copy and the SVCR001 check off (for example when SERVIRTIUM_VCR_LIB points at the engine).
  • On .NET Framework, CreateClientHandler is not available (it is built on SocketsHttpHandler): point the HttpClient at BaseUrl directly.
  • Disposing a record server with neither Commit() nor Discard() writes nothing and throws VcrException.
  • NU1101 means the local feed is not configured for the project; NU1102 means a wrong file name or version (Servirtium.Vcr.<ver>.nupkg, no leading v). The NuGet cache is keyed by id and version: dotnet nuget locals all --clear after replacing a nupkg.
  • FreeBSD has no RID in the package: fetch the release libservirtium_vcr .so and set SERVIRTIUM_VCR_LIB to its absolute path.
  • Do not ProjectReference the repo's dotnet/ projects: they build only through aeb. A tape recorded with RedactCredentials holds tokens, not real values: apply the same preset on the playback builder when the client sends a real credential.

Prompt for your coding agent

Paste this into Claude, Codex, Gemini or any coding agent, in your .NET project. It wraps one of your existing integration tests with Servirtium. The general prompt has a slot for the language instead.

Hey Gemini or Claude or Codex,

I want a test in this .NET project to run without calling a real web service every time. Use Servirtium (https://servirtium.dev), which records HTTP conversations as readable Markdown "tapes" and replays them. Do these steps in order and tell me what you did at each one.

1. Find an existing integration test in this project that calls a web service as a side effect of what it is testing. Prefer one that is slow, flaky, or needs credentials or network because of that call. Tell me which test you chose and why.

2. Add the Servirtium binding for .NET. Follow https://servirtium.dev/languages/dotnet/ exactly. Servirtium ships a built package for this language on the GitHub release v2.0.0-alpha.20: Servirtium.Vcr.2.0.0-alpha.20.nupkg (net8.0 + net48, eight RIDs); use the release directory as a local NuGet feed.
   Toolchain and minimum version: .NET 8 SDK or newer for net8.0 (verified here with the .NET 10 SDK, nupkg consumed on net10.0); .NET Framework 4.8 via the net48 target (64-bit only; SDK-style or packages.config projects).
   How the binding finds the native library: Deployed from the package's runtimes/<rid>/native/ (NuGet, or build/net48/Servirtium.Vcr.targets on .NET Framework); no SERVIRTIUM_VCR_LIB. SERVIRTIUM_VCR_LIB wins over the packaged copy and is needed only for FreeBSD (no RID in the package).

3. Route that test's HTTP client to the Servirtium VCR's base URL instead of the real service, using the idiomatic .NET way of setting a base URL or HTTP client. The VCR starts on its own port (use port 0, so it picks a free one) and gives you its base URL; give that URL to the client and never hard-code a port.

4. Run the test once in record mode against the real service. Commit the recording when the test passes and discard it when the test fails: a record VCR that is closed without a commit or a discard writes no tape and raises an error. Use redact_credentials (the spelling for .NET is on the page) so secrets become numbered tokens, and remove volatile request headers such as User-Agent and Date from the tape so playback does not depend on them.

5. Keep the tape beside the test as readable Markdown, in version control. Open it and check there are no secrets or personal data in it before committing.

6. Make the test play back by default, with an explicit switch (for example an environment variable such as SERVIRTIUM_RECORD=1) that re-records.

7. Prove it works offline: make the real service unreachable (network off, or a wrong host) and show the test still passes in playback.

Traps to avoid:
- A record VCR needs Commit() or Discard(); closing it without either writes nothing and raises.
- The VCR runs in your test process on its own port; the system under test must use the VCR's base URL, not the real one.
- There is no 32-bit engine. PlatformTarget x86, RuntimeIdentifier win-x86, or an AnyCPU exe with Prefer32Bit (the classic console template's default) fails the build with SVCR001; set PlatformTarget x64 (or ARM64), Prefer32Bit false for an exe, and a 64-bit test host.
- .NET Framework 4.8: an SDK-style PackageReference with a RuntimeIdentifier gets the engine from NuGet; otherwise the package's build/net48/Servirtium.Vcr.targets copies it (x64 or ARM64 PlatformTarget: that engine beside the assembly; AnyCPU: both under runtimes\win-x64\native\ and runtimes\win-arm64\native\, and the binding loads the one for the process). A project reaching the package through a ProjectReference gets it via buildTransitive/net48/; packages.config projects get the import added on install.
- ServirtiumVcrCopyNative=false turns the engine copy and the SVCR001 check off (for example when SERVIRTIUM_VCR_LIB points at the engine).
- On .NET Framework, CreateClientHandler is not available (it is built on SocketsHttpHandler): point the HttpClient at BaseUrl directly.
- Disposing a record server with neither Commit() nor Discard() writes nothing and throws VcrException.
- NU1101 means the local feed is not configured for the project; NU1102 means a wrong file name or version (Servirtium.Vcr.<ver>.nupkg, no leading v). The NuGet cache is keyed by id and version: dotnet nuget locals all --clear after replacing a nupkg.
- FreeBSD has no RID in the package: fetch the release libservirtium_vcr .so and set SERVIRTIUM_VCR_LIB to its absolute path.
- Do not ProjectReference the repo's dotnet/ projects: they build only through aeb. A tape recorded with RedactCredentials holds tokens, not real values: apply the same preset on the playback builder when the client sends a real credential.

References:
- This language's page: https://servirtium.dev/languages/dotnet/
- The binding's directory: https://github.com/servirtium/servirtium-vcr/tree/main/dotnet
- Concepts, features and every language's quick start: https://servirtium.dev/llms-full.txt (index: https://servirtium.dev/llms.txt)

How these snippets were checked

On linux x64 (cachyos, .NET SDK 10.0.114): restored the release Servirtium.Vcr.2.0.0-alpha.20.nupkg (sha256 checked) from a local feed into a fresh console project, ran the README Program.cs check (ok-body Ok), then a program running the playback snippet (AssertNoMismatches, AssertAllConsumed), the record snippet (RemoveTransportHeaders, RedactCredentials, Commit) against a local HttpListener upstream, and Record with neither Commit nor Discard (throws VcrException 'record VCR closed without commit or discard'). Ran dotnet/net48/check.sh against the release nupkg: 0 failures (AnyCPU both engines, RID win-x64, x64, x86 refused with SVCR001, transitive, packages.config incl. Prefer32Bit refused, binding tests built on net48); net48 assemblies cannot run off Windows, so nothing was executed on .NET Framework here. Volatile/Redact/Unredact snippets mirror docs/usage.md and the xunit tests; not run.

They mirror these files in the repository:

See also the feature reference, llms.txt and the dotnet/ directory.