Lesson 23 of 23 · 8 min read · Updated
Modular Monolith in ASP.NET Core with IHostingStartup: Choose Your Modules at Startup
On this page
A modular monolith keeps module boundaries in the code, but lets you decide at deployment time how many processes run. ASP.NET Core already has what you need: IHostingStartup loads an assembly by name before your app starts, and IStartupFilter lets that assembly add its own middleware and endpoints. No extra NuGet package is required.
In this lesson you build a small parcel-shipping app out of four modules. The host never references a module type. One environment variable chooses the modules, so the same build runs as one process or as two. The idea comes from the Habr series by gandjustas on modular monoliths in ASP.NET (part 2). The scenario and code here are our own, and every command was run on .NET 10.
Technical Requirements
- .NET 10 SDK
curlor therequests.httpfile in the sample- Sample code: github.com/Fcakiroglu16/dotnetacademy, branch aspnet-modular-monolith
Core Concepts
- Module: a class library that uses the
Microsoft.NET.Sdk.WebSDK with<OutputType>Library</OutputType>. It owns its endpoints and its service registrations. - Contract: a small assembly with interfaces and records. Modules know each other only through contracts, never through implementations.
- **
IHostingStartup**: the host reads theHostingStartupAssembliessetting (a semicolon-separated list, usually set with theHOSTINGSTARTUPASSEMBLIESenvironment variable), loads each assembly, finds[assembly: HostingStartup(typeof(T))]and callsT.Configure(IWebHostBuilder). - **
IStartupFilter**: wraps the request pipeline. A module uses it to add endpoints after the host pipeline is built. - Topology: which modules run in which process. It becomes configuration, not a build decision.
The Plan
| Project | Role |
|---|---|
Logistics.Contracts | IShippingQuoteService, QuoteRequest, QuoteResult |
Logistics.Quotes.Core | the price rule |
Logistics.ModuleKit | ModuleBase |
Logistics.Host | empty web host |
Parcels module | GET /parcels/{id}/quote, depends on the contract only |
Quotes.Local module | calculates the quote in-process |
Quotes.Remote module | calls another process over HTTP |
Quotes.Api module | POST /quotes, the other side of the remote call |
Parcels needs a quote, but does not care where it is calculated. Quotes.Local and Quotes.Remote both implement the same interface, so choosing one of them is the whole deployment decision.
Step 1: The Contract
src/Logistics.Contracts/IShippingQuoteService.cs
Step 2: One Base Class for Every Module
ModuleBase is both the hosting startup and the startup filter. A module overrides only the hooks it needs.
src/Logistics.ModuleKit/ModuleBase.cs
Two details matter. The module registers itself as an IStartupFilter, so its pipeline code runs after the host's. And ModuleBase only uses ConfigureServices and ConfigureAppConfiguration on the web host builder. Calling IWebHostBuilder.Configure(app => ...) from a hosting startup would replace the host pipeline instead of extending it.
Step 3: A Host That Knows Nothing
src/Logistics.Host/Program.cs
UseRouting() is explicit because module endpoints are mapped after it. The / endpoint only prints the setting, so you can see which modules this process loaded.
The project file references every module, but only so the build copies the DLLs next to the host. No host code uses a module type.
src/Logistics.Host/Logistics.Host.csproj
Step 4: The Modules
The Parcels module registers one endpoint and asks for the contract:
src/Modules/Parcels/ParcelsModule.cs
The in-process implementation registers itself in ConfigureServices:
src/Modules/Quotes.Local/LocalQuotesModule.cs
The remote implementation reads the backend address from configuration and sends the same request over HTTP:
src/Modules/Quotes.Remote/RemoteQuotesModule.cs
Finally, the Quotes.Api module is the receiving end. It exposes the same price rule as POST /quotes:
src/Modules/Quotes.Api/QuotesApiModule.cs
Step 5: Run Two Topologies
Topology A: one process. Load the parcels module and the local quote module:
Topology B: two processes, same build. The gateway swaps Quotes.Local for Quotes.Remote. A second instance of the same host runs only Quotes.Api:
scripts/run-split.sh
The responses from both topologies:
The price is identical. Only calculatedBy changes, because the calculation moved to the backend process. ParcelsModule was not touched.
What Goes Wrong
We ran these cases on purpose:
| Setup | Result |
|---|---|
Correct names in HOSTINGSTARTUPASSEMBLIES | modules load, endpoints answer |
| No variable at all | host starts, / returns "modules": null, /parcels/1/quote returns 404 |
Typo (Logistics.Module.Parcelz) | one crit log: Startup assembly ... failed to execute, host still starts and answers / |
Parcels loaded, no quote module | every request returns 500, even /, with Body was inferred but the method does not allow inferred body parameters |
Quotes.Api not loaded, POST /quotes | 404 |
The last two rows are the price of this design. The compiler cannot see that Parcels needs a quote module, and a typo is only a log line. A startup check that every required contract has an implementation, or a health probe after deploy, closes that gap. Treat the module list as production configuration and review it like code.
Practical Rules
- Put every type that crosses a module boundary in a contracts assembly. If the host or another module uses a concrete module type, you lose the point of the design.
- Keep module startup code to
ConfigureServicesandConfigureAppConfiguration. Do not callConfigure(app => ...)on the web host builder. - Build once and select modules with environment variables. Compose or Kubernetes then change topology without a rebuild.
- If you use MVC or Razor Pages, the Razor SDK discovers controllers from referenced assemblies at build time, so a disabled module can still register them. The source series covers the fix (
GenerateMvcApplicationPartsAssemblyAttributesandAddApplicationPartin each module). This sample uses Minimal APIs, so it does not apply here.
Summary
IHostingStartupplusIStartupFiltergive you modules with no framework: each module owns its services and endpoints, and the host stays empty.- A contract assembly lets you swap an in-process implementation for an HTTP one without touching the consumer.
- The same build ran as one process and as two, and the quote stayed
7.94 EURfor parcel 1. - Missing modules fail quietly or late, so add a startup or deploy-time check for required contracts.