In this particular case, Add*
is not affected by the order they are added to the service collection.
However, depending on the implementation of the particular Add*
extension, the order may affect configuration. For example, if internally it uses the TryAdd*
extension, then only the first call registers. Subsequent calls wont add as a registration would already exist.
The general AddScoped/AddSingleton/AddTransient
calls recognizes the last call for a type as it overrides the previous registration calls for that type. When registering multiple implementations for a type, for resolving via IEnumerable<T>
then the implementations in the collection will be in the order they were registered.
Reference Dependency injection in ASP.NET Core
For the Use*
middleware the order they are added to the pipeline is important as they are invoked in the same order.
Order
The order that middleware components are added in the Startup.Configure
method defines the order in which the middleware
components are invoked on requests and the reverse order for the
response. The order is critical for security, performance, and
functionality.
Reference ASP.NET Core Middleware
In my experience, depending on their designed purpose, some 3rd party integrations (including swagger) that need access to the pipeline suggest adding their extensions after AddMvc
to try and avoid route conflicts.
Most security (Authentication/Authorization) and logging middleware are usually suggested to be added early in the pipeline, so they tend to come before AddMvc
.