Microsoft.Testing.Extensions.Hosting
1.0.0-alpha.26507.8
Prefix Reserved
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
<PackageReference Include="Microsoft.Testing.Extensions.Hosting" Version="1.0.0-alpha.26507.8" />
<PackageVersion Include="Microsoft.Testing.Extensions.Hosting" Version="1.0.0-alpha.26507.8" />
<PackageReference Include="Microsoft.Testing.Extensions.Hosting" />
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"
#:package Microsoft.Testing.Extensions.Hosting@1.0.0-alpha.26507.8
#addin nuget:?package=Microsoft.Testing.Extensions.Hosting&version=1.0.0-alpha.26507.8&prerelease
#tool nuget:?package=Microsoft.Testing.Extensions.Hosting&version=1.0.0-alpha.26507.8&prerelease
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:
- Creates an MTP application builder.
- Imports a read-only snapshot of the host's
IConfiguration. - Forwards MTP diagnostic logs to the host-owned
ILoggerFactory. - Applies each caller-owned
ITestingPlatformBuilderConfiguratorregistered in the host service provider. - Invokes the caller's MTP registration callback, which therefore has final configuration precedence.
- Starts the host before building MTP, so host-owned observability providers can subscribe first.
- Links the caller token and
IHostApplicationLifetime.ApplicationStoppingto MTP's cooperative cancellation path. - Runs MTP and returns its exit code.
- Stops the host in a
finallyblock.
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
StartAsyncfollows the host contract and can throwOperationCanceledException; 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,ApplicationStoppingis 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 callStopApplication; 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
Datadictionary. If that dictionary cannot be updated, anAggregateExceptionexposes 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 | Versions 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. |
-
.NETStandard 2.0
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
- Microsoft.Testing.Extensions.Configuration (>= 1.0.0-alpha.26507.8)
- Microsoft.Testing.Extensions.Logging (>= 1.0.0-alpha.26507.8)
- Microsoft.Testing.Platform (>= 2.5.1 && < 3.0.0)
-
net8.0
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
- Microsoft.Testing.Extensions.Configuration (>= 1.0.0-alpha.26507.8)
- Microsoft.Testing.Extensions.Logging (>= 1.0.0-alpha.26507.8)
- Microsoft.Testing.Platform (>= 2.5.1 && < 3.0.0)
-
net9.0
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
- Microsoft.Testing.Extensions.Configuration (>= 1.0.0-alpha.26507.8)
- Microsoft.Testing.Extensions.Logging (>= 1.0.0-alpha.26507.8)
- Microsoft.Testing.Platform (>= 2.5.1 && < 3.0.0)
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 |
See the release notes at https://github.com/microsoft/testfx/blob/main/docs/Changelog-Platform.md#2.5.1.