-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure Magneto throws meaningful exception when context is not regist…
…ered
- Loading branch information
1 parent
8b5a391
commit aa3482b
Showing
3 changed files
with
25 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,20 @@ | ||
using System; | ||
using System; | ||
|
||
namespace Magneto.Core | ||
{ | ||
static class ServiceProviderExtensions | ||
{ | ||
internal static T GetService<T>(this IServiceProvider serviceProvider) => (T)serviceProvider.GetService(typeof(T)); | ||
|
||
internal static T GetRequiredService<T>(this IServiceProvider serviceProvider) | ||
{ | ||
var serviceType = typeof(T); | ||
var service = serviceProvider.GetService(serviceType); | ||
|
||
if (service == null) | ||
throw new InvalidOperationException($"No service for type '{serviceType.FullName}' has been registered."); | ||
|
||
return (T)service; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters