private ComponentRegistration <T> WithLifespan <T>( ComponentRegistration <T> componentRegistration, Lifespan lifespan) where T : class { if (lifespan == Lifespan.Singleton) { return(componentRegistration.LifestyleSingleton()); } else if (lifespan == Lifespan.Transient) { return(componentRegistration.LifeStyle.Transient); } else if (lifespan is Lifespan.LifespanTypeBound typeBound) { var method = componentRegistration.GetType().GetMethod("LifestyleBoundTo", new Type[] { }); var genericMethod = method.MakeGenericMethod(typeBound.BoundType); return((ComponentRegistration <T>)genericMethod.Invoke( componentRegistration, new object[0])); } else { throw new ArgumentException("invalid lifespan"); } }
public void Register <TContract, TService>(LifeCycle lifeCycle, Func <TContract> factoryMethod = null) where TContract : class where TService : TContract { ComponentRegistration <TContract> component = Component.For <TContract>().ImplementedBy <TService>(); switch (lifeCycle) { case LifeCycle.Transient: component = component.LifestyleTransient(); break; case LifeCycle.Singletone: component = component.LifestyleSingleton(); break; case LifeCycle.PerThread: component = component.LifestylePerThread(); break; default: throw new NotSupportedException("Lifecycle"); } if (factoryMethod != null) { component = component.UsingFactoryMethod(factoryMethod, managedExternally: true); } _Container.Register(component); }
private static ComponentRegistration <T> SetLifeStyle <T>(LifeStyleType lifeStyle, ComponentRegistration <T> registration) where T : class { if (lifeStyle == LifeStyleType.Singleton) { return(registration.LifestyleSingleton()); } else if (lifeStyle == LifeStyleType.Transient) { return(registration.LifestyleTransient()); } else if (lifeStyle == LifeStyleType.PerWebRequest) { return(registration.LifestylePerWebRequest()); } else if (lifeStyle == LifeStyleType.PerThread) { return(registration.LifestylePerThread()); } else if (lifeStyle == LifeStyleType.Scoped) { return(registration.LifestyleScoped()); } return(registration); }
private void ConfigureComponent([NotNull] ComponentRegistration obj) { if (obj.Implementation.HasCustomAttribute <SingletonAttribute>()) { obj.LifestyleSingleton(); } //obj.LifestyleTransient(); }
private static ComponentRegistration <T> ApplyLifestyle <T>(ComponentRegistration <T> registration, DependencyLifeStyle lifeStyle) where T : class { switch (lifeStyle) { case DependencyLifeStyle.Transient: return(registration.LifestyleTransient()); case DependencyLifeStyle.Singleton: return(registration.LifestyleSingleton()); default: return(registration); } }
private static ComponentRegistration <object> RegisterComponents(this ComponentRegistration <object> builder, EDependencyLifecycle lifecycle) { switch (lifecycle) { case EDependencyLifecycle.Singleton: builder.LifestyleSingleton(); break; default: throw new ArgumentOutOfRangeException(nameof(lifecycle), lifecycle, null); } return(builder); }
private static ComponentRegistration <T> AplicarEstiloDeVida <T>(ComponentRegistration <T> registracao, EstiloDeVidaDependencia estiloDeVida) where T : class { switch (estiloDeVida) { case EstiloDeVidaDependencia.Singleton: return(registracao.LifestyleSingleton()); case EstiloDeVidaDependencia.Transient: return(registracao.LifestyleTransient()); default: return(registracao); } }
private void RegisterWindsor(Microsoft.Extensions.DependencyInjection.ServiceDescriptor item) { try { ComponentRegistration <object> r = null; if (item.ImplementationType != null) { if (_container.Kernel.HasComponent(item.ImplementationType)) { return; } r = Component.For(item.ServiceType).ImplementedBy(item.ImplementationType); } else if (item.ImplementationFactory != null) { var provider = WindsorRegistrationHelper.CreateServiceProvider(_container, this); r = Component.For(item.ServiceType).UsingFactoryMethod(() => item.ImplementationFactory.Invoke(provider)); } else if (item.ImplementationInstance != null) { r = Component.For(item.ServiceType).UsingFactoryMethod(() => item.ImplementationInstance); } if (item.Lifetime == ServiceLifetime.Scoped) { _container.Register(r.LifestyleScoped()); } else if (item.Lifetime == ServiceLifetime.Transient) { _container.Register(r.LifestyleTransient()); } else if (item.Lifetime == ServiceLifetime.Singleton) { _container.Register(r.LifestyleSingleton()); } } catch (Exception ex) { if (!ex.Message.Contains("Component Microsoft.Extensions.Options.OptionsManager`1 could not be registered") && !ex.Message.Contains("Component Late bound Microsoft.Extensions.Options.IConfigureOptions`1[[Microsoft.Extensions.Logging.LoggerFilterOptions, Microsoft.Extensions.Logging")) { // Known issue at: https://gist.github.com/cwe1ss/050a531e2711f5b62ab0 throw; } } }
/// <summary> /// Lifestyle conversion and application /// </summary> public static ComponentRegistration <T> ApplyLifestyle <T>(this ComponentRegistration <T> registration, DependencyLifeStyle lifeStyle) where T : class { switch (lifeStyle) { case DependencyLifeStyle.Transient: return(registration.LifestyleTransient()); case DependencyLifeStyle.Scoped: return(registration.LifestyleScoped <LocalLifetimeScopeAccessor>()); case DependencyLifeStyle.Singleton: return(registration.LifestyleSingleton()); default: throw new ArgumentException(nameof(lifeStyle)); } }
ComponentRegistration <T> Lifestyle <T>(ComponentRegistration <T> registration, FeignClientLifetime lifetime) where T : class { switch (lifetime) { case FeignClientLifetime.Transient: return(registration.LifestyleTransient()); case FeignClientLifetime.Singleton: return(registration.LifestyleSingleton()); case FeignClientLifetime.Scoped: return(registration.LifestyleScoped()); default: return(registration); } }
/// <summary> /// Set the lifestyle of the registered builder /// </summary> private ComponentRegistration <T> ApplyLifestyle <T>(ComponentRegistration <T> registration, DependencyLifeStyle lifeStyle) where T : class { switch (lifeStyle) { case DependencyLifeStyle.Transient: return(registration.LifestyleTransient()); case DependencyLifeStyle.Scoped: return(registration.LifestyleScoped()); case DependencyLifeStyle.Singleton: return(registration.LifestyleSingleton()); default: throw new ArgumentException(nameof(lifeStyle)); } }
public static ComponentRegistration <T> WithLifetime <T>(this ComponentRegistration <T> registration, Lifetime lifetime) where T : class { switch (lifetime) { case Lifetime.Transient: return(registration.LifestyleTransient()); case Lifetime.Scope: return(registration.LifestyleScoped()); case Lifetime.Singleton: return(registration.LifestyleSingleton()); default: throw new NotSupportedException(lifetime.ToString()); } }
private static ComponentRegistration <object> ConfigureLifecycle( this ComponentRegistration <object> registrationBuilder, ServiceLifetime serviceLifetime) { switch (serviceLifetime) { case ServiceLifetime.Singleton: registrationBuilder.LifestyleSingleton(); break; case ServiceLifetime.Scoped: registrationBuilder.LifestyleScoped(); break; case ServiceLifetime.Transient: registrationBuilder.LifestyleTransient(); break; } return(registrationBuilder); }
private static ComponentRegistration <object> ConfigureLifecycle(this ComponentRegistration <object> registrationBuilder, ServiceLifetime serviceLifetime) { switch (serviceLifetime) { case ServiceLifetime.Transient: registrationBuilder.LifestyleTransient(); break; case ServiceLifetime.Scoped: registrationBuilder.LifestylePerWebRequest(); break; case ServiceLifetime.Singleton: registrationBuilder.LifestyleSingleton(); break; default: throw new InvalidOperationException("Unknown ServiceLifetime: " + serviceLifetime); } return(registrationBuilder); }
private static ComponentRegistration <object> ConfigureLifecycle(this ComponentRegistration <object> registrationBuilder, ServiceLifetime serviceLifetime) { switch (serviceLifetime) { case ServiceLifetime.Transient: registrationBuilder.LifestyleTransient(); break; case ServiceLifetime.Scoped: registrationBuilder.LifestyleCustom <MsScopedLifestyleManager>(); break; case ServiceLifetime.Singleton: registrationBuilder.LifestyleSingleton(); break; default: throw new NotImplementedException("Unknown ServiceLifetime: " + serviceLifetime); } return(registrationBuilder); }
internal static ComponentRegistration <T> ApplyLifestyle <T>(this ComponentRegistration <T> registration, LifetimeType lifetimeType, bool classicWeb = false) where T : class { switch (lifetimeType) { case LifetimeType.Transient: return(registration.LifestyleTransient()); //return registration.LifestyleCustom<MsScopedTransientLifestyleManager>(); case LifetimeType.Singleton: return(registration.LifestyleSingleton()); //return registration.LifestyleCustom<MsScopedLifestyleManager>(); case LifetimeType.Scoped: //return classicWeb ? registration.LifestylePerWebRequest() : registration.LifestyleCustom<MyScopedSingletonLifestyleManager>(); //registration.LifestyleScoped();//// return(classicWeb ? registration.LifestylePerWebRequest() : registration.LifestyleScoped()); //// default: return(registration); } }
private static void Configure(ComponentRegistration registration) { registration .LifestyleSingleton(); }
private static ComponentRegistration<object> InitializeLifeStyle(IComponent component, ComponentRegistration<object> componentFor) { switch (component.LifeStyle) { case (LifeStyle.Singleton): return componentFor.LifestyleSingleton(); case (LifeStyle.Transient): return componentFor.LifestyleTransient(); case (LifeStyle.WebRequest): return componentFor.LifestylePerWebRequest(); case (LifeStyle.Pooled): return componentFor.LifestylePooled(); default: throw new ContainerException("LifeStyle not allowed " + component.LifeStyle); } }