Microsoft.Testing.Extensions.Hosting 1.0.0-alpha.26507.8

Prefix Reserved
This is a prerelease version of Microsoft.Testing.Extensions.Hosting.
dotnet add package Microsoft.Testing.Extensions.Hosting --version 1.0.0-alpha.26507.8
                    
NuGet\Install-Package Microsoft.Testing.Extensions.Hosting -Version 1.0.0-alpha.26507.8
                    
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="Microsoft.Testing.Extensions.Hosting" Version="1.0.0-alpha.26507.8" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Microsoft.Testing.Extensions.Hosting" Version="1.0.0-alpha.26507.8" />
                    
Directory.Packages.props
<PackageReference Include="Microsoft.Testing.Extensions.Hosting" />
                    
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 Microsoft.Testing.Extensions.Hosting --version 1.0.0-alpha.26507.8
                    
#r "nuget: Microsoft.Testing.Extensions.Hosting, 1.0.0-alpha.26507.8"
                    
#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 Microsoft.Testing.Extensions.Hosting@1.0.0-alpha.26507.8
                    
#: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=Microsoft.Testing.Extensions.Hosting&version=1.0.0-alpha.26507.8&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Microsoft.Testing.Extensions.Hosting&version=1.0.0-alpha.26507.8&prerelease
                    
Install as a Cake Tool

Microsoft.Testing.Extensions.Hosting

Microsoft.Testing.Extensions.Hosting runs Microsoft.Testing.Platform inside an application-owned Microsoft.Extensions.Hosting host.

The host remains the composition root and owns its service provider, configuration, logging, OpenTelemetry providers, lifetime, and disposal. The extension starts and stops the host around the test run but never disposes it.

Install the package

dotnet add package Microsoft.Testing.Extensions.Hosting
dotnet add package Microsoft.Extensions.Hosting
dotnet add package MSTest

Generated entry point

MSTest.Sdk and other MTP projects can generate the hosting glue instead of maintaining a custom Program.cs:

<PropertyGroup>
  <TestingPlatformHostFactory>Contoso.Tests.TestHost.CreateHost</TestingPlatformHostFactory>
  <TestingPlatformOpenTelemetryMode>HostOwned</TestingPlatformOpenTelemetryMode>
</PropertyGroup>

The configured method has this V1 contract:

public static Task<IHost> CreateHost();

It returns one fresh, unstarted host. It does not receive MTP command-line arguments. Generated code owns and disposes the host (preferring IAsyncDisposable when implemented), invokes the existing RunTestingPlatformAsync bridge, and preserves the generated AddSelfRegisteredExtensions(builder, args) registration exactly once. Help and info options supplied on the process command line or through response files do not invoke the factory.

TestingPlatformOpenTelemetryMode=HostOwned requires Microsoft.Testing.Extensions.OpenTelemetry. It activates only the MTP diagnostics producer; the host remains the sole owner of providers, exporters, resource identity, and disposal.

The factory must configure a host that never writes startup, shutdown, logging, or exporter output to standard output. MTP reserves stdout for machine-readable modes such as --list-tests json, server JSON-RPC, and the dotnet test protocol. Remove default console logging providers and use OTLP, files, or another non-stdout sink.

Help or info activated only through configuration is resolved while the MTP application is built, after the factory-created host supplies its configuration snapshot. In that uncommon case the factory is invoked, but the host contract still requires construction to be side-effect-free until StartAsync.

Manual usage

using System.Reflection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Testing.Extensions;

HostApplicationBuilder builder = Host.CreateApplicationBuilder();
builder.Logging.ClearProviders();

using IHost host = builder.Build();
return await host.RunTestingPlatformAsync(args, testApplication =>
{
    testApplication.AddMSTest(() => [Assembly.GetExecutingAssembly()]);
});

The process command line belongs to MTP in this composition. Configure the host through code, environment variables, or application configuration rather than passing the same argument array to both parsers.

RunTestingPlatformAsync:

  1. Creates an MTP application builder.
  2. Imports a read-only snapshot of the host's IConfiguration.
  3. Forwards MTP diagnostic logs to the host-owned ILoggerFactory.
  4. Applies each caller-owned ITestingPlatformBuilderConfigurator registered in the host service provider.
  5. Invokes the caller's MTP registration callback, which therefore has final configuration precedence.
  6. Starts the host before building MTP, so host-owned observability providers can subscribe first.
  7. Links the caller token and IHostApplicationLifetime.ApplicationStopping to MTP's cooperative cancellation path.
  8. Runs MTP and returns its exit code.
  9. Stops the host in a finally block.

Ownership and limitations

  • The caller owns and disposes the IHost.
  • Builder configurators are borrowed from host.Services; the Hosting extension never disposes them separately.
  • The extension does not create a second Microsoft.Extensions dependency-injection container.
  • Imported configuration has snapshot semantics and does not propagate reloads after the MTP application is built.
  • Composition is process-local. Live services do not cross into separately launched test host or controller processes.
  • Process-restart extensions (for example retry, crash dump, or hang dump scenarios) execute the generated entry point in each process. Until MTP exposes a role-aware pre-build hook, the factory can therefore be invoked in both the controller and child process; factories used with those extensions must avoid exclusive global resources such as fixed ports.
  • The cancellation token controls host startup and active MTP execution. Cancellation during StartAsync follows the host contract and can throw OperationCanceledException; once MTP is running, cancellation uses MTP's cooperative path and returns the test-session-aborted exit code (3) after cleanup.
  • When the host provides IHostApplicationLifetime, ApplicationStopping is linked to the same MTP cancellation source. Caller cancellation, host stopping, Ctrl+C, --timeout, controller cancellation, and test framework stop policies are peer signals; the first signal starts the same idempotent MTP shutdown path. MTP cancellation does not call StopApplication; the helper stops the host after MTP cleanup completes.
  • In controller mode, cancellation uses MTP's existing controller control channel to request cooperative cancellation in the active test host process. Existing bounded termination remains the fallback for a non-cooperative child.
  • Cancellation registrations are removed when execution completes. The lifetime bridge disconnects before the helper's own StopAsync, and shutdown uses an uncancelled token so graceful cleanup is still attempted without re-entering MTP cancellation.
  • Exceptions from host startup, MTP construction/execution, and host shutdown are surfaced to the caller. When an operation and its cleanup both fail, the operation remains the primary exception and the cleanup exception is attached to its Data dictionary. If that dictionary cannot be updated, an AggregateException exposes the operation exception first and the cleanup exception second.

See the runnable ASP.NET Core and Aspire ServiceDefaults samples under samples/public/MTPHostIntegration.

Documentation

For comprehensive documentation, see https://aka.ms/testingplatform.

Feedback & contributing

Microsoft.Testing.Platform is an open source project. Provide feedback or report issues in the microsoft/testfx GitHub repository.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  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 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Microsoft.Testing.Extensions.Hosting:

Package Downloads
MSTest.Extensions.Hosting

This package creates MSTest test classes from an application-owned Microsoft.Extensions.Hosting service provider with one dependency-injection scope per test invocation. MSTest is Microsoft supported Test Framework.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0-alpha.26507.8 38 10/7/2026
1.0.0-alpha.26506.1 61 10/6/2026