public static IBus ReGisterReBusBus(
            this IContainer container, 
            AppSettings settings, 
            string applicationName)
        {
            string inputQueue = applicationName + ".input";
            string errorQueue = applicationName + ".error";

            var adapter = new AutofacContainerAdapter(container);
            var bus = Configure.With(adapter)
                .Logging(l => l.Log4Net())
                .Transport(t => t.UseSqlServer(settings.Env.DcsDbConn,
                    "BusTransport",
                    inputQueue,
                    errorQueue)
                    .EnsureTableIsCreated())
                .Serialization(s => s.UseJsonSerializer())
                .MessageOwnership(m => m.Use(new SingleDestinationMessageOwnership(inputQueue)))
                .Subscriptions(s =>
                    s.StoreInSqlServer(settings.Env.DcsDbConn, "BusSubscription")
                        .EnsureTableIsCreated())
                .Behavior(b => b.SetMaxRetriesFor<Exception>(1))
                .CreateBus()
                .Start();

            return bus;
        }
        public void MefCanResolveLazyTypesRegisteredInAutofacTest()
        {
            // Setup
            var builder = new ContainerBuilder();

            builder.RegisterType <AutofacOnlyComponent1>().As <IAutofacOnlyComponent>();
            builder.RegisterType <AutofacOnlyComponent2>().Named <IAutofacOnlyComponent>("a");
            var autofacContainer = builder.Build();
            var adapter          = new AutofacContainerAdapter(autofacContainer);
            var provider         = new ContainerExportProvider(adapter);
            var assemblyCatalog  = new AssemblyCatalog(Assembly.GetExecutingAssembly());
            var container        = new CompositionContainer(assemblyCatalog, provider);

            AutofacOnlyComponent1.InstanceCount = 0;

            var lazyAutofacComponent = container.GetExports <IAutofacOnlyComponent>().ToList();

            Assert.That(lazyAutofacComponent, Is.Not.Null);
            Assert.That(AutofacOnlyComponent1.InstanceCount, Is.EqualTo(0));

            Assert.That(lazyAutofacComponent, Is.Not.Null);
            Assert.That(lazyAutofacComponent[0].Value, Is.Not.Null);
            Assert.That(lazyAutofacComponent[1].Value, Is.Not.Null);
            Assert.That(AutofacOnlyComponent1.InstanceCount, Is.EqualTo(1));
        }
 public void PreRegisteredContainerAdapter()
 {
     ContainerAdapterAccessor.Clear();
     var adapter = new AutofacContainerAdapter();
     adapter.Register<IDependantClass, DependantClass2>();
     IContainerManager containerManager = new ContainerManager(adapter);
     Assert.IsNotNull(containerManager.Resolve<IDependantClass>());
     var containerManager2 = new ContainerManager();
     Assert.IsNotNull(containerManager2.Resolve<IDependantClass>());
 }
Beispiel #4
0
        public void PreRegisteredContainerAdapter()
        {
            ContainerAdapterAccessor.Clear();
            var adapter = new AutofacContainerAdapter();

            adapter.Register <IDependantClass, DependantClass2>();
            IContainerManager containerManager = new ContainerManager(adapter);

            Assert.IsNotNull(containerManager.Resolve <IDependantClass>());
            var containerManager2 = new ContainerManager();

            Assert.IsNotNull(containerManager2.Resolve <IDependantClass>());
        }
        public void ExportProviderResolvesServiceRegisteredByTypeTest()
        {
            // Setup
            var builder = new ContainerBuilder();
            builder.RegisterType<AutofacOnlyComponent1>().As<IAutofacOnlyComponent>();
            var autofacContainer = builder.Build();
            var adapter = new AutofacContainerAdapter(autofacContainer);
            var provider = new ContainerExportProvider(adapter);

            var component = provider.GetExportedValue<IAutofacOnlyComponent>();
            Assert.That(component, Is.Not.Null);
            Assert.That(component.GetType(), Is.EqualTo(typeof(AutofacOnlyComponent1)));
        }
Beispiel #6
0
        public void Should_Dispose_Container()
        {
            // Arrange
            var containerMock = new Mock <IContainer>();

            containerMock.Setup(x => x.Dispose());
            var adapter = new AutofacContainerAdapter(containerMock.Object, (config, commands) => config);

            // Act
            adapter.Dispose();

            // Assert
            containerMock.Verify(x => x.Dispose(), Times.Once());
        }
        public void ExportProviderResolvesServiceRegisteredByTypeTest()
        {
            // Setup
            var builder = new ContainerBuilder();

            builder.RegisterType <AutofacOnlyComponent1>().As <IAutofacOnlyComponent>();
            var autofacContainer = builder.Build();
            var adapter          = new AutofacContainerAdapter(autofacContainer);
            var provider         = new ContainerExportProvider(adapter);

            var component = provider.GetExportedValue <IAutofacOnlyComponent>();

            Assert.That(component, Is.Not.Null);
            Assert.That(component.GetType(), Is.EqualTo(typeof(AutofacOnlyComponent1)));
        }
        public void ExportProviderResolvesServicesRegisteredByTypeTest()
        {
            // Setup
            var builder = new ContainerBuilder();
            builder.RegisterType<AutofacOnlyComponent1>().As<IAutofacOnlyComponent>();
            builder.RegisterType<AutofacOnlyComponent2>().Named<IAutofacOnlyComponent>("b");
            var autofacContainer = builder.Build();
            var adapter = new AutofacContainerAdapter(autofacContainer);
            var provider = new ContainerExportProvider(adapter);

            var components = provider.GetExports<IAutofacOnlyComponent>();
            Assert.That(components, Is.Not.Null);
            Assert.That(components.Count(), Is.EqualTo(2));

            Assert.That(components.Select(t => t.Value).OfType<AutofacOnlyComponent1>().Count(), Is.EqualTo(1));
            Assert.That(components.Select(t => t.Value).OfType<AutofacOnlyComponent2>().Count(), Is.EqualTo(1));
        }
Beispiel #9
0
        public static void ConfigureRebus(IContainer container)
        {
            var adapter = new AutofacContainerAdapter(container);

            Configure.With(adapter)
            .Logging(l => l.None())
            .Transport(t => t
                       .UseAzureServiceBus("PUT YOUR AZURE SERVICE BUS CONNECTION STRING OR NAME HERE", "my-rebus-queue")
                       .AutomaticallyRenewPeekLock())
            // .Transport(x => x.UseFileSystem(@"c:\rebus", "my-rebus-queue"))
            .Routing(r => r.TypeBased().Map <Heartbeat>("my-rebus-queue"))
            .Start();

            var timer = container.ResolveKeyed <Timer>("heartbeat");

            timer.Start();
        }
Beispiel #10
0
        public void Should_Allow_Null_Extra_Configuration()
        {
            // Arrange
            var containerMock = new Mock <IContainer>();
            var lifetimeScope = Mock.Of <ILifetimeScope>();
            Action <ContainerBuilder> configAction = null;

            containerMock.Setup(x => x.BeginLifetimeScope(It.IsAny <Action <ContainerBuilder> >())).Callback <Action <ContainerBuilder> >(action => { configAction = action; }).Returns(lifetimeScope);
            var adapter = new AutofacContainerAdapter(containerMock.Object, null);

            // Act / Assert
            Should.NotThrow(() =>
            {
                adapter.GetScoped(new List <(ContainerConfigurationOption option, string value)>());
                configAction(null);
            });
        }
Beispiel #11
0
        public void Should_Return_Nested_Container()
        {
            // Arrange
            var containerMock = new Mock <IContainer>();
            var lifetimeScope = Mock.Of <ILifetimeScope>();

            containerMock.Setup(x => x.BeginLifetimeScope(It.IsAny <Action <ContainerBuilder> >())).Returns(lifetimeScope);
            var adapter = new AutofacContainerAdapter(containerMock.Object, (config, commands) => config);

            // Act
            var nested = adapter.GetScoped(new List <(ContainerConfigurationOption option, string value)>());

            // Assert
            nested.ShouldNotBe(adapter);
            nested.ShouldBeOfType <AutofacLifetimeScopeAdapter>();
            containerMock.Verify(x => x.BeginLifetimeScope(It.IsAny <Action <ContainerBuilder> >()), Times.Once());
        }
        public void MefCanResolveTypesRegisteredInAutofacAfterTrackingExtensionIsAddedTest()
        {
            // Setup
            var builder = new ContainerBuilder();

            builder.RegisterType <AutofacOnlyComponent2>().Named <IAutofacOnlyComponent>("autofacComponent2");
            var autofacContainer = builder.Build();

            // Further setup
            var adapter         = new AutofacContainerAdapter(autofacContainer);
            var provider        = new ContainerExportProvider(adapter);
            var assemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
            var container       = new CompositionContainer(assemblyCatalog, provider);

            var component = container.GetExportedValue <IAutofacOnlyComponent>("autofacComponent2");

            Assert.That(component, Is.Not.Null);
            Assert.That(component.GetType(), Is.EqualTo(typeof(AutofacOnlyComponent2)));
        }
        public void ExportProviderResolvesServicesRegisteredByTypeTest()
        {
            // Setup
            var builder = new ContainerBuilder();

            builder.RegisterType <AutofacOnlyComponent1>().As <IAutofacOnlyComponent>();
            builder.RegisterType <AutofacOnlyComponent2>().Named <IAutofacOnlyComponent>("b");
            var autofacContainer = builder.Build();
            var adapter          = new AutofacContainerAdapter(autofacContainer);
            var provider         = new ContainerExportProvider(adapter);

            var components = provider.GetExports <IAutofacOnlyComponent>();

            Assert.That(components, Is.Not.Null);
            Assert.That(components.Count(), Is.EqualTo(2));

            Assert.That(components.Select(t => t.Value).OfType <AutofacOnlyComponent1>().Count(), Is.EqualTo(1));
            Assert.That(components.Select(t => t.Value).OfType <AutofacOnlyComponent2>().Count(), Is.EqualTo(1));
        }
        public void MefCanResolveLazyTypeRegisteredInAutofacTest()
        {
            // Setup
            var builder = new ContainerBuilder();
            builder.RegisterType<AutofacOnlyComponent1>().As<IAutofacOnlyComponent>();
            var autofacContainer = builder.Build();
            var adapter = new AutofacContainerAdapter(autofacContainer);
            var provider = new ContainerExportProvider(adapter);
            var assemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
            var container = new CompositionContainer(assemblyCatalog, provider);

            AutofacOnlyComponent1.InstanceCount = 0;

            var lazyAutofacComponent = container.GetExport<IAutofacOnlyComponent>();
            Assert.That(lazyAutofacComponent, Is.Not.Null);
            Assert.That(AutofacOnlyComponent1.InstanceCount, Is.EqualTo(0));

            Assert.That(lazyAutofacComponent.Value, Is.Not.Null);
            Assert.That(lazyAutofacComponent.Value.GetType(), Is.EqualTo(typeof(AutofacOnlyComponent1)));
            Assert.That(AutofacOnlyComponent1.InstanceCount, Is.EqualTo(1));
        }
Beispiel #15
0
        public void Should_Invoke_Extra_Configuration()
        {
            // Arrange
            var containerMock = new Mock <IContainer>();
            var lifetimeScope = Mock.Of <ILifetimeScope>();
            Action <ContainerBuilder> configAction = null;

            containerMock.Setup(x => x.BeginLifetimeScope(It.IsAny <Action <ContainerBuilder> >())).Callback <Action <ContainerBuilder> >(action => { configAction = action; }).Returns(lifetimeScope);
            bool configCalled = false;
            var  adapter      = new AutofacContainerAdapter(containerMock.Object, (config, commands) =>
            {
                configCalled = true;
                return(config);
            });

            // Act
            adapter.GetScoped(new List <(ContainerConfigurationOption option, string value)>());
            configAction(null);

            // Assert
            configCalled.ShouldBeTrue();
        }
Beispiel #16
0
        static void Main(string[] args)
        {
            var builder = new ContainerBuilder();

            builder.RegisterType <ExampleService>().As <IExampleService>();
            builder.RegisterType <TestSimpleCommandClass>().AsSelf();

            var autofacContainer = builder.Build();

            var containerAdapter = new AutofacContainerAdapter(autofacContainer, (scopeBuilder, commands) =>
            {
                scopeBuilder.RegisterInstance(new ExampleConfiguration(commands)).As <IExampleConfiguration>();
                return(scopeBuilder);
            });

            var app = new CommandLineInjectingApplication("commandlineinjector-example", containerAdapter);

            app.RequiresCommand();
            app.AddToSubsequentAllCommands(ExampleConfiguration.ConfigValueOption);

            app.Command <TestSimpleCommandClass>("simple");
            app.Execute(args);
        }
        public void SetUp()
        {
            _builder = new ContainerBuilder();

            _adapter = new AutofacContainerAdapter(_builder);
        }
        public void MefCanResolveTypesRegisteredInAutofacAfterTrackingExtensionIsAddedTest()
        {
            // Setup
            var builder = new ContainerBuilder();
            builder.RegisterType<AutofacOnlyComponent2>().Named<IAutofacOnlyComponent>("autofacComponent2");
            var autofacContainer = builder.Build();

            // Further setup
            var adapter = new AutofacContainerAdapter(autofacContainer);
            var provider = new ContainerExportProvider(adapter);
            var assemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
            var container = new CompositionContainer(assemblyCatalog, provider);

            var component = container.GetExportedValue<IAutofacOnlyComponent>("autofacComponent2");
            Assert.That(component, Is.Not.Null);
            Assert.That(component.GetType(), Is.EqualTo(typeof(AutofacOnlyComponent2)));
        }