protected override void AttachToComponentRegistration(IComponentRegistryBuilder componentRegistry, IComponentRegistration registration) { //// Ignore components that provide loggers (and thus avoid a circular dependency below) //if (registration.Services.OfType<TypedService>().Any(ts => ts.ServiceType == typeof(ILogger))) // return; bool hasCtorLogger = false; bool hasPropertyLogger = false; FastProperty[] loggerProperties = null; var ra = registration.Activator as ReflectionActivator; if (ra != null) { // // Look for ctor parameters of type "ILogger" var ctors = GetConstructorsSafe(ra); var loggerParameters = ctors.SelectMany(ctor => ctor.GetParameters()).Where(pi => pi.ParameterType == typeof(ILogger)); hasCtorLogger = loggerParameters.Any(); // Autowire properties // Look for settable properties of type "ILogger" loggerProperties = ra.LimitType .GetProperties(BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance) .Select(p => new { PropertyInfo = p, p.PropertyType, IndexParameters = p.GetIndexParameters(), Accessors = p.GetAccessors(false) }) .Where(x => x.PropertyType == typeof(ILogger)) // must be a logger .Where(x => x.IndexParameters.Count() == 0) // must not be an indexer .Where(x => x.Accessors.Length != 1 || x.Accessors[0].ReturnType == typeof(void)) //must have get/set, or only set .Select(x => FastProperty.Create(x.PropertyInfo)) .ToArray(); hasPropertyLogger = loggerProperties.Length > 0; // Ignore components known to be without logger dependencies if (!hasCtorLogger && !hasPropertyLogger) { return; } if (hasPropertyLogger) { registration.Metadata.Add("LoggerProperties", loggerProperties); } } registration.PipelineBuilding += (sender, pipeline) => { // Add our middleware to the pipeline. pipeline.Use(new AutofacSerilogMiddleware(registration.Activator.LimitType, hasCtorLogger, hasPropertyLogger)); }; }
public CachedMember(MemberInfo mi) : base(GetMemberType(mi)) { Member = mi; if (Member is PropertyInfo) { fastproperty = FastProperty.Create((PropertyInfo)Member); } }
protected override void AttachToComponentRegistration(IComponentRegistryBuilder componentRegistry, IComponentRegistration registration) { if (!DataSettings.DatabaseIsInstalled()) { return; } var userProperty = FindUserProperty(registration.Activator.LimitType); if (userProperty == null) { return; } registration.Metadata.Add("Property.T", FastProperty.Create(userProperty)); registration.PipelineBuilding += (sender, pipeline) => { // Add our Localizer middleware to the pipeline. pipeline.Use(PipelinePhase.ParameterSelection, (context, next) => { next(context); if (!context.Resolve <IEngine>().IsInitialized) { return; } if (!context.NewInstanceActivated || context.Registration.Metadata.Get("Property.T") is not FastProperty prop) { return; } try { var iText = context.Resolve <IText>(); if (prop.Property.PropertyType == typeof(Localizer)) { Localizer localizer = context.Resolve <IText>().Get; prop.SetValue(context.Instance, localizer); } else { LocalizerEx localizerEx = context.Resolve <IText>().GetEx; prop.SetValue(context.Instance, localizerEx); } } catch { } }); };
protected override void AttachToComponentRegistration(IComponentRegistryBuilder componentRegistry, IComponentRegistration registration) { // Look for first settable property of type "ICommonServices" and inject var servicesProperty = FindCommonServicesProperty(registration.Activator.LimitType); if (servicesProperty == null) { return; } registration.Metadata.Add("Property.ICommonServices", FastProperty.Create(servicesProperty)); registration.PipelineBuilding += (sender, pipeline) => { // Add our CommonServices middleware to the pipeline. pipeline.Use(PipelinePhase.ParameterSelection, (context, next) => { next(context); if (!context.Resolve <IEngine>().IsInitialized) { return; } if (!DataSettings.DatabaseIsInstalled()) { return; } if (!context.NewInstanceActivated || context.Registration.Metadata.Get("Property.ICommonServices") is not FastProperty prop) { return; } try { var services = context.Resolve <ICommonServices>(); prop.SetValue(context.Instance, services); } catch { } }); };
protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration) { bool hasCtorLogger = false; bool hasPropertyLogger = false; FastProperty[] loggerProperties = null; var ra = registration.Activator as ReflectionActivator; if (ra != null) { // // Look for ctor parameters of type "ILogger" var ctors = ra.ConstructorFinder.FindConstructors(ra.LimitType); var loggerParameters = ctors.SelectMany(ctor => ctor.GetParameters()).Where(pi => pi.ParameterType == typeof(ILogger)); hasCtorLogger = loggerParameters.Any(); // Autowire properties // Look for settable properties of type "ILogger" loggerProperties = ra.LimitType .GetProperties(BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance) .Select(p => new { PropertyInfo = p, p.PropertyType, IndexParameters = p.GetIndexParameters(), Accessors = p.GetAccessors(false) }) .Where(x => x.PropertyType == typeof(ILogger)) // must be a logger .Where(x => x.IndexParameters.Count() == 0) // must not be an indexer .Where(x => x.Accessors.Length != 1 || x.Accessors[0].ReturnType == typeof(void)) //must have get/set, or only set .Select(x => FastProperty.Create(x.PropertyInfo)) .ToArray(); hasPropertyLogger = loggerProperties.Length > 0; // Ignore components known to be without logger dependencies if (!hasCtorLogger && !hasPropertyLogger) { return; } if (hasPropertyLogger) { registration.Metadata.Add("LoggerProperties", loggerProperties); } } if (hasCtorLogger) { registration.Preparing += (sender, args) => { var logger = GetLoggerFor(args.Component.Activator.LimitType, args.Context); args.Parameters = new[] { TypedParameter.From(logger) }.Concat(args.Parameters); }; } if (hasPropertyLogger) { registration.Activating += (sender, args) => { var logger = GetLoggerFor(args.Component.Activator.LimitType, args.Context); var loggerProps = args.Component.Metadata.Get("LoggerProperties") as FastProperty[]; if (loggerProps != null) { foreach (var prop in loggerProps) { prop.SetValue(args.Instance, logger); } } }; } }