public void GetRequiredServiceNonGeneric()
        {
            IContainerBuilder builder = new TestContainerBuilder();

            builder.AddService(ServiceLifetime.Transient, typeof(Foo));
            IServiceProvider container = builder.BuildContainer();

            Assert.NotNull(container.GetRequiredService(typeof(Foo)));
        }
        public void GetNonExistingServiceGeneric()
        {
            IContainerBuilder builder = new TestContainerBuilder();

            builder.AddService(ServiceLifetime.Transient, typeof(Foo));
            IServiceProvider container = builder.BuildContainer();

            Assert.Null(container.GetService <IFoo>());
        }
        public void GetNonExistingRequiredServiceThrows()
        {
            IContainerBuilder builder = new TestContainerBuilder();

            builder.AddService(ServiceLifetime.Transient, typeof(Foo));
            IServiceProvider container = builder.BuildContainer();

            Assert.Throws(typeof(ODataException), () => container.GetRequiredService <IFoo>());
        }
        public void AddServiceWithTServiceAndTImplementationFactory()
        {
            IContainerBuilder builder = new TestContainerBuilder();

            builder.AddService <IFoo>(ServiceLifetime.Transient, sp => new Foo());
            IServiceProvider container = builder.BuildContainer();

            Assert.NotNull(container.GetService <IFoo>());
        }
        public void AddServiceWithTServiceOnly()
        {
            IContainerBuilder builder = new TestContainerBuilder();

            builder.AddService <Foo>(ServiceLifetime.Transient);
            IServiceProvider container = builder.BuildContainer();

            Assert.NotNull(container.GetService <Foo>());
        }
        public void AddServiceWithServiceType()
        {
            IContainerBuilder builder = new TestContainerBuilder();

            builder.AddService(ServiceLifetime.Transient, typeof(Foo));
            IServiceProvider container = builder.BuildContainer();

            Assert.NotNull(container.GetService <Foo>());
        }
        public void GetServicesGeneric()
        {
            IContainerBuilder builder = new TestContainerBuilder();

            builder.AddService <IFoo, Foo>(ServiceLifetime.Transient);
            builder.AddService <IFoo, Bar>(ServiceLifetime.Transient);
            IServiceProvider container = builder.BuildContainer();

            Assert.Equal(2, container.GetServices <IFoo>().Count());
        }
Пример #8
0
        /// <summary>
        /// Gets the shared DI container
        /// </summary>
        /// <returns>Instance of the DI container</returns>
        private static IServiceProvider GetSharedContainer()
        {
            if (container == null)
            {
                var builder = new TestContainerBuilder();
                builder.AddDefaultODataServices();
                container = builder.BuildContainer();
            }

            return(container);
        }
        public void Validation_Service_Test()
        {
            var containerBuilder = new TestContainerBuilder();
            var autofacContainer = containerBuilder.BuildContainer();

            ValidationTestServiceValidator.ResetCount();
            using (var scope = autofacContainer.BeginLifetimeScope())
            {
                var service = scope.Resolve <IValidationTestService>();
                service.Method1();
            }

            Assert.AreEqual(expected: 1, actual: ValidationTestServiceValidator.GetInputParametersCallCount());
            Assert.AreEqual(expected: 1, actual: ValidationTestServiceValidator.GetResultCallCount());
        }
        public void WorkflowWriter_Service_Test()
        {
            var containerBuilder = new TestContainerBuilder();
            var autofacContainer = containerBuilder.BuildContainer();

            WorkflowWriterTestServiceWriter.ResetCount();
            using (var scope = autofacContainer.BeginLifetimeScope())
            {
                var service = scope.Resolve <IWorkflowWriterTestService>();
                service.Method1();
            }

            Assert.AreEqual(expected: 1, actual: WorkflowWriterTestServiceWriter.GetPreInvocationCallCount());
            Assert.AreEqual(expected: 1, actual: WorkflowWriterTestServiceWriter.GetPostInvocationCallCount());
        }
        public void Logging_Elevated_To_Warn_Service_Test()
        {
            var applicationConfiguration =
                new ApplicationConfiguration("appsettings.json");

            applicationConfiguration.GenerateConfiguration();

            var containerBuilder = new TestContainerBuilder(applicationConfiguration);
            var autofacContainer = containerBuilder.BuildContainer();

            using (var scope = autofacContainer.BeginLifetimeScope())
            {
                var service = scope.Resolve <ILoggingElevatedToWarn>();
                service.Method1("Input Warn Value");
            }
        }
Пример #12
0
        public void Notification_Test()
        {
            var containerBuilder = new TestContainerBuilder();
            var autofacContainer = containerBuilder.BuildContainer();

            MediatRReceiverService.ResetCount();
            NotificationService.ResetCount();

            using (var scope = autofacContainer.BeginLifetimeScope())
            {
                var service = scope.Resolve <IMediatRReceiverService>();
                service.RunNotification();
            }

            Assert.AreEqual(expected: 1, actual: NotificationService.GetCallCount());
            Assert.AreEqual(expected: 1, actual: MediatRReceiverService.GetCallCount());
        }
Пример #13
0
        public void Request_Response_Test()
        {
            var containerBuilder = new TestContainerBuilder();
            var autofacContainer = containerBuilder.BuildContainer();

            MediatRReceiverService.ResetCount();
            RequestResponseService.ResetCount();

            using (var scope = autofacContainer.BeginLifetimeScope())
            {
                var service = scope.Resolve <IMediatRReceiverService>();
                service.RunRequest();
            }

            Assert.AreEqual(expected: 1, actual: RequestResponseService.GetCallCount());
            Assert.AreEqual(expected: 1, actual: MediatRReceiverService.GetCallCount());
        }
        public void Logging_Service_Exception_Test()
        {
            var containerBuilder = new TestContainerBuilder();
            var autofacContainer = containerBuilder.BuildContainer();

            LoggingTestExceptionServiceLogger.ResetCount();
            LoggingTestExceptionService.ResetCount();

            using (var scope = autofacContainer.BeginLifetimeScope())
            {
                var service = scope.Resolve <ILoggingTestExceptionService>();
                service.Method1();
                service.Method1();
            }

            Assert.AreEqual(expected: 2, actual: LoggingTestExceptionService.GetCallCount());
            Assert.AreEqual(expected: 1, actual: LoggingTestExceptionServiceLogger.GetPreInvocationCallCount());
            Assert.AreEqual(expected: 1, actual: LoggingTestExceptionServiceLogger.GetPostInvocationCallCount());
        }
Пример #15
0
        public void Not_Executed_In_Test_Mode()
        {
            var containerBuilder = new TestContainerBuilder();
            var autofacContainer = containerBuilder.BuildContainer();

            Service.ResetCount();
            LiveService.ResetCount();

            using (var scope = autofacContainer.BeginLifetimeScope())
            {
                var service = scope.Resolve <IService>();
                service.RunService();
            }

            // Service will be called once
            Assert.AreEqual(expected: 1, actual: Service.GetCallCount());

            // Live service implementation should not be called.
            Assert.AreEqual(expected: 0, actual: LiveService.GetCallCount());
        }
Пример #16
0
        public static void InitSimpleServiceTest(TestContext testContext)
        {
            var containerBuilder = new TestContainerBuilder();

            autofacContainer = containerBuilder.BuildContainer();
        }