public static object GetRequiredNamedService(this IServiceProvider serviceProvider, Type type, string key)
        {
            var namedServiceType = NamedServiceHelper.GenerateNamedServiceType(key, type);
            var namedService     = serviceProvider.GetRequiredService(namedServiceType) as INamedService;

            return(namedService?.Service);
        }
        public static T GetNamedService <T>(this IServiceProvider serviceProvider, Enum key) where T : class
        {
            var namedServiceType = NamedServiceHelper.GenerateNamedServiceType <T>(key);
            var namedService     = serviceProvider.GetService(namedServiceType) as INamedService <T>;

            return(namedService?.Service);
        }
Exemple #3
0
        public static IServiceCollection TryAddNamed(this IServiceCollection serviceCollection, Type type, Enum key, Func <IServiceProvider, object> implementationFactory, ServiceLifetime serviceLifetime)
        {
            var namedService = NamedServiceHelper.GenerateNamedServiceType(key, type);

            serviceCollection.TryAdd(new ServiceDescriptor(namedService, provider => Activator.CreateInstance(namedService, implementationFactory(provider)), serviceLifetime));

            return(serviceCollection);
        }
Exemple #4
0
        public static IServiceCollection TryAddNamed(this IServiceCollection serviceCollection, Type type, Enum key, object implementationInstance)
        {
            var namedService = NamedServiceHelper.GenerateNamedServiceType(key, type);
            var namedImplementationInstance = Activator.CreateInstance(namedService, implementationInstance);

            serviceCollection.TryAdd(new ServiceDescriptor(namedService, namedImplementationInstance));

            return(serviceCollection);
        }