Beispiel #1
0
        /// <inheritdoc/>
        public IServiceRegister Register(Type serviceType, Type implementationType, ServiceLifetime serviceLifetime, bool replace = false)
        {
            if (serviceType == null)
            {
                throw new ArgumentNullException(nameof(serviceType));
            }
            if (implementationType == null)
            {
                throw new ArgumentNullException(nameof(implementationType));
            }

            if (replace)
            {
                ServiceCollection.Replace(new ServiceDescriptor(serviceType, implementationType, TranslateLifetime(serviceLifetime)));
            }
            else
            {
                ServiceCollection.Add(new ServiceDescriptor(serviceType, implementationType, TranslateLifetime(serviceLifetime)));
            }
            return(this);
        }
Beispiel #2
0
        public static IGraphQLBuilder AddSelfActivatingSchema <TSchema>(this IGraphQLBuilder builder, ServiceLifetime serviceLifetime = ServiceLifetime.Singleton)
            where TSchema : class, ISchema
        {
            if (serviceLifetime == ServiceLifetime.Transient && typeof(IDisposable).IsAssignableFrom(typeof(TSchema)))
            {
                // This scenario can cause a memory leak if the schema is requested from the root service provider.
                // If it was requested from a scoped provider, then there is no reason to register it as transient.
                // See following link:
                // https://docs.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-guidelines#disposable-transient-services-captured-by-container
                throw new InvalidOperationException("A schema that implements IDisposable should not be registered as a transient service. " +
                                                    "See https://docs.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-guidelines#disposable-transient-services-captured-by-container");
            }

            // Register the service with the DI provider as TSchema, overwriting any existing registration
            builder.Register(provider =>
            {
                var selfActivatingServices = new SelfActivatingServiceProvider(provider);
                var schema = ActivatorUtilities.CreateInstance <TSchema>(selfActivatingServices);
                return(schema);
            }, serviceLifetime);

            // Now register the service as ISchema if not already registered.
            builder.TryRegister <ISchema>(provider =>
            {
                var selfActivatingServices = new SelfActivatingServiceProvider(provider);
                var schema = ActivatorUtilities.CreateInstance <TSchema>(selfActivatingServices);
                return(schema);
            }, serviceLifetime);

            return(builder);
        }
Beispiel #3
0
 private static MSServiceLifetime TranslateLifetime(ServiceLifetime serviceLifetime)
 => serviceLifetime switch
 {
Beispiel #4
0
        /// <inheritdoc/>
        public IServiceRegister TryRegister(Type serviceType, Func <IServiceProvider, object> implementationFactory, ServiceLifetime serviceLifetime, RegistrationCompareMode mode = RegistrationCompareMode.ServiceType)
        {
            if (serviceType == null)
            {
                throw new ArgumentNullException(nameof(serviceType));
            }
            if (implementationFactory == null)
            {
                throw new ArgumentNullException(nameof(implementationFactory));
            }

            var descriptor = new ServiceDescriptor(serviceType, implementationFactory, TranslateLifetime(serviceLifetime));

            if (mode == RegistrationCompareMode.ServiceType)
            {
                ServiceCollection.TryAdd(descriptor);
            }
            else if (mode == RegistrationCompareMode.ServiceTypeAndImplementationType)
            {
                ServiceCollection.TryAddEnumerable(descriptor);
            }
            else
            {
                throw new ArgumentOutOfRangeException(nameof(mode));
            }

            return(this);
        }
Beispiel #5
0
        public override IGraphQLBuilder Register(Type serviceType, Type implementationType, ServiceLifetime serviceLifetime)
        {
            if (serviceType == null)
            {
                throw new ArgumentNullException(nameof(serviceType));
            }
            if (implementationType == null)
            {
                throw new ArgumentNullException(nameof(implementationType));
            }

            _services.Add(new ServiceDescriptor(serviceType, implementationType, TranslateLifetime(serviceLifetime)));
            return(this);
        }
Beispiel #6
0
        public override IGraphQLBuilder TryRegister(Type serviceType, Func <IServiceProvider, object> implementationFactory, ServiceLifetime serviceLifetime)
        {
            if (serviceType == null)
            {
                throw new ArgumentNullException(nameof(serviceType));
            }
            if (implementationFactory == null)
            {
                throw new ArgumentNullException(nameof(implementationFactory));
            }

            _services.TryAdd(new ServiceDescriptor(serviceType, implementationFactory, TranslateLifetime(serviceLifetime)));
            return(this);
        }
Beispiel #7
0
        public override IGraphQLBuilder Register(Type serviceType, Func <IServiceProvider, object> implementationFactory, ServiceLifetime serviceLifetime, bool replace = false)
        {
            if (serviceType == null)
            {
                throw new ArgumentNullException(nameof(serviceType));
            }
            if (implementationFactory == null)
            {
                throw new ArgumentNullException(nameof(implementationFactory));
            }

            if (replace)
            {
                Services.Replace(new ServiceDescriptor(serviceType, implementationFactory, TranslateLifetime(serviceLifetime)));
            }
            else
            {
                Services.Add(new ServiceDescriptor(serviceType, implementationFactory, TranslateLifetime(serviceLifetime)));
            }
            return(this);
        }