RegisterCallback() public méthode

public RegisterCallback ( Action configurationCallback ) : void
configurationCallback Action
Résultat void
        protected override void Load(ContainerBuilder builder)
        {
            //builder.RegisterType<LogHealthTrackingService>()
            //    .As<IHealthTrackingService>()
            //    .SingleInstance();

            builder.RegisterType<MetricsHealthTrackingService>()
                .As<IHealthTrackingService>()
                .SingleInstance();

            builder.RegisterType<FrequencyTrackHelper>().InstancePerDependency();
            builder.RegisterType<DurationTrackHelper>().InstancePerDependency();

            builder.RegisterType<SchedulerServiceTracker>().SingleInstance();
            builder.RegisterType<PerformanceCountersTracker>().SingleInstance();

            builder.RegisterType<RandomTracker>().SingleInstance();

            builder.RegisterType<BandwidthTrackerDecorator>()
                .Named<IBrowsingSessionDecorator>("BandwidthTracker").SingleInstance();

            builder.RegisterCallback(
                (c) =>
                    {
                        var activityLogRegistration =
                            c.Registrations.Where(r => r.Activator.LimitType == typeof(ActivityLogService))
                                .FirstOrDefault();
                        activityLogRegistration.Activated +=
                            (s, x) =>
                                {
                                    var logFreq = x.Context.Resolve<FrequencyTrackHelper>();
                                    logFreq.Start(TimeSpan.FromMinutes(1), "logFreq");
                                    (x.Instance as ActivityLogService)
                                        .OnNewLog += (_) => logFreq.Notify();
                                };

                        var browsingSessionRegistration =
                            c.Registrations.Where(r => r.Activator.LimitType == typeof (StatefullBrowsingSessionWrapper))
                                .FirstOrDefault();
                        browsingSessionRegistration.Activating +=
                            (s,x) =>
                                {
                                    (x.Instance as IBrowsingSession)
                                        .AddDecorator(x.Context.ResolveNamed<IBrowsingSessionDecorator>("BandwidthTracker"));
                                };
                    });
        }
        protected override void Load(ContainerBuilder builder)
        {
            builder
                .Register(this.BuildMessageSubsciber)
                .As<ISubscribeToMessages>()
                .As<IUnsubscribeFromMessages>()
                .SingleInstance()
                .ExternallyOwned();

            builder
                .Register(this.BuildMessageHandlers)
                .As<SubscriptionMessageHandlers>()
                .InstancePerDependency() // this MessageContext only exists in child container, so we can't do SingleInstance
                .ExternallyOwned();

            this.RegisterMessageHandlerRoutes();

            if (this.requests.Count > 0)
                builder.RegisterCallback(this.OnContainerConfigured);
        }
        /// <summary>
        /// Check all <see cref="IComponentRegistration"/> and enable Interface or Class interceptor on the component if the register services have <see cref="OutputCacheAttribute"/> 
        /// decorated on the type or methods
        /// </summary>
        /// <param name="builder"></param>
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterModule(new FlatwhiteCoreModule());
            //var rb = RegistrationBuilder.ForType<IInterceptor>();

            //var k = new RegistrationBuilder<object, ConcreteReflectionActivatorData, SingleRegistrationStyle>(
            //    new TypedService(implementationType),
            //    new ConcreteReflectionActivatorData(implementationType),
            //    new SingleRegistrationStyle());

            //builder.RegisterCallback(cr => RegistrationBuilder.RegisterSingleComponent(cr, rb));

            builder.RegisterCallback(x =>
            {
                foreach (var r in x.Registrations)
                {
                    //var type = r.Activator.LimitType;
                    var types = r
                        .Services
                        .OfType<IServiceWithType>()
                        .Select(s => s.ServiceType).ToList();

                    var typesWithAttributes =
                           types.Where(type => type.GetCustomAttributes(typeof(MethodFilterAttribute), true).Any() ||
                                               type.GetCustomAttributes(typeof(ExceptionFilterAttribute), true).Any() ||
                                               type.GetMethods().SelectMany(p => p.GetCustomAttributes(typeof(MethodFilterAttribute), true)).Any() ||
                                               type.GetMethods().SelectMany(p => p.GetCustomAttributes(typeof(ExceptionFilterAttribute), true)).Any()).ToList();

                    if (typesWithAttributes.Count == 0) continue;

                    /*
                    builder.RegistrationStyle.Id,               = registration.Id
                    builder.RegistrationData,
                        data.Lifetime,                          = registration.Lifetime
                        data.Sharing,                           = registration.Sharing
                        data.Ownership,                         = registration.Ownership
                        services,                               = registration.Services
                        data.Metadata                           = registration.Metadata
                    builder.ActivatorData.Activator,            = registration.Activator
                    builder.RegistrationData.Services,          = registration.Services
                    builder.RegistrationStyle.Target            = registration.Target
                    */

                    IRegistrationBuilder<object, ConcreteReflectionActivatorData, SingleRegistrationStyle> rb;
                    //https://github.com/autofac/Autofac/blob/master/src/Autofac/Builder/RegistrationBuilder.cs#214
                    if (typesWithAttributes.Any(t => t.IsClass))
                    {
                        rb = RegistrationBuilder.ForType(typesWithAttributes.First(type => type.IsClass));
                        rb.EnableClassInterceptors().InterceptedBy(typeof (MethodInterceptorAdaptor));
                        ((ComponentRegistration)r).Activator = rb.ActivatorData.Activator;
                        // NOTE: Find where to set it to rb.ActivatorData.ImplementationType;
                    }
                    else
                    {
                        rb = RegistrationBuilder.ForType(typesWithAttributes.First(type => type.IsInterface));
                        rb.EnableInterfaceInterceptors().InterceptedBy(typeof(MethodInterceptorAdaptor));
                    }

                    foreach (var pair in rb.RegistrationData.Metadata)
                        r.Metadata[pair.Key] = pair.Value;
                    foreach (var p in rb.RegistrationData.PreparingHandlers)
                        r.Preparing += p;
                    foreach (var ac in rb.RegistrationData.ActivatingHandlers)
                        r.Activating += ac;
                    foreach (var ad in rb.RegistrationData.ActivatedHandlers)
                        r.Activated += ad;
                }
            });
        }
Exemple #4
0
        public static void EventFoo2()
        {
            var builder = new ContainerBuilder();

            builder.RegisterCallback(cr =>
            {
                // 下面的Registered事件相当类型的OnRegistered事件
                cr.Registered += (sender, eventArgs) =>
                {
                    Console.WriteLine(eventArgs);
                    // OnPreparing事件
                    eventArgs.ComponentRegistration.Preparing += (o, preparingEventArgs) =>
                    {
                        // 1.不做任何处理时,最后输出 result: 3
                        // 2.修改传入值,最后输出 result: 5
                        preparingEventArgs.Parameters = new[] { new NamedParameter("x", 2), new NamedParameter("y", 3) };
                        // 3.修改参数类型,最后输出 id: 7, name: 'undefined'
                        preparingEventArgs.Parameters = new Parameter[] { new PositionalParameter(0, 7), new TypedParameter(typeof(string), "undefined") };
                        // 4.直接不要参数,最后输出 no parameter constructor
                        preparingEventArgs.Parameters = Enumerable.Empty<Parameter>();

                    };
                    // OnActivating事件
                    eventArgs.ComponentRegistration.Activating += (o, activatingEventArgs) =>
                    {

                    };
                    // OnActivated事件
                    eventArgs.ComponentRegistration.Activated += (o, activatedEventArgs) =>
                    {

                    };
                };
            });

            builder.RegisterType<A>();
               IContainer container= builder.Build();
               A a = container.Resolve<A>();
        }