1.6 KiB
Consider the following ConfigureServices
method, which registers services and configures options:
Related groups of registrations can be moved to an extension method to register services. For example, the configuration services are added to the following class:
The remaining services are registered in a similar class. The following ConfigureServices
method uses the new extension methods to register the services:
Note: Each services.Add{GROUP_NAME}
extension method adds and potentially configures services. For example, xref:Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddControllersWithViews%2A adds the services MVC controllers with views require, and xref:Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddRazorPages%2A adds the services Razor Pages requires. We recommend that apps follow the naming convention of creating extension methods in the xref:Microsoft.Extensions.DependencyInjection?displayProperty=fullName namespace. Creating extension methods in the Microsoft.Extensions.DependencyInjection
namespace:
- Encapsulates groups of service registrations.
- Provides convenient IntelliSense access to the service.