예제 #1
0
        public static List <ServiceRegistrationResult> TryRegisterService(this IServiceCollection services, Type type, ServiceBuilderDelegate instantiate = null)
        {
            List <ServiceRegistrationResult> result = null;

            if (!type.IsClass || type.IsAbstract)
            {
                return(result);
            }

            var serviceAttributes = type.GetCustomAttributes(typeof(ServiceAttribute), false)
                                    .OfType <ServiceAttribute>().ToList();

            foreach (var serviceAttribute in serviceAttributes)
            {
                if (result == null)
                {
                    result = new List <ServiceRegistrationResult>();
                }

                result.Add(ServiceRegistration.Register(services, serviceAttribute, type, instantiate));
            }

            return(result);
        }
예제 #2
0
        public static ServiceRegistrationResult Register(IServiceCollection services, ServiceAttribute attribute, Type declaredType, ServiceBuilderDelegate instantiate = null)
        {
            if (declaredType == null)
            {
                throw new NotSupportedException();
            }

            var serviceType = attribute.ServiceType;

            if (serviceType == null)
            {
                serviceType = declaredType;
            }

            if (declaredType.ContainsGenericParameters && serviceType.ContainsGenericParameters)
            {
                // Open generic type
                if (instantiate != null)
                {
                    throw new NotSupportedException("Cannot provide delegate for instantiation for open generic types.");
                }
            }
            else
            {
                if (declaredType.ContainsGenericParameters && !serviceType.ContainsGenericParameters)
                {
                    // We are registering services on a generic type implementation
                    declaredType = declaredType.MakeGenericType(serviceType.GetGenericArguments());
                }
            }

            services.Add(instantiate != null
                ? new ServiceDescriptor(serviceType, provider => instantiate(provider, serviceType, declaredType),
                                        attribute.Lifetime)
                : new ServiceDescriptor(serviceType, declaredType, attribute.Lifetime));

            return(new ServiceRegistrationResult(serviceType, declaredType, attribute.Lifetime));
        }
예제 #3
0
        public static void Register(IServiceCollection services, ServiceAttribute attribute, Type declaredType, ServiceBuilderDelegate instantiate = null)
        {
            if (instantiate == null)
            {
                instantiate = (provider, service, implementation) =>
                              ActivatorUtilities.CreateInstance(provider, implementation);
            }

            if (declaredType == null)
            {
                // Not supported (yet)
                throw new NotImplementedException();
            }

            var serviceType = attribute.ServiceType;

            if (serviceType == null)
            {
                serviceType = declaredType;
            }

            if (declaredType.IsGenericType)
            {
                // We are registering services on a generic type implementation
                declaredType = declaredType.MakeGenericType(serviceType.GetGenericArguments());
            }

            switch (attribute.Lifetime)
            {
            case ServiceLifetime.Scoped:
                services.AddScoped(serviceType, provider => instantiate(provider, serviceType, declaredType));
                break;

            case ServiceLifetime.Singleton:
                services.AddSingleton(serviceType, provider => instantiate(provider, serviceType, declaredType));
                break;

            case ServiceLifetime.Transient:
                services.AddTransient(serviceType, provider => instantiate(provider, serviceType, declaredType));
                break;
            }
        }