mirror of https://github.com/abpframework/abp.git
5 changed files with 44 additions and 19 deletions
@ -0,0 +1,26 @@ |
|||
using System; |
|||
using System.Linq; |
|||
|
|||
namespace Microsoft.Extensions.DependencyInjection |
|||
{ |
|||
public static class ServiceCollectionCommonExtensions |
|||
{ |
|||
public static T GetSingletonInstanceOrNull<T>(this IServiceCollection services) |
|||
{ |
|||
return (T)services |
|||
.FirstOrDefault(d => d.ServiceType == typeof(T)) |
|||
?.ImplementationInstance; |
|||
} |
|||
|
|||
public static T GetSingletonInstance<T>(this IServiceCollection services) |
|||
{ |
|||
var service = services.GetSingletonInstanceOrNull<T>(); |
|||
if (service == null) |
|||
{ |
|||
throw new InvalidOperationException("Could not find singleton service: " + typeof(T).AssemblyQualifiedName); |
|||
} |
|||
|
|||
return service; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue