コード例 #1
0
 private TypeRegistration CreateLoggingUpdateCoordinatorRegistration()
 {
     return
         (new TypeRegistration <ILoggingUpdateCoordinator>(() =>
                                                           new LoggingUpdateCoordinator(
                                                               Container.Resolved <ConfigurationChangeEventSource>(),
                                                               Container.Resolved <ILoggingInstrumentationProvider>()))
     {
         Lifetime = TypeRegistrationLifetime.Singleton,
         IsDefault = true
     });
 }
コード例 #2
0
 private static TypeRegistration CreateTraceManagerRegistration()
 {
     return
         (new TypeRegistration <TraceManager>(() =>
                                              new TraceManager(
                                                  Container.Resolved <LogWriter>(),
                                                  Container.Resolved <ITracerInstrumentationProvider>()))
     {
         Lifetime = TypeRegistrationLifetime.Transient,
         IsDefault = true
     });
 }
コード例 #3
0
 private TypeRegistration CreateLogWriterRegistration()
 {
     return
         (new TypeRegistration <LogWriter>(() =>
                                           new LogWriterImpl(
                                               Container.Resolved <LogWriterStructureHolder>(),
                                               Container.Resolved <ILoggingInstrumentationProvider>(),
                                               Container.Resolved <ILoggingUpdateCoordinator>()))
     {
         Lifetime = TypeRegistrationLifetime.Singleton,
         IsDefault = true,
         IsPublicName = true
     });
 }
コード例 #4
0
        /// <summary>
        /// Returns the <see cref="TypeRegistration"/> container configuration model to register <see cref="ExceptionPolicyEntry"/> items with the container.
        /// </summary>
        /// <param name="namePrefix"></param>
        /// <returns>A <see cref="TypeRegistration"/></returns>
        public TypeRegistration GetRegistration(string namePrefix)
        {
            string registrationName = BuildChildName(namePrefix, Name);

            return(new TypeRegistration <ExceptionPolicyEntry>(
                       () =>
                       new ExceptionPolicyEntry(
                           Type,
                           PostHandlingAction,
                           Container.ResolvedEnumerable <IExceptionHandler>(from hd in ExceptionHandlers select BuildChildName(registrationName, hd.Name)),
                           Container.Resolved <IExceptionHandlingInstrumentationProvider>(namePrefix)))
            {
                Name = registrationName,
                Lifetime = TypeRegistrationLifetime.Transient
            });
        }
コード例 #5
0
 private TypeRegistration CreateLogWriterStructureHolderRegistration()
 {
     return
         (new TypeRegistration <LogWriterStructureHolder>(() =>
                                                          new LogWriterStructureHolder(
                                                              Container.ResolvedEnumerable <ILogFilter>(LogFilters.Select(lfd => lfd.Name)),
                                                              TraceSources.Select(tsd => tsd.Name).ToArray(),
                                                              Container.ResolvedEnumerable <LogSource>(TraceSources.Select(tsd => tsd.Name)),
                                                              Container.Resolved <LogSource>(AllTraceSourceKey),
                                                              Container.Resolved <LogSource>(NoMatchesTraceSourceKey),
                                                              Container.Resolved <LogSource>(ErrorsTraceSourceKey),
                                                              DefaultCategory,
                                                              TracingEnabled,
                                                              LogWarningWhenNoCategoriesMatch,
                                                              RevertImpersonation))
     {
         Lifetime = TypeRegistrationLifetime.Transient,
         IsDefault = true
     });
 }
        public void Visit_ResolvedEnumerableValue()
        {
            // Set up EntLib.
            var itemNames = new string[]
            {
                "first",
                "second",
                "third"
            };
            var registration      = new TypeRegistration <RegisteredServiceConsumer>(() => new RegisteredServiceConsumer(EntLibContainer.ResolvedEnumerable <ISampleService>(itemNames)));
            var registrationParam = registration.ConstructorParameters.First();

            Assert.IsAssignableFrom <ContainerResolvedEnumerableParameter>(registrationParam);

            // Visit the parameter to get the Autofac equivalent.
            var ctorParam = this.GetCtorParam <RegisteredServiceConsumer>(typeof(IEnumerable <ISampleService>));
            var visitor   = new AutofacParameterBuilderVisitor(ctorParam);

            visitor.Visit(registrationParam);
            var result = visitor.AutofacParameter;

            Assert.NotNull(result);

            // Verify the converted parameter resolves correctly.
            var builder = new ContainerBuilder();

            builder.RegisterType <RegisteredServiceConsumer>().UsingConstructor(typeof(IEnumerable <ISampleService>)).WithParameter(result);
            var first = new SampleServiceImpl();

            builder.RegisterInstance(first).Named <ISampleService>("first");
            var second = new SampleServiceImpl();

            builder.RegisterInstance(second).Named <ISampleService>("second");
            var third = new SampleServiceImpl();

            builder.RegisterInstance(third).Named <ISampleService>("third");
            var container = builder.Build();
            var resolved  = container.Resolve <RegisteredServiceConsumer>();

            Assert.IsAssignableFrom <IEnumerable <ISampleService> >(resolved.CtorParameter);
            var services = ((IEnumerable <ISampleService>)resolved.CtorParameter).ToArray();

            Assert.Same(first, services[0]);
            Assert.Same(second, services[1]);
            Assert.Same(third, services[2]);
        }
        public void Visit_ResolvedTypedNamedValue()
        {
            // Set up EntLib.
            var registration      = new TypeRegistration <RegisteredServiceConsumer>(() => new RegisteredServiceConsumer(EntLibContainer.Resolved <ISampleService>("named")));
            var registrationParam = registration.ConstructorParameters.First();

            Assert.IsType <ContainerResolvedParameter>(registrationParam);

            // Visit the parameter to get the Autofac equivalent.
            var ctorParam = this.GetCtorParam <RegisteredServiceConsumer>(typeof(ISampleService));
            var visitor   = new AutofacParameterBuilderVisitor(ctorParam);

            visitor.Visit(registrationParam);
            var result = visitor.AutofacParameter;

            Assert.NotNull(result);

            // Verify the converted parameter resolves correctly.
            var builder = new ContainerBuilder();

            builder.RegisterType <RegisteredServiceConsumer>().UsingConstructor(typeof(ISampleService)).WithParameter(result);
            var named = new SampleServiceImpl();

            builder.RegisterInstance(named).Named <ISampleService>("named");
            var notNamed = new SampleServiceImpl();

            builder.RegisterInstance(notNamed).As <ISampleService>();
            var container = builder.Build();
            var resolved  = container.Resolve <RegisteredServiceConsumer>();

            Assert.Same(named, resolved.CtorParameter);
        }
コード例 #8
0
        public void Visit_ResolvedEnumerableValue()
        {
            // Set up EntLib.
            var itemNames = new string[]
            {
                "first",
                "second",
                "third"
            };
            var registration      = new TypeRegistration <RegisteredServiceConsumer>(() => new RegisteredServiceConsumer(EntLibContainer.ResolvedEnumerable <ISampleService>(itemNames)));
            var registrationParam = registration.ConstructorParameters.First();

            Assert.IsInstanceOf <ContainerResolvedEnumerableParameter>(registrationParam, "The parameter should have been seen by EntLib as a resolved enumerable parameter.");

            // Visit the parameter to get the Autofac equivalent.
            var ctorParam = this.GetCtorParam <RegisteredServiceConsumer>(typeof(IEnumerable <ISampleService>));
            var visitor   = new AutofacParameterBuilderVisitor(ctorParam);

            visitor.Visit(registrationParam);
            var result = visitor.AutofacParameter;

            Assert.IsNotNull(result, "After visiting the registration value, the generated parameter should be set.");

            // Verify the converted parameter resolves correctly.
            var builder = new ContainerBuilder();

            builder.RegisterType <RegisteredServiceConsumer>().UsingConstructor(typeof(IEnumerable <ISampleService>)).WithParameter(result);
            var first = new SampleServiceImpl();

            builder.RegisterInstance(first).Named <ISampleService>("first");
            var second = new SampleServiceImpl();

            builder.RegisterInstance(second).Named <ISampleService>("second");
            var third = new SampleServiceImpl();

            builder.RegisterInstance(third).Named <ISampleService>("third");
            var container = builder.Build();
            var resolved  = container.Resolve <RegisteredServiceConsumer>();

            Assert.IsInstanceOf <IEnumerable <ISampleService> >(resolved.CtorParameter, "The constructor parameter was not the right type.");
            var services = ((IEnumerable <ISampleService>)resolved.CtorParameter).ToArray();

            Assert.AreSame(first, services[0], "The first enumerable service was not resolved properly.");
            Assert.AreSame(second, services[1], "The second enumerable service was not resolved properly.");
            Assert.AreSame(third, services[2], "The third enumerable service was not resolved properly.");
        }
コード例 #9
0
        public void Visit_ResolvedTypedValue()
        {
            // Set up EntLib.
            var registration      = new TypeRegistration <RegisteredServiceConsumer>(() => new RegisteredServiceConsumer(EntLibContainer.Resolved <ISampleService>()));
            var registrationParam = registration.ConstructorParameters.First();

            Assert.IsInstanceOf <ContainerResolvedParameter>(registrationParam, "The parameter should have been seen by EntLib as a resolved parameter.");

            // Visit the parameter to get the Autofac equivalent.
            var ctorParam = this.GetCtorParam <RegisteredServiceConsumer>(typeof(ISampleService));
            var visitor   = new AutofacParameterBuilderVisitor(ctorParam);

            visitor.Visit(registrationParam);
            var result = visitor.AutofacParameter;

            Assert.IsNotNull(result, "After visiting the registration value, the generated parameter should be set.");

            // Verify the converted parameter resolves correctly.
            var builder = new ContainerBuilder();

            builder.RegisterType <RegisteredServiceConsumer>().UsingConstructor(typeof(ISampleService)).WithParameter(result);
            var service = new SampleServiceImpl();

            builder.RegisterInstance(service).As <ISampleService>();
            var container = builder.Build();
            var resolved  = container.Resolve <RegisteredServiceConsumer>();

            Assert.AreSame(service, resolved.CtorParameter, "The constructor parameter was not properly set.");
        }
コード例 #10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="configurationSource"></param>
        /// <returns></returns>
        public override IEnumerable <TypeRegistration> GetRegistrations(IConfigurationSource configurationSource)
        {
            yield return(GetInstrumentationProviderRegistration(configurationSource));

            Dictionary <string, IAuthorizationRule> authorizationRules = Rules.ToDictionary(ruleData => ruleData.Name, ruleData => (IAuthorizationRule)ruleData);

            yield return(new TypeRegistration <IAuthorizationProvider>(() => new AuthorizationRuleProvider(authorizationRules, Container.Resolved <IAuthorizationProviderInstrumentationProvider>(Name)))
            {
                Name = this.Name,
                Lifetime = TypeRegistrationLifetime.Transient
            });
        }
        public void RegisterTypeRegistration_Default_WithEnumerationParameter()
        {
            var itemNames = new string[]
            {
                "first",
                "second",
                "third"
            };
            var registration = new TypeRegistration <RegisteredServiceConsumer>(() => new RegisteredServiceConsumer(EntLibContainer.ResolvedEnumerable <ISampleService>(itemNames)));

            registration.IsDefault = true;
            registration.Lifetime  = TypeRegistrationLifetime.Transient;

            var builder = new ContainerBuilder();

            builder.RegisterTypeRegistration(registration);
            var first = new SampleServiceImpl();

            builder.RegisterInstance(first).Named <ISampleService>("first");
            var second = new SampleServiceImpl();

            builder.RegisterInstance(second).Named <ISampleService>("second");
            var third = new SampleServiceImpl();

            builder.RegisterInstance(third).Named <ISampleService>("third");
            var container = builder.Build();

            var resolved = container.Resolve <RegisteredServiceConsumer>();

            Assert.IsAssignableFrom <IEnumerable <ISampleService> >(resolved.CtorParameter);
            var services = ((IEnumerable <ISampleService>)resolved.CtorParameter).ToArray();

            Assert.Same(first, services[0]);
            Assert.Same(second, services[1]);
            Assert.Same(third, services[2]);
        }
        public void RegisterTypeRegistration_Named_WithParameters()
        {
            var registration = new TypeRegistration <RegisteredServiceConsumer>(() => new RegisteredServiceConsumer(EntLibContainer.Resolved <ISampleService>()));

            registration.Name     = "named-service";
            registration.Lifetime = TypeRegistrationLifetime.Transient;

            var dependency = new SampleServiceImpl();
            var builder    = new ContainerBuilder();

            builder.RegisterTypeRegistration(registration);
            builder.RegisterInstance(dependency).As <ISampleService>();
            var container = builder.Build();

            var instance = container.ResolveNamed <RegisteredServiceConsumer>("named-service");

            Assert.Same(dependency, instance.CtorParameter);
            var instance2 = container.ResolveNamed <RegisteredServiceConsumer>("named-service");

            Assert.NotSame(instance, instance2);
        }
        public void RegisterTypeRegistration_Default_WithEnumerationParameter()
        {
            var itemNames = new string[]
            {
                "first",
                "second",
                "third"
            };
            var registration = new TypeRegistration <RegisteredServiceConsumer>(() => new RegisteredServiceConsumer(EntLibContainer.ResolvedEnumerable <ISampleService>(itemNames)));

            registration.IsDefault = true;
            registration.Lifetime  = TypeRegistrationLifetime.Transient;

            var builder = new ContainerBuilder();

            builder.RegisterTypeRegistration(registration);
            var first = new SampleServiceImpl();

            builder.RegisterInstance(first).Named <ISampleService>("first");
            var second = new SampleServiceImpl();

            builder.RegisterInstance(second).Named <ISampleService>("second");
            var third = new SampleServiceImpl();

            builder.RegisterInstance(third).Named <ISampleService>("third");
            var container = builder.Build();

            var resolved = container.Resolve <RegisteredServiceConsumer>();

            Assert.IsInstanceOf <IEnumerable <ISampleService> >(resolved.CtorParameter, "The constructor parameter was not the right type.");
            var services = ((IEnumerable <ISampleService>)resolved.CtorParameter).ToArray();

            Assert.AreSame(first, services[0], "The first enumerable service was not resolved properly.");
            Assert.AreSame(second, services[1], "The second enumerable service was not resolved properly.");
            Assert.AreSame(third, services[2], "The third enumerable service was not resolved properly.");
        }
        public void RegisterTypeRegistration_Named_WithParameters()
        {
            var registration = new TypeRegistration <RegisteredServiceConsumer>(() => new RegisteredServiceConsumer(EntLibContainer.Resolved <ISampleService>()));

            registration.Name     = "named-service";
            registration.Lifetime = TypeRegistrationLifetime.Transient;

            var dependency = new SampleServiceImpl();
            var builder    = new ContainerBuilder();

            builder.RegisterTypeRegistration(registration);
            builder.RegisterInstance(dependency).As <ISampleService>();
            var container = builder.Build();

            var instance = container.ResolveNamed <RegisteredServiceConsumer>("named-service");;

            Assert.AreSame(dependency, instance.CtorParameter, "The service implementation parameter constructor should have been invoked with the appropriate argument.");
            var instance2 = container.ResolveNamed <RegisteredServiceConsumer>("named-service");;

            Assert.AreNotSame(instance, instance2, "The lifetime was not set on the registration.");
        }