public AwsFileRepository(VcapAwsS3Bucket vcapAwsS3Bucket)
 {
     this.vcapAwsS3Bucket = vcapAwsS3Bucket;
 }
        // ConfigureContainer is where you can register things directly
        // with Autofac. This runs after ConfigureServices so the things
        // here will override registrations made in ConfigureServices.
        // Don't build the container; that gets done for you. If you
        // need a reference to the container, you need to use the
        // "Without ConfigureContainer" mechanism shown later.
        public IContainer BuildContainerIoC(IServiceCollection services)
        {
            var builder = new ContainerBuilder();

            // Note that Populate is basically a foreach to add things
            // into Autofac that are in the collection. If you register
            // things in Autofac BEFORE Populate then the stuff in the
            // ServiceCollection can override those things; if you register
            // AFTER Populate those registrations can override things
            // in the ServiceCollection. Mix and match as needed.
            builder.Populate(services);

            //Register the configuration
            builder.RegisterInstance(Config.Configuration).SingleInstance();

            builder.Register(c => new SqlRepository(new GpgDatabaseContext(Global.DatabaseConnectionString, true)))
            .As <IDataRepository>()
            .InstancePerLifetimeScope();

            builder.RegisterType <CompaniesHouseAPI>()
            .As <ICompaniesHouseAPI>()
            .SingleInstance()
            .WithParameter(
                (p, ctx) => p.ParameterType == typeof(HttpClient),
                (p, ctx) => ctx.Resolve <IHttpClientFactory>().CreateClient(nameof(ICompaniesHouseAPI)));

            if (!Config.IsLocal())
            {
                VcapAwsS3Bucket fileStorageBucketConfiguration = Global.VcapServices.AwsS3Bucket.First(b => b.Name.EndsWith("-filestorage"));

                builder.Register(c => new AwsFileRepository(fileStorageBucketConfiguration))
                .As <IFileRepository>()
                .SingleInstance();
            }
            else
            {
                string localStorageRoot = @"..\..\..\..\Temp\";
                builder.Register(c => new SystemFileRepository(localStorageRoot)).As <IFileRepository>().SingleInstance();
            }

            // BL Services
            builder.RegisterType <UserRepository>().As <IUserRepository>().InstancePerLifetimeScope();
            builder.RegisterType <RegistrationRepository>().As <RegistrationRepository>().InstancePerLifetimeScope();
            builder.RegisterType <OrganisationService>().As <OrganisationService>().InstancePerLifetimeScope();
            builder.RegisterType <ReturnService>().As <ReturnService>().InstancePerLifetimeScope();
            builder.RegisterType <DraftReturnService>().As <DraftReturnService>().InstancePerLifetimeScope();

            builder.RegisterType <ScopeBusinessLogic>().As <IScopeBusinessLogic>().InstancePerLifetimeScope();
            builder.RegisterType <SubmissionBusinessLogic>().As <ISubmissionBusinessLogic>().InstancePerLifetimeScope();
            builder.RegisterType <OrganisationBusinessLogic>().As <IOrganisationBusinessLogic>().InstancePerLifetimeScope();

            builder.RegisterType <UpdateFromCompaniesHouseService>().As <UpdateFromCompaniesHouseService>().InstancePerLifetimeScope();

            // register web ui services
            builder.RegisterType <DraftFileBusinessLogic>().As <IDraftFileBusinessLogic>().InstancePerLifetimeScope();

            builder.RegisterType <SubmissionService>().As <ISubmissionService>().InstancePerLifetimeScope();
            builder.RegisterType <ViewingService>().As <IViewingService>().InstancePerLifetimeScope();
            builder.RegisterType <ViewingSearchService>().As <ViewingSearchService>().InstancePerLifetimeScope();
            builder.RegisterType <SearchViewService>().As <ISearchViewService>().InstancePerLifetimeScope();
            builder.RegisterType <CompareViewService>().As <ICompareViewService>().InstancePerLifetimeScope();
            builder.RegisterType <AdminSearchService>().As <AdminSearchService>().InstancePerLifetimeScope();
            builder.RegisterType <AutoCompleteSearchService>().As <AutoCompleteSearchService>().InstancePerLifetimeScope();
            builder.RegisterType <AddOrganisationSearchService>().As <AddOrganisationSearchService>().InstancePerLifetimeScope();
            builder.RegisterType <AuditLogger>().As <AuditLogger>().InstancePerLifetimeScope();

            //Register some singletons
            builder.RegisterType <InternalObfuscator>().As <IObfuscator>().SingleInstance();
            builder.RegisterType <EncryptionHandler>().As <IEncryptionHandler>().SingleInstance();
            builder.RegisterType <PinInThePostService>().As <PinInThePostService>().SingleInstance();
            builder.RegisterType <GovNotifyAPI>().As <IGovNotifyAPI>().InstancePerLifetimeScope();
            builder.RegisterType <EmailSendingService>().As <EmailSendingService>().InstancePerLifetimeScope();
            builder.RegisterType <BackgroundJobsApi>().As <IBackgroundJobsApi>().InstancePerLifetimeScope();


            //Register HttpCache and HttpSession
            builder.RegisterType <HttpSession>().As <IHttpSession>().InstancePerLifetimeScope();
            builder.RegisterType <HttpCache>().As <IHttpCache>().SingleInstance();

            // Register Action helpers
            builder.RegisterType <ActionContextAccessor>().As <IActionContextAccessor>().SingleInstance();
            builder.Register(
                x =>
            {
                ActionContext actionContext = x.Resolve <IActionContextAccessor>().ActionContext;
                var factory = x.Resolve <IUrlHelperFactory>();
                return(factory.GetUrlHelper(actionContext));
            });

            //Register WebTracker
            builder.RegisterType <GoogleAnalyticsTracker>()
            .As <IWebTracker>()
            .SingleInstance()
            .WithParameter(
                (p, ctx) => p.ParameterType == typeof(HttpClient),
                (p, ctx) => ctx.Resolve <IHttpClientFactory>().CreateClient(nameof(IWebTracker)))
            .WithParameter("trackingId", Global.GoogleAnalyticsAccountId);

            //Register all controllers - this is required to ensure KeyFilter is resolved in constructors
            builder.RegisterAssemblyTypes(typeof(BaseController).Assembly)
            .Where(t => t.IsAssignableTo <BaseController>())
            .InstancePerLifetimeScope()
            .WithAttributeFiltering();

            //TODO: Implement AutoFac modules
            //builder.RegisterModule(new AutofacModule());

            //Override any test services
            ConfigureTestContainer?.Invoke(builder);

            IContainer container = builder.Build();

            HangfireConfigurationHelper.ConfigureIOC(container);

            return(container);
        }