The difference between GetService() and GetRequiredService() in ASP.NET Core

added by DotNetKicks
9/11/2018 10:19:18 AM

2 Kicks, 191 Views

This post looks at the GetService () and GetRequiredService () methods of the default/built-in ASP.NET Core DI container, provided in Microsoft.Extensions.DependencyInjection. I'll describe the differences between them and which one you should use. tl;dr GetService() returns null if a service does not exist, GetRequiredService() throws an exception instead.