public void Add_TypeList() { var list = new ServiceInterceptorList(); list.GetInterceptors(typeof(IProxiedService)).ShouldBeEmpty(); Should.Throw <ArgumentNullException>(() => list.Add(typeof(IProxiedService), (Type)null)); Should.Throw <ArgumentNullException>(() => list.Add(null, (ITypeList <IInterceptor>)null)); Should.Throw <ArgumentNullException>(() => list.Add(null, new TypeList <IInterceptor>())); list.Add(typeof(IProxiedService), new TypeList <IInterceptor>()); list.GetInterceptors(typeof(IProxiedService)).ShouldBeEmpty(); list.Add(typeof(IProxiedService), new TypeList <IInterceptor>().Action(l => l.Add <TestInterceptor>())); list.GetInterceptors(typeof(IProxiedService)).ShouldHaveSingleItem().ShouldBe(typeof(TestInterceptor)); }
public void EnablePropertyInjection() { var build = new ContainerBuilder().RegisterType <PropertyInjectionService>(); var interceptors = new ServiceInterceptorList(); interceptors.Add(typeof(TestProxiedService), typeof(TestInterceptor)); var container = Substitute.For <IModuleContainer>(); var module = Substitute.For <IModuleDescriptor>(); module.Configure().Assembly.Returns(typeof(PropertyInjectionService).Assembly); container.Configure().Modules.Returns(new List <IModuleDescriptor> { module }); Should.NotThrow(() => build.ConfigureConventions(container, interceptors)); build.RegistrationData.ActivatingHandlers.ShouldHaveSingleItem(); }
public void ConfigureConventions() { var build = new ContainerBuilder().RegisterType <TestProxiedService>().As <IProxiedService>(); var interceptors = new ServiceInterceptorList(); var container = Substitute.For <IModuleContainer>(); Should.Throw <ArgumentNullException>(() => build.ConfigureConventions(null, null)); Should.Throw <ArgumentNullException>(() => build.ConfigureConventions(null, interceptors)); Should.Throw <ArgumentNullException>(() => build.ConfigureConventions(container, null)); Should.NotThrow(() => build.ConfigureConventions(container, interceptors)); build = null; Should.Throw <ArgumentNullException>(() => build.ConfigureConventions(null, null)); Should.Throw <ArgumentNullException>(() => build.ConfigureConventions(null, interceptors)); Should.Throw <ArgumentNullException>(() => build.ConfigureConventions(container, null)); Should.Throw <ArgumentNullException>(() => build.ConfigureConventions(container, interceptors)); }
public static IRegistrationBuilder <TLimit, TActivatorData, TRegistrationStyle> ConfigureConventions <TLimit, TActivatorData, TRegistrationStyle>( this IRegistrationBuilder <TLimit, TActivatorData, TRegistrationStyle> registrationBuilder, IModuleContainer moduleContainer, ServiceInterceptorList serviceInterceptorList) where TActivatorData : ReflectionActivatorData { Check.NotNull(moduleContainer, nameof(moduleContainer)); Check.NotNull(registrationBuilder, nameof(registrationBuilder)); Check.NotNull(serviceInterceptorList, nameof(serviceInterceptorList)); var serviceType = registrationBuilder.RegistrationData.Services.OfType <IServiceWithType>().FirstOrDefault()?.ServiceType; if (serviceType != null) { var implementationType = registrationBuilder.ActivatorData.ImplementationType; if (implementationType != null) { registrationBuilder = registrationBuilder.EnablePropertyInjection(moduleContainer, implementationType); registrationBuilder = registrationBuilder.InvokeRegistrationActions(serviceInterceptorList, serviceType, implementationType); } } return(registrationBuilder); }
public void InvokeRegistrationActions() { var build = new ContainerBuilder().RegisterType <TestProxiedService>(); var interceptors = new ServiceInterceptorList(); interceptors.Add(typeof(TestProxiedService), typeof(TestInterceptor)); var container = Substitute.For <IModuleContainer>(); Should.NotThrow(() => build.ConfigureConventions(container, interceptors)); build.RegistrationData.Metadata.Where(kv => kv.Key == "Autofac.Extras.DynamicProxy.RegistrationExtensions.InterceptorsPropertyName").ShouldHaveSingleItem().Action(kv => { kv.Key.ShouldBe("Autofac.Extras.DynamicProxy.RegistrationExtensions.InterceptorsPropertyName"); kv.Value.As <IEnumerable <Service> >().ShouldHaveSingleItem().ShouldBeOfType <TypedService>().ServiceType.ShouldBe(typeof(AsyncDeterminationInterceptor <TestInterceptor>)); } ); build = new ContainerBuilder().RegisterType <TestProxiedService>().As <IProxiedService>(); Should.NotThrow(() => build.ConfigureConventions(container, interceptors)); build.RegistrationData.Metadata.Where(kv => kv.Key == "Autofac.Extras.DynamicProxy.RegistrationExtensions.InterceptorsPropertyName").ShouldHaveSingleItem().Action(kv => { kv.Key.ShouldBe("Autofac.Extras.DynamicProxy.RegistrationExtensions.InterceptorsPropertyName"); kv.Value.As <IEnumerable <Service> >().ShouldHaveSingleItem().ShouldBeOfType <TypedService>().ServiceType.ShouldBe(typeof(AsyncDeterminationInterceptor <TestInterceptor>)); } ); }
private static IRegistrationBuilder <TLimit, TActivatorData, TRegistrationStyle> InvokeRegistrationActions <TLimit, TActivatorData, TRegistrationStyle>(this IRegistrationBuilder <TLimit, TActivatorData, TRegistrationStyle> registrationBuilder, ServiceInterceptorList serviceInterceptorList, Type serviceType, Type implementationType) where TActivatorData : ReflectionActivatorData { var interceptors = serviceInterceptorList.GetInterceptors(serviceType).Concat(serviceInterceptorList.GetInterceptors(implementationType)); if (interceptors.Any()) { registrationBuilder = registrationBuilder.AddInterceptors( serviceType, interceptors ); } return(registrationBuilder); }