Class ProtoHostBuilderExtensions
- Namespace
- ProtoTest.AspNetCore
- Assembly
- ProtoTest.AspNetCore.dll
Provides extension methods for IProtoHostBuilder to configure ASP.NET Core test hosts.
public static class ProtoHostBuilderExtensions
- Inheritance
-
ProtoHostBuilderExtensions
- Inherited Members
Methods
AddAspNetCoreServer<TProgram>(IProtoApplicationBuilder, Action<IWebHostBuilder>?, Action<WebApplicationFactoryClientOptions>?, AspNetCoreServerLifetime)
Backs an application with an in-process ASP.NET Core server, registered under the application name so the application's HTTP clients (a client with no configured URL) fall back to its transport automatically.
public static IProtoApplicationBuilder AddAspNetCoreServer<TProgram>(this IProtoApplicationBuilder application, Action<IWebHostBuilder>? configureWebHost = null, Action<WebApplicationFactoryClientOptions>? configureClientOptions = null, AspNetCoreServerLifetime lifetime = AspNetCoreServerLifetime.PerRun) where TProgram : class
Parameters
applicationIProtoApplicationBuilderconfigureWebHostAction<IWebHostBuilder>configureClientOptionsAction<WebApplicationFactoryClientOptions>lifetimeAspNetCoreServerLifetime
Returns
Type Parameters
TProgram
Remarks
Repeating the call registers each server and lets the client initializer hook pick the first that initializes, exactly like re-registering a REST client name; the transport, keyed application services and capability stay registered once. There is no whole-call guard, so nothing survives a failure.
AddAspNetCoreServer<TProgram>(IProtoHostBuilder, string, Action<IWebHostBuilder>?, Action<WebApplicationFactoryClientOptions>?, AspNetCoreServerLifetime)
Registers an ASP.NET Core application under test into the ProtoTest execution pipeline.
public static IProtoHostBuilder AddAspNetCoreServer<TProgram>(this IProtoHostBuilder builder, string name = "Default", Action<IWebHostBuilder>? configureWebHost = null, Action<WebApplicationFactoryClientOptions>? configureClientOptions = null, AspNetCoreServerLifetime lifetime = AspNetCoreServerLifetime.PerRun) where TProgram : class
Parameters
builderIProtoHostBuilderThe IProtoHostBuilder instance.
namestringThe unique identifier for this server instance. Defaults to "Default".
configureWebHostAction<IWebHostBuilder>Optional callback for replacing services or changing the in-process web host.
configureClientOptionsAction<WebApplicationFactoryClientOptions>Optional callback for configuring the generated HTTP client options.
lifetimeAspNetCoreServerLifetimeWhether one application instance serves the whole run (the default) or each test starts its own.
Returns
- IProtoHostBuilder
The modified IProtoHostBuilder.
Type Parameters
TProgramThe entry point class of the ASP.NET Core application under test.
AddLoopbackApplication(IProtoHostBuilder, string, Func<string[], WebApplication>)
Hosts a hand-built WebApplication on its own loopback listener inside the test
process and publishes the address the listener bound as the application's BaseUrl, so a
browser session, a REST client and the readiness probe all resolve that one running
application and the run releases the listener with the run.
public static IProtoHostBuilder AddLoopbackApplication(this IProtoHostBuilder builder, string applicationName, Func<string[], WebApplication> createApp)
Parameters
builderIProtoHostBuilderThe IProtoHostBuilder instance.
applicationNamestringThe application the published address belongs to.
createAppFunc<string[], WebApplication>Builds the application without running it, from the given command-line arguments. Pass them to
WebApplication.CreateBuilder(orHost.CreateApplicationBuilder): they carry the suite's configuration with the values the infrastructure started before this piece published, and the loopback--urlspair. Arguments the factory ignores never reach the application.
Returns
- IProtoHostBuilder
The modified IProtoHostBuilder.
Remarks
The piece is registered with the address key it fills
(ProtoTest:Applications:{applicationName}:BaseUrl), so a run that configures that key
skips the listener and points at that environment instead. The published instance is a real
application, not the test host: register it instead of AddAspNetCoreServer for that
application, because ServerFactory, ApplicationServices and
[RequiresInProcess] belong to the test host.
UseInProcess<TProgram>(IProtoApplicationBuilder, Action<IWebHostBuilder>?, Action<WebApplicationFactoryClientOptions>?, AspNetCoreServerLifetime)
Adds the in-process provider to the application's chain: when it wins, the application runs on
an in-process ASP.NET Core test server and declares the honest server and clock
capabilities, so [RequiresInProcess] and [RequiresTestClock] gate exactly the runs
it serves. Put it last in a chain whose earlier providers serve the environment.
public static IProtoApplicationBuilder UseInProcess<TProgram>(this IProtoApplicationBuilder application, Action<IWebHostBuilder>? configureWebHost = null, Action<WebApplicationFactoryClientOptions>? configureClientOptions = null, AspNetCoreServerLifetime lifetime = AspNetCoreServerLifetime.PerRun) where TProgram : class
Parameters
applicationIProtoApplicationBuilderconfigureWebHostAction<IWebHostBuilder>configureClientOptionsAction<WebApplicationFactoryClientOptions>lifetimeAspNetCoreServerLifetime
Returns
Type Parameters
TProgram
Remarks
The server, its transport and the keyed application services register while the host is built, for the winning provider only: an application served by a configured address, a loopback listener or an AppHost resource runs no test server and no second client.
UseLoopback(IProtoApplicationBuilder, Func<string[], WebApplication>)
Adds the loopback provider to the application's chain: when it wins, the run starts the
hand-built application on its own loopback listener and publishes the bound address as the
application's BaseUrl. The application is a real process boundary, so the winner
declares no server and no clock: [RequiresInProcess] and
[RequiresTestClock] skip, and the readiness probe waits for the published address.
public static IProtoApplicationBuilder UseLoopback(this IProtoApplicationBuilder application, Func<string[], WebApplication> createApp)
Parameters
applicationIProtoApplicationBuildercreateAppFunc<string[], WebApplication>