How would you move a 2019 ASP.NET Core veterinary-clinic API built on Startup.cs and WebHost.CreateDefaultBuilder onto WebApplicationBuilder in .NET 10?
answer
- ConfigureServices -> builder.Services
- Configure -> after Build, on app
- WebHost obsolete: ASPDEPR008, ASPDEPR004
- UseMvc throws under endpoint routing
- UseStartup on builder.WebHost: NotSupportedException
basics
~10 sReplace WebHost.CreateDefaultBuilder with WebApplication.CreateBuilder, move ConfigureServices into builder.Services and Configure below builder.Build(), swap IHostingEnvironment for app.Environment and UseMvc for MapControllers; builder.WebHost.UseStartup is not supported.
solid answer
~40 sIn .NET 10 `WebHost` and `IWebHost` are obsolete (ASPDEPR008) and so is `WebHostBuilder` (ASPDEPR004), so the 2019 entry point has to go. Create `WebApplication.CreateBuilder(args)`; the `Startup` constructor's `IConfiguration` becomes `builder.Configuration`; the body of `ConfigureServices` moves onto `builder.Services`, with `AddMvc()` usually becoming `AddControllers()` for a JSON API. After `var app = builder.Build();` comes the body of `Configure`: `IHostingEnvironment env` becomes `app.Environment`, and `app.UseMvc()` becomes `app.MapControllers()` — `UseMvc` throws while endpoint routing is on. `builder.WebHost.UseStartup<Startup>()` throws `NotSupportedException` (analyzer ASP0010), so a staged move either calls the `Startup` methods by hand or keeps `Host.CreateDefaultBuilder(...).ConfigureWebHostDefaults(...)`, which is still supported. Services resolved at startup now need an explicit scope.
code
csharp · 6 lines// 2019, ASP.NET Core 2.2 - obsolete in .NET 10 (ASPDEPR008)
public static void Main(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build()
.Run();go deeper
Recall the mapping: ConfigureServices becomes builder.Services, Configure becomes code after Build on app, and UseMvc becomes MapControllers.
Explain which 2019 APIs are obsolete in .NET 10 with their ASPDEPR ids, and why UseStartup on builder.WebHost throws.
Plan a staged migration: call Startup by hand or stay on the Generic Host first, add explicit scopes for startup work, and verify routes and configuration match.
Sequence migrations across many legacy services, weighing a quick Generic Host bridge against a full move, and budget for test and pipeline changes.
## Where the 2019 app starts from A clinic API written in 2019 on ASP.NET Core 2.2 typically has two files. `Program.cs` calls `WebHost.CreateDefaultBuilder(args).UseStartup<Startup>().Build().Run()`. `Startup.cs` takes `IConfiguration` in its constructor, registers services in `ConfigureServices(IServiceCollection services)` — often `services.AddMvc()` and a `DbContext` for the clinic's patients and appointments — and builds the pipeline in `Configure(IApplicationBuilder app, IHostingEnvironment env)`, ending in `app.UseMvc()`. In ASP.NET Core 10 almost every one of those names is history: | 2019 code | Status in .NET 10 | Replacement | |---|---|---| | `WebHost.CreateDefaultBuilder` | `WebHost` obsolete, **ASPDEPR008** | `WebApplication.CreateBuilder(args)` | | `new WebHostBuilder()` | obsolete, **ASPDEPR004** | `WebApplication` or `HostBuilder` | | `IWebHost` | obsolete, **ASPDEPR008** | `IHost` / `WebApplication` | | `IHostingEnvironment` | `[Obsolete]` | `IWebHostEnvironment` (`app.Environment`) | | `services.AddMvc()` for an API | works, registers views and pages too | `AddControllers()` | | `app.UseMvc()` | throws while `MvcOptions.EnableEndpointRouting` is `true` (the default) | `app.MapControllers()` | ## The target shape 1. `var builder = WebApplication.CreateBuilder(args);` 2. Everything `Startup`'s constructor read from `IConfiguration` now reads `builder.Configuration`. 3. The body of `ConfigureServices` moves line by line onto `builder.Services`. 4. `var app = builder.Build();` 5. The body of `Configure` moves below it; `env.IsDevelopment()` becomes `app.Environment.IsDevelopment()`. 6. `app.UseMvc()` becomes `app.MapControllers()`; explicit `UseRouting()` / `UseEndpoints(...)` boilerplate can go, because `WebApplication` adds routing and endpoint execution itself. 7. `app.Run();` Settings that used to sit on the builder chain — `UseContentRoot`, `UseEnvironment` — move into `WebApplicationOptions`; changing them through `builder.Host` or `builder.WebHost` throws `NotSupportedException`. ## Moving in stages A big `Startup` class does not have to be dissolved in one commit. Two supported bridges exist: - **Call the Startup class yourself.** Construct it with `builder.Configuration`, call `startup.ConfigureServices(builder.Services)` before `Build()`, then `startup.Configure(app, app.Environment)` after it, after changing the `Configure` signature to take `IWebHostEnvironment`. No reflection is involved, and services that `Configure` used to receive as extra parameters are resolved from `app.Services` instead. - **Stay on the Generic Host for now.** `Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(web => web.UseStartup<Startup>())` is not obsolete in .NET 10; only the `WebHost` / `WebHostBuilder` path is. That removes the warnings while the class is taken apart. What does **not** work is `builder.WebHost.UseStartup<Startup>()`: `WebApplicationBuilder` refuses it with `NotSupportedException`, and analyzer **ASP0010** warns at compile time. ## Behaviour that changes on the way - **No automatic scope at startup.** The minimal host does not create a DI scope when it builds the provider. Code in `Configure` that resolved the clinic's scoped `DbContext` to seed data or apply migrations now needs `using var scope = app.Services.CreateScope();`. - **Inline callbacks.** `builder.Host.ConfigureServices(...)` runs immediately, not deferred, so registration order follows the source text. - **Middleware added for you.** `WebApplication` inserts routing, authentication and authorization middleware when their services exist; code that relied on a specific placement may need explicit `Use...` calls (the ordering rules live with middleware order). - **Integration tests** that referenced `Startup` as the entry-point type need the new entry point instead. ## Verifying the move - The build is free of ASPDEPR004, ASPDEPR008, ASP0010 and `IHostingEnvironment` obsolete warnings. - `app.MapControllers()` finds the same routes; `[ApiController]` controllers keep their attribute routes. - The environment, content root and configuration values seen at startup match the old app's. - Startup seeding and migrations run inside an explicit scope. ## A sensible order of commits 1. **Retarget and build.** Move the project to `net10.0` with current packages and collect the obsolete warnings; nothing is rewritten yet. 2. **Bridge.** Replace `WebHost.CreateDefaultBuilder` with either the Generic Host plus `UseStartup`, or `WebApplication.CreateBuilder` calling the `Startup` methods by hand. Behaviour should be identical; the warnings disappear. 3. **Fix endpoint routing.** Swap `UseMvc` for `MapControllers`, and `AddMvc` for `AddControllers` if the clinic API serves no views. 4. **Dissolve Startup.** Inline `ConfigureServices` and `Configure` into `Program.cs`, or into extension methods such as `AddClinicServices(this IServiceCollection)` when the file grows. 5. **Delete the bridge** and the `Startup` class. Each step is small enough to review and to roll back, and the API's routes can be compared against the old build after every one.
- Can a .NET 10 app keep its Startup class while moving to WebApplicationBuilder?Yes, by calling it explicitly: `var startup = new Startup(builder.Configuration); startup.ConfigureServices(builder.Services);` before `Build()`, then `startup.Configure(app, app.Environment)` after it. `builder.WebHost.UseStartup<Startup>()` is the one form that throws `NotSupportedException`.
- Why does app.UseMvc() throw in a migrated ASP.NET Core 10 app?`MvcOptions.EnableEndpointRouting` defaults to `true`, and `UseMvc` throws `InvalidOperationException` when it is on, because it builds the pre-3.0 router. Replace it with `app.MapControllers()` (or `MapControllerRoute` for conventional routes) rather than turning endpoint routing off.
- Is a Startup class itself obsolete in .NET 10?No. `WebHostBuilder`, `WebHost` and `IWebHost` are obsolete; a `Startup` class used through `Host.CreateDefaultBuilder(...).ConfigureWebHostDefaults(web => web.UseStartup<Startup>())` is still supported. New code simply has no need for it.
- builder.WebHost.UseStartup<Startup>() lets the old Startup class run unchanged
- WebHost.CreateDefaultBuilder is still the supported entry point in .NET 10
- app.UseMvc() keeps working once the project targets net10.0
- Any Startup class is obsolete in .NET 10 and must be deleted
- Resolving a scoped DbContext from app.Services at startup needs no scope
Assumes ASP.NET Core 10: WebHostBuilder (ASPDEPR004) and WebHost / IWebHost (ASPDEPR008) became obsolete in 10.0; WebApplicationBuilder has been the template shape since 6.0.