© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
12 replies
steven preadly

✅ ASP.NET Core: Confusion with builder.Services.AddTransient() and App Lifetime Management

I'm new to ASP.NET Core, and I'm encountering some confusion regarding service registration. In the code snippet below:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddTransient<RequestCultureMiddleware>();
var app = builder.Build();
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddTransient<RequestCultureMiddleware>();
var app = builder.Build();

We see that builder.Services.AddTransient<RequestCultureMiddleware>() is used to register the RequestCultureMiddleware service with a transient lifetime. However, I understand that the WebApplication.CreateBuilder(args) method builds the application configuration, while service registration is performed at the application level using app.

My question is: Why can't we register services like RequestCultureMiddleware directly with app.Services.AddTransient<RequestCultureMiddleware>() instead of using builder.Services? What's the underlying principle behind this separation?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

asp.net core services initialization
C#CC# / help
4y ago
✅ ASP.NET CORE with React.js or ASP.NET CORE WEB APP
C#CC# / help
3y ago
Frontend package management in ASP.NET Core
C#CC# / help
4y ago