Table of Contents

7.0 Hosting

An RDCore.Web application is an ASP.NET Core application: it maps the endpoints of its controller modules into its routing, and a pool of machines serves them (ASP.NET Core fundamentals).


7.1 Mapping Endpoints

Syntax

// program: the application's composed program, such as the composition `rdc compose` wrote (ComposedProgramFile.LoadAsync, RDCore.Runtime.Hosting).
var endpoints = RDWebEndpointReader.Read(program);
await using var pool = await RDWebMachinePool.StartAsync(program, new RDWebMachinePoolOptions(), cancellationToken);
app.MapRDWebEndpoints(endpoints, pool);

MapRDWebEndpoints returns a builder of the conventions of every endpoint it mapped, such as an authorization policy (Minimal APIs).

Runtime Semantics

Each endpoint is mapped to its route (§3.0) and its HTTP method. Its display name is the method, the route and the procedure, such as GET api/orders/{id} (OrdersController.Find).

A request to an endpoint:

  1. binds the procedure's arguments from its route values, query string and body (§4.0);
  2. runs the procedure on a machine of the pool (§6.1);
  3. answers with how the procedure ended (§5.0).

A request to an endpoint's route with another HTTP method answers 405 Method Not Allowed, and runs nothing.


7.2 The RDCore.Web Host

Runtime Semantics

RDCore.Web.Host runs one of three ways.

Started It is
By the RDCore platform, with the pipe the platform names The RDWeb extension: it reports the RDW diagnostics of the documents the language server sends it, each linked to its page (Diagnostics).
By itself, as the external host of its machines The process that makes their forms, the objects of automation servers and their native calls. A deployed host needs no rdc beside it.
Any other way An ASP.NET Core application that serves the files of its web root, index.html at the root, and the program its configuration names. It reads its command-line arguments and appsettings.json as ASP.NET Core does.

The application's configuration:

Key Meaning
RDWeb:Composition The program the host serves: the composition file that rdc compose wrote. A relative path is relative to the current directory. Without it, the host serves its static files only.
RDWeb:Pool The options of the machine pool (§6.1), such as RDWeb:Pool:MaxMachines.
RDWeb:KeptMachines The options of the kept machines (§6.2), such as RDWeb:KeptMachines:IdleTimeout.
RDWeb:OpenApi Whether the host serves the OpenAPI document of the endpoints and its Swagger UI page (§9.0): by default in the Development environment only.
webroot The folder of the static files: wwwroot by default.

Given a composition, the host loads the program, runs Program.Run and plans each endpoint's run (§7.3), and maps the endpoints of its controller modules (§7.1) before it serves any request. Its pool makes its MinMachines machines after that: a program that cannot be served makes none. A controller module that declares an endpoint that cannot be served stops the host before it starts (§2.0), and so does a startup error (§7.3).

Each request runs its action in RD-VBA, on a machine of the program: the action's value is the response's body (§5.1), and a run-time error is a 500 with problem details that give its number (§5.2).

RDCore.Web.Host --RDWeb:Composition bin/Debug/NavalBattle.rdcomposition --webroot web/dist --urls http://localhost:5000

7.3 The Request Dispatch

Static Semantics

RDWeb is a library of the RDCore.Web extension. A project references it by name, as it references any library of the platform, and rdc compose writes its description into the composition's Symbols folder. The host gives the library to every machine of the program it serves; a host that does not give it refuses the composition, naming the library. The objects the library makes (Result, RequestContext, ClaimsUser, the builders) are objects of the session, which RD-VBA code holds and compares as it does its own.

The host serves a program with the RDWeb library's dispatch when the program has Program.Run, a '@Service class (§2.7), a controller class with a default instance or that implements IRequestHook or IErrorHook, an action with a RequestContext parameter, or an action declared As RDWeb.Result (§5.4). It serves any other program as each endpoint's procedure alone (§7.1).

Program.Run is the program's entry point: a public Sub Run of the standard module Program. The host runs it once at startup, on a machine of its own that it disposes of once Run returns. Run is given a WebApplicationBuilder that the RDWeb library makes from the host's configuration (each key that has a value, as a string), and records the application with it. A Run that declares no parameter is given none, and records nothing.

With the builder Program.Run records
Services.AddScoped, Services.AddTransient, Services.AddSingleton A service: its name, the type a parameter declares, and the Module.Function that makes one. It wins over a '@Service class of its name.
UseFilter A filter: a Module.Function that runs before each controller, in the order the filters are added.
Authentication, Authorization Authentication schemes, and the policies that '@Authorize names (§2.9).
Configuration Nothing: it reads the host's configuration.

A service's lifetime says when another object of it is made:

Lifetime Objects
Scoped One per request: each parameter that needs it in the request's run is given the same object.
Transient One for each parameter that needs it.
Singleton One per machine, kept between requests. A pool machine's reset after each request lets go of it (§6.1), so a singleton outlives a request only on a kept machine (§6.2). A singleton is given only singletons.

Each parameter of a factory, a filter or a service's function is given the service its declared type names, made first. A parameter declared as RequestContext is given the request's context.

Runtime Semantics

A request is one run on one machine, in this order:

  1. its RequestContext, made from its method, path, headers and user;
  2. the services the run needs, each before the one it is given to;
  3. each filter, in order: one that returns Nothing lets the request go on, and one that returns a result answers the request, which skips to step 7;
  4. the factory, for a controller class with a default instance (§2.8);
  5. IRequestHook.OnRequest, for a controller class that implements it: Nothing lets the request go on, and a result answers it, which skips to step 7;
  6. the action. When it leaves a run-time error unhandled, IErrorHook.OnError of a controller class that implements it answers: a result is the response, and Nothing answers with the error's problem details (§5.2);
  7. RDWeb.Requests.ResponseOf, given the run's value: a Result answers with its status, location and body, and any other value is the body. A Sub that returns no result answers 204 No Content.

An error that a step leaves unhandled, other than the action's error that OnError answers, or an End, ends the run, and nothing after it runs: the request answers with the run-time error (§5.2).

A startup error stops the host before it serves anything, and its pool makes no machine. Its message names what is wrong:

Startup error Fix
Program.Run does not run to its end: it leaves an error unhandled, or runs End. Handle the error.
A parameter's declared type names no service. Register the service in Program.Run, or declare it with '@Service.
A parameter of a factory, a filter or a service is not of an object type (RDW00006). Declare it as the service's class or interface.
Services need each other, so none of them can be made first. Break the cycle.
A Singleton is given a Scoped or a Transient service. Make that service a Singleton, or the singleton shorter-lived.
A factory is not a public Function of its class, or a registration or a filter names no public Function of a standard module. Name a public Function.
The program asks for authentication or authorization: '@Authorize, or a scheme or a policy that Program.Run adds. Remove it. RDCore.Web signs no user in.

Implementation

Type or member Role
MapRDWebEndpoints Maps each endpoint to its route and HTTP method, served by the pool.
RDWebMachinePool.StartAsync Starts the pool the endpoints are served by.
RDWebEndpointReader Reads the endpoints a module's annotations declare, or those of every module of a program.
RDWebHostApplication The host's application, and the program it serves.
RDWebStartupException A startup error: what Program.Run recorded, or the services there are, cannot serve the program.
RDWebLibraryFactory Makes the provider that runs the calls of the RDWeb library in a runtime session; the host gives it to every machine, and the environment host loads it from the extension's folder.

Tests

Rule Test
An endpoint serves only its own HTTP method RDWebEndpointsHostingTests.AnEndpoint_ServesOnlyItsOwnMethod
Through a pool of one machine, every request is served RDWebEndpointsHostingTests.ThroughAPoolOfOneMachine_EveryRequestIsServed
The host serves its page at the root RDWebHostApplicationTests.TheRoot_ServesThePage_WhichLoadsItsScript
The host serves the page's script RDWebHostApplicationTests.ThePagesScript_IsServed
Without a composition, the host loads no program and serves no endpoint RDWebHostApplicationTests.WithoutAComposition_TheHostLoadsNoProgram_AndServesNoEndpoint
A composed RD-VBA project's actions run in RD-VBA: a GET with its route and query arguments, a POST with its JSON body as an object of its class, each value as the response RDWebHostedControllerTests.AGetAction_RunsInRDVBA_WithItsRouteAndQueryArguments_AndItsValueIsTheResponse, RDWebHostedControllerTests.APostAction_RunsInRDVBA_WithItsBodyAsAnObjectOfItsClass_AndTheObjectItReturnsIsTheResponse
A run-time error of a composed project's action answers 500, with its number RDWebHostedControllerTests.ARunTimeErrorOfAnAction_IsA500_WithTheErrorsNumberInItsProblemDetails
A composition is loaded from its full path, with the host as its machines' external host, and its endpoints are served RDWebHostApplicationTests.AComposition_IsLoadedFromItsFullPath_WithTheHostAsItsMachinesExternalHost_AndItsControllersEndpointsAreServed
The pool's options are read from RDWeb:Pool RDWebHostApplicationTests.ThePoolsOptions_AreReadFromTheirSection
A kept machine's endpoint is served by the kept machines, with the options of RDWeb:KeptMachines RDWebHostApplicationTests.AKeptMachinesEndpoint_IsServedByTheKeptMachines_WithTheOptionsOfTheirSection
A program's endpoints are those of every module of its documents RDWebEndpointReaderTests.AProgramsEndpoints_AreThoseOfEveryModuleOfItsDocuments_BlocksIncluded_EachNamedAsItsDocumentNamesIt
The extension reports the RDW findings, each linked to its page RDWebExtensionTests.TheExtensionsHandler_ReportsRDWebsFindings_EachLinkedToItsPage
Program.Run runs once at startup, on a machine of its own, given the builder the library makes from the host's configuration RDWebHostDispatchTests.ProgramRun_RunsOnceAtStartup_OnAMachineOfItsOwn_GivenTheBuilderTheLibraryMakesFromTheHostsConfiguration
A request is one run: its context, the services, the factory, the hooks, the action and ResponseOf RDWebHostDispatchTests.ARequest_IsOneRun_OfItsContext_TheServicesItsFactoryTakes_TheFactory_TheHooks_TheAction_AndResponseOf
The request's context is made from its method, path and headers; an action's RequestContext parameter is given it RDWebHostDispatchTests.TheRequestsContext_IsMadeFromItsMethodPathAndHeaders_AndAnActionsRequestContextParameterIsGivenIt_NotTheQueryString
Each filter is a guard, in the order Program.Run added it RDWebHostDispatchTests.EachFilter_IsAGuard_InTheOrderProgramRunAddedIt_GivenTheServicesItTakes
A scoped service is made once per request, a transient one once for each parameter RDWebHostDispatchTests.AServiceTwoParametersTake_IsMadeOnce_WhenScoped_AndOnceForEach_WhenTransient
A service class is made by its Create on its default instance, or by New; a registration of its name wins RDWebHostDispatchTests.AServiceClass_IsMadeByItsCreate_OnItsDefaultInstance_OrByNew_AndARegistrationOfItsNameWins
What ResponseOf returns is the response RDWebHostDispatchTests.WhatResponseOfReturns_IsTheResponse
A Sub that returns no result answers 204 RDWebHostDispatchTests.ASubsValueThatIsNoResult_IsAnswered204
A program with no entry point, service or factory is served as each procedure's run alone RDWebHostDispatchTests.AProgramWithNoEntryPointServiceOrFactory_IsServedAsEachProceduresRunAlone
A Program.Run that does not complete is a startup error, and the pool makes no machine RDWebHostDispatchTests.AProgramRunThatDoesNotComplete_IsAStartupError_AndThePoolMakesNoMachine
A program the dispatch cannot serve is a startup error that names why RDWebHostDispatchTests.AProgramTheDispatchCannotServe_IsAStartupError_NamingWhy
A request hook's result is the response, and its Nothing lets the action run; an error hook's result answers the action's run-time error RDWebHooksControllerTests.ARequestHookThatGivesAResult_IsTheResponse, RDWebHooksControllerTests.ARequestHookThatGivesNothing_LetsTheActionAnswer, RDWebHooksControllerTests.AnErrorHook_AnswersTheActionsRunTimeError, RDWebHostedControllerTests.ARequestHookThatGivesAResult_IsTheResponse_AndTheActionDoesNotRun, RDWebHostedControllerTests.AnErrorHook_AnswersTheActionsRunTimeError_WithItsNumberAndTheRequestsPath
The context has the request's method, path, headers by name in any case, and user, which is one object RDWebCallProviderTests.TheContext_HasTheRequestsMethodPath_AndHeadersByNameInAnyCase, RDWebCallProviderTests.TheUser_HasTheClaimsTheHostGave_AndIsOneObject, RDWebCallProviderTests.ARequestWithNoUser_IsNotAuthenticated_AndHasNoClaim
The builder records services, schemes, policies and filters in order, and reads the configuration by key in any case RDWebCallProviderTests.ServicesSchemesPoliciesAndFilters_AreRecorded_EachInTheOrderItWasAdded, RDWebCallProviderTests.TheConfiguration_ReadsAKeyInAnyCase_AndIsEmptyForAKeyItHasNot
A service, a policy, a filter or a scheme the builder cannot record raises error 5 RDWebCallProviderTests.AServiceWithNoNameOrNoFactory_IsError5, RDWebCallProviderTests.APolicyItCannotMeet_IsError5, RDWebCallProviderTests.AFilterOrASchemeWithNoName_IsError5
An object a result holds is a reference of the session, let go of with the result RDWebCallProviderTests.AnObjectAResultHolds_IsAReferenceOfTheSession_LetGoOfWithTheResult