Short Answer
For most use cases, there is not a difference between the two. Both properties point to the same IServiceProvider instance, and both will get the same instance of a required singleton service. In edge cases, the timing of the calls might be different, but I could not think of an edge case that would cause that to happen. Unless we're doing something unusual, both will run only once, and that will be during application startup.
Experiment to Demonstrate
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSingleton<SomeSingltonService>();
}
public void Configure(IApplicationBuilder app)
{
var appServices = app.ApplicationServices;
var appService = appServices.GetRequiredService<SomeSingltonService>();
Console.WriteLine("=======================");
Console.WriteLine("Configure");
app.UseMvc(configureRoutes =>
{
var routeServices = routeBuilder.ServiceProvider;
var routeService = routeServices.GetRequiredService<SomeSingltonService>();
Console.WriteLine("UseMvc");
if (appServices == routeServices && appService == routeService)
{
Console.WriteLine("They are the same instances.");
}
});
Console.WriteLine("=======================");
}
}
This is the output:
=======================
Configure
UseMvc
They are the same instance.
=======================
Source Code to Demonstrate
Behind the scenes UseMvc passes the IApplicationBulder to the RouteBuilder constructor. Then the RouteBuilder assigns the IApplicationBulder.ApplicationServices to its own IRouteBuilder.ServiceProvider property.
Code from MvcApplicationBuilderExtensions.cs
public static IApplicationBuilder UseMvc(
this IApplicationBuilder app,
Action<IRouteBuilder> configureRoutes)
{
// ...
var routes = new RouteBuilder(app) // ln 136
{
DefaultHandler = app.ApplicationServices.GetRequiredService<MvcRouteHandler>(),
};
configureRoutes(routes);
// ...
}
Code from RouteBuilder.cs
public RouteBuilder(IApplicationBuilder applicationBuilder, IRouter defaultHandler)
{
// ...
ServiceProvider = applicationBuilder.ApplicationServices; // ln 36
// ...
}