Example #1
0
        public void Should_Get_Custom_Exposed_Types_If_Available()
        {
            //Act

            var exposedServices = ExposedServiceExplorer.GetExposedServices(typeof(ExplicitDerivedService));

            //Assert

            exposedServices.Count.ShouldBe(1);
            exposedServices.ShouldContain(typeof(IDerivedService));
        }
Example #2
0
        public void Should_Get_Conventional_Exposed_Types_By_Default()
        {
            //Act

            var exposedServices = ExposedServiceExplorer.GetExposedServices(typeof(DefaultDerivedService));

            //Assert

            exposedServices.Count.ShouldBe(3);
            exposedServices.ShouldContain(typeof(DefaultDerivedService));
            exposedServices.ShouldContain(typeof(IService));
            exposedServices.ShouldContain(typeof(IDerivedService));
        }
        public override void AddType(IServiceCollection services, Type type)
        {
            if (IsConventionalRegistrationDisabled(type))
            {
                return;
            }

            var dependencyAttribute = GetDependencyAttributeOrNull(type);
            var lifeTime            = GetLifeTimeOrNull(type, dependencyAttribute);

            if (lifeTime == null)
            {
                return;
            }

            var exposedServiceTypes = ExposedServiceExplorer.GetExposedServices(type);

            TriggerServiceExposing(services, type, exposedServiceTypes);

            foreach (var exposedServiceType in exposedServiceTypes)
            {
                var serviceDescriptor = CreateServiceDescriptor(
                    type,
                    exposedServiceType,
                    exposedServiceTypes,
                    lifeTime.Value
                    );

                if (dependencyAttribute?.ReplaceServices == true)
                {
                    services.Replace(serviceDescriptor);
                }
                else if (dependencyAttribute?.TryRegister == true)
                {
                    services.TryAdd(serviceDescriptor);
                }
                else
                {
                    services.Add(serviceDescriptor);
                }
            }
        }
 protected virtual List <Type> GetExposedServiceTypes(Type type)
 {
     return(ExposedServiceExplorer.GetExposedServices(type));
 }