protected override void Load(ContainerBuilder builder)
        {
            var services = new ServiceCollection();

            var configuration       = new ConfigurationService();
            var secretReaderFactory = new SecretReaderFactory(configuration);
            var secretReader        = secretReaderFactory.CreateSecretReader();
            var secretInjector      = secretReaderFactory.CreateSecretInjector(secretReader);

            builder.RegisterInstance(secretInjector)
            .AsSelf()
            .As <ISecretInjector>()
            .SingleInstance();

            configuration.SecretInjector = secretInjector;

            // Register the ILoggerFactory and configure it to use AppInsights if an instrumentation key is provided.
            var instrumentationKey = configuration.Current.AppInsightsInstrumentationKey;

            if (!string.IsNullOrEmpty(instrumentationKey))
            {
                TelemetryConfiguration.Active.InstrumentationKey = instrumentationKey;
            }

            var loggerConfiguration = LoggingSetup.CreateDefaultLoggerConfiguration(withConsoleLogger: false);
            var loggerFactory       = LoggingSetup.CreateLoggerFactory(loggerConfiguration);

            var telemetryClient = TelemetryClientWrapper.Instance;

            builder.RegisterInstance(telemetryClient)
            .AsSelf()
            .As <ITelemetryClient>()
            .SingleInstance();

            var diagnosticsService = new DiagnosticsService(telemetryClient);

            builder.RegisterInstance(diagnosticsService)
            .AsSelf()
            .As <IDiagnosticsService>()
            .SingleInstance();

            services.AddSingleton(loggerFactory);
            services.AddSingleton(typeof(ILogger <>), typeof(Logger <>));

            UrlHelperExtensions.SetConfigurationService(configuration);

            builder.RegisterInstance(configuration)
            .AsSelf()
            .As <IGalleryConfigurationService>();

            builder.Register(c => configuration.Current)
            .AsSelf()
            .AsImplementedInterfaces();

            // Force the read of this configuration, so it will be initialized on startup
            builder.Register(c => configuration.Features)
            .AsSelf()
            .As <FeatureConfiguration>();

            builder.Register(c => configuration.PackageDelete)
            .As <IPackageDeleteConfiguration>();

            var telemetryService = new TelemetryService(diagnosticsService, telemetryClient);

            builder.RegisterInstance(telemetryService)
            .AsSelf()
            .As <ITelemetryService>()
            .As <IFeatureFlagTelemetryService>()
            .SingleInstance();

            builder.RegisterType <CredentialBuilder>().As <ICredentialBuilder>().SingleInstance();
            builder.RegisterType <CredentialValidator>().As <ICredentialValidator>().SingleInstance();

            builder.RegisterInstance(LuceneCommon.GetDirectory(configuration.Current.LuceneIndexLocation))
            .As <Lucene.Net.Store.Directory>()
            .SingleInstance();

            ConfigureResilientSearch(loggerFactory, configuration, telemetryService, services);
            ConfigureSearch(builder, configuration);

            builder.RegisterType <DateTimeProvider>().AsSelf().As <IDateTimeProvider>().SingleInstance();

            builder.RegisterType <HttpContextCacheService>()
            .AsSelf()
            .As <ICacheService>()
            .InstancePerLifetimeScope();

            var galleryDbConnectionFactory = CreateDbConnectionFactory(
                diagnosticsService,
                nameof(EntitiesContext),
                configuration.Current.SqlConnectionString,
                secretInjector);

            builder.RegisterInstance(galleryDbConnectionFactory)
            .AsSelf()
            .As <ISqlConnectionFactory>()
            .SingleInstance();

            builder.Register(c => new EntitiesContext(CreateDbConnection(galleryDbConnectionFactory), configuration.Current.ReadOnlyMode))
            .AsSelf()
            .As <IEntitiesContext>()
            .As <DbContext>()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <User> >()
            .AsSelf()
            .As <IEntityRepository <User> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <ReservedNamespace> >()
            .AsSelf()
            .As <IEntityRepository <ReservedNamespace> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <PackageRegistration> >()
            .AsSelf()
            .As <IEntityRepository <PackageRegistration> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <Package> >()
            .AsSelf()
            .As <IEntityRepository <Package> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <PackageDependency> >()
            .AsSelf()
            .As <IEntityRepository <PackageDependency> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <PackageDelete> >()
            .AsSelf()
            .As <IEntityRepository <PackageDelete> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <Certificate> >()
            .AsSelf()
            .As <IEntityRepository <Certificate> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <AccountDelete> >()
            .AsSelf()
            .As <IEntityRepository <AccountDelete> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <Credential> >()
            .AsSelf()
            .As <IEntityRepository <Credential> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <Scope> >()
            .AsSelf()
            .As <IEntityRepository <Scope> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <PackageOwnerRequest> >()
            .AsSelf()
            .As <IEntityRepository <PackageOwnerRequest> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <Organization> >()
            .AsSelf()
            .As <IEntityRepository <Organization> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <SymbolPackage> >()
            .AsSelf()
            .As <IEntityRepository <SymbolPackage> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <PackageDeprecation> >()
            .AsSelf()
            .As <IEntityRepository <PackageDeprecation> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <Cve> >()
            .AsSelf()
            .As <IEntityRepository <Cve> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <Cwe> >()
            .AsSelf()
            .As <IEntityRepository <Cwe> >()
            .InstancePerLifetimeScope();

            var supportDbConnectionFactory = CreateDbConnectionFactory(
                diagnosticsService,
                nameof(SupportRequestDbContext),
                configuration.Current.SqlConnectionStringSupportRequest,
                secretInjector);

            builder.Register(c => new SupportRequestDbContext(CreateDbConnection(supportDbConnectionFactory)))
            .AsSelf()
            .As <ISupportRequestDbContext>()
            .InstancePerLifetimeScope();

            builder.RegisterType <SupportRequestService>()
            .AsSelf()
            .As <ISupportRequestService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <UserService>()
            .AsSelf()
            .As <IUserService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <PackageService>()
            .AsSelf()
            .As <IPackageService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <PackageDeleteService>()
            .AsSelf()
            .As <IPackageDeleteService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <DeleteAccountService>()
            .AsSelf()
            .As <IDeleteAccountService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <PackageOwnerRequestService>()
            .AsSelf()
            .As <IPackageOwnerRequestService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <FormsAuthenticationService>()
            .As <IFormsAuthenticationService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <CookieTempDataProvider>()
            .As <ITempDataProvider>()
            .InstancePerLifetimeScope();

            builder.RegisterType <StatusService>()
            .AsSelf()
            .As <IStatusService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <SecurityPolicyService>()
            .AsSelf()
            .As <ISecurityPolicyService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <ReservedNamespaceService>()
            .AsSelf()
            .As <IReservedNamespaceService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <SymbolPackageService>()
            .AsSelf()
            .As <ISymbolPackageService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <PackageUploadService>()
            .AsSelf()
            .As <IPackageUploadService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <SymbolPackageUploadService>()
            .AsSelf()
            .As <ISymbolPackageUploadService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <PackageOwnershipManagementService>()
            .AsSelf()
            .As <IPackageOwnershipManagementService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <ValidationService>()
            .AsSelf()
            .As <IValidationService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <ReadMeService>()
            .AsSelf()
            .As <IReadMeService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <ApiScopeEvaluator>()
            .AsSelf()
            .As <IApiScopeEvaluator>()
            .InstancePerLifetimeScope();

            builder.RegisterType <ContentObjectService>()
            .AsSelf()
            .As <IContentObjectService>()
            .SingleInstance();

            builder.RegisterType <CertificateValidator>()
            .AsSelf()
            .As <ICertificateValidator>()
            .SingleInstance();

            builder.RegisterType <CertificateService>()
            .AsSelf()
            .As <ICertificateService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <TyposquattingService>()
            .AsSelf()
            .As <ITyposquattingService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <TyposquattingCheckListCacheService>()
            .AsSelf()
            .As <ITyposquattingCheckListCacheService>()
            .SingleInstance();

            builder.Register <ServiceDiscoveryClient>(c =>
                                                      new ServiceDiscoveryClient(c.Resolve <IAppConfiguration>().ServiceDiscoveryUri))
            .As <IServiceDiscoveryClient>();

            builder.RegisterType <LicenseExpressionSplitter>()
            .As <ILicenseExpressionSplitter>()
            .InstancePerLifetimeScope();

            builder.RegisterType <LicenseExpressionParser>()
            .As <ILicenseExpressionParser>()
            .InstancePerLifetimeScope();

            builder.RegisterType <LicenseExpressionSegmentator>()
            .As <ILicenseExpressionSegmentator>()
            .InstancePerLifetimeScope();

            builder.RegisterType <PackageDeprecationService>()
            .As <IPackageDeprecationService>()
            .InstancePerLifetimeScope();

            RegisterFeatureFlagsService(builder, configuration);
            RegisterMessagingService(builder, configuration);

            builder.Register(c => HttpContext.Current.User)
            .AsSelf()
            .As <IPrincipal>()
            .InstancePerLifetimeScope();

            IAuditingService defaultAuditingService = null;

            switch (configuration.Current.StorageType)
            {
            case StorageType.FileSystem:
            case StorageType.NotSpecified:
                ConfigureForLocalFileSystem(builder, configuration);
                defaultAuditingService = GetAuditingServiceForLocalFileSystem(configuration);
                break;

            case StorageType.AzureStorage:
                ConfigureForAzureStorage(builder, configuration, telemetryClient);
                defaultAuditingService = GetAuditingServiceForAzureStorage(builder, configuration);
                break;
            }

            RegisterAsynchronousValidation(builder, diagnosticsService, configuration, secretInjector);

            RegisterAuditingServices(builder, defaultAuditingService);

            RegisterCookieComplianceService(builder, configuration, diagnosticsService);

            builder.RegisterType <MicrosoftTeamSubscription>()
            .AsSelf()
            .InstancePerLifetimeScope();

            if (configuration.Current.Environment == GalleryConstants.DevelopmentEnvironment)
            {
                builder.RegisterType <AllowLocalHttpRedirectPolicy>()
                .As <ISourceDestinationRedirectPolicy>()
                .InstancePerLifetimeScope();
            }
            else
            {
                builder.RegisterType <NoLessSecureDestinationRedirectPolicy>()
                .As <ISourceDestinationRedirectPolicy>()
                .InstancePerLifetimeScope();
            }

            ConfigureAutocomplete(builder, configuration);
            builder.Populate(services);
        }
Beispiel #2
0
        protected override void Load(ContainerBuilder builder)
        {
            var telemetryClient = TelemetryClientWrapper.Instance;

            builder.RegisterInstance(telemetryClient)
            .AsSelf()
            .As <ITelemetryClient>()
            .SingleInstance();

            var diagnosticsService = new DiagnosticsService(telemetryClient);

            builder.RegisterInstance(diagnosticsService)
            .AsSelf()
            .As <IDiagnosticsService>()
            .SingleInstance();

            var configuration       = new ConfigurationService();
            var secretReaderFactory = new SecretReaderFactory(configuration, diagnosticsService);
            var secretReader        = secretReaderFactory.CreateSecretReader();
            var secretInjector      = secretReaderFactory.CreateSecretInjector(secretReader);

            builder.RegisterInstance(secretInjector)
            .AsSelf()
            .As <ISecretInjector>()
            .SingleInstance();

            configuration.SecretInjector = secretInjector;

            UrlExtensions.SetConfigurationService(configuration);

            builder.RegisterInstance(configuration)
            .AsSelf()
            .As <IGalleryConfigurationService>();

            builder.Register(c => configuration.Current)
            .AsSelf()
            .As <IAppConfiguration>();

            // Force the read of this configuration, so it will be initialized on startup
            builder.Register(c => configuration.Features)
            .AsSelf()
            .As <FeatureConfiguration>();

            builder.Register(c => configuration.PackageDelete)
            .As <IPackageDeleteConfiguration>();

            builder.RegisterType <TelemetryService>().As <ITelemetryService>().SingleInstance();
            builder.RegisterType <CredentialBuilder>().As <ICredentialBuilder>().SingleInstance();
            builder.RegisterType <CredentialValidator>().As <ICredentialValidator>().SingleInstance();

            builder.RegisterInstance(LuceneCommon.GetDirectory(configuration.Current.LuceneIndexLocation))
            .As <Lucene.Net.Store.Directory>()
            .SingleInstance();

            ConfigureSearch(builder, configuration);

            builder.RegisterType <DateTimeProvider>().AsSelf().As <IDateTimeProvider>().SingleInstance();

            builder.RegisterType <HttpContextCacheService>()
            .AsSelf()
            .As <ICacheService>()
            .InstancePerLifetimeScope();

            var galleryDbConnectionFactory = new AzureSqlConnectionFactory(configuration.Current.SqlConnectionString, secretInjector);

            builder.RegisterInstance(galleryDbConnectionFactory)
            .AsSelf()
            .As <ISqlConnectionFactory>()
            .SingleInstance();

            builder.Register(c => new EntitiesContext(CreateDbConnection(galleryDbConnectionFactory), configuration.Current.ReadOnlyMode))
            .AsSelf()
            .As <IEntitiesContext>()
            .As <DbContext>()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <User> >()
            .AsSelf()
            .As <IEntityRepository <User> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <ReservedNamespace> >()
            .AsSelf()
            .As <IEntityRepository <ReservedNamespace> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <CuratedFeed> >()
            .AsSelf()
            .As <IEntityRepository <CuratedFeed> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <CuratedPackage> >()
            .AsSelf()
            .As <IEntityRepository <CuratedPackage> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <PackageRegistration> >()
            .AsSelf()
            .As <IEntityRepository <PackageRegistration> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <Package> >()
            .AsSelf()
            .As <IEntityRepository <Package> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <PackageDependency> >()
            .AsSelf()
            .As <IEntityRepository <PackageDependency> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <PackageDelete> >()
            .AsSelf()
            .As <IEntityRepository <PackageDelete> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <Certificate> >()
            .AsSelf()
            .As <IEntityRepository <Certificate> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <AccountDelete> >()
            .AsSelf()
            .As <IEntityRepository <AccountDelete> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <Credential> >()
            .AsSelf()
            .As <IEntityRepository <Credential> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <Scope> >()
            .AsSelf()
            .As <IEntityRepository <Scope> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <PackageOwnerRequest> >()
            .AsSelf()
            .As <IEntityRepository <PackageOwnerRequest> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <EntityRepository <Organization> >()
            .AsSelf()
            .As <IEntityRepository <Organization> >()
            .InstancePerLifetimeScope();

            builder.RegisterType <CuratedFeedService>()
            .AsSelf()
            .As <ICuratedFeedService>()
            .InstancePerLifetimeScope();

            var supportDbConnectionFactory = new AzureSqlConnectionFactory(configuration.Current.SqlConnectionStringSupportRequest, secretInjector);

            builder.Register(c => new SupportRequestDbContext(CreateDbConnection(supportDbConnectionFactory)))
            .AsSelf()
            .As <ISupportRequestDbContext>()
            .InstancePerLifetimeScope();

            builder.RegisterType <SupportRequestService>()
            .AsSelf()
            .As <ISupportRequestService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <UserService>()
            .AsSelf()
            .As <IUserService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <PackageService>()
            .AsSelf()
            .As <IPackageService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <PackageDeleteService>()
            .AsSelf()
            .As <IPackageDeleteService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <DeleteAccountService>()
            .AsSelf()
            .As <IDeleteAccountService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <PackageOwnerRequestService>()
            .AsSelf()
            .As <IPackageOwnerRequestService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <FormsAuthenticationService>()
            .As <IFormsAuthenticationService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <CookieTempDataProvider>()
            .As <ITempDataProvider>()
            .InstancePerLifetimeScope();

            builder.RegisterType <StatusService>()
            .AsSelf()
            .As <IStatusService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <SecurityPolicyService>()
            .AsSelf()
            .As <ISecurityPolicyService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <ReservedNamespaceService>()
            .AsSelf()
            .As <IReservedNamespaceService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <PackageUploadService>()
            .AsSelf()
            .As <IPackageUploadService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <PackageOwnershipManagementService>()
            .AsSelf()
            .As <IPackageOwnershipManagementService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <ValidationService>()
            .AsSelf()
            .As <IValidationService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <ReadMeService>()
            .AsSelf()
            .As <IReadMeService>()
            .InstancePerLifetimeScope();

            builder.RegisterType <ApiScopeEvaluator>()
            .AsSelf()
            .As <IApiScopeEvaluator>()
            .InstancePerLifetimeScope();

            builder.RegisterType <ContentObjectService>()
            .AsSelf()
            .As <IContentObjectService>()
            .SingleInstance();

            builder.RegisterType <CertificateValidator>()
            .AsSelf()
            .As <ICertificateValidator>()
            .SingleInstance();

            builder.RegisterType <CertificateService>()
            .AsSelf()
            .As <ICertificateService>()
            .InstancePerLifetimeScope();

            Func <MailSender> mailSenderFactory = () =>
            {
                var settings = configuration;
                if (settings.Current.SmtpUri != null && settings.Current.SmtpUri.IsAbsoluteUri)
                {
                    var smtpUri = new SmtpUri(settings.Current.SmtpUri);

                    var mailSenderConfiguration = new MailSenderConfiguration
                    {
                        DeliveryMethod = SmtpDeliveryMethod.Network,
                        Host           = smtpUri.Host,
                        Port           = smtpUri.Port,
                        EnableSsl      = smtpUri.Secure
                    };

                    if (!string.IsNullOrWhiteSpace(smtpUri.UserName))
                    {
                        mailSenderConfiguration.UseDefaultCredentials = false;
                        mailSenderConfiguration.Credentials           = new NetworkCredential(
                            smtpUri.UserName,
                            smtpUri.Password);
                    }

                    return(new MailSender(mailSenderConfiguration));
                }
                else
                {
                    var mailSenderConfiguration = new MailSenderConfiguration
                    {
                        DeliveryMethod          = SmtpDeliveryMethod.SpecifiedPickupDirectory,
                        PickupDirectoryLocation = HostingEnvironment.MapPath("~/App_Data/Mail")
                    };

                    return(new MailSender(mailSenderConfiguration));
                }
            };

            builder.Register(c => mailSenderFactory())
            .AsSelf()
            .As <IMailSender>()
            .InstancePerLifetimeScope();

            builder.RegisterType <MessageService>()
            .AsSelf()
            .As <IMessageService>()
            .InstancePerLifetimeScope();

            builder.Register(c => HttpContext.Current.User)
            .AsSelf()
            .As <IPrincipal>()
            .InstancePerLifetimeScope();

            IAuditingService defaultAuditingService = null;

            switch (configuration.Current.StorageType)
            {
            case StorageType.FileSystem:
            case StorageType.NotSpecified:
                ConfigureForLocalFileSystem(builder, configuration);
                defaultAuditingService = GetAuditingServiceForLocalFileSystem(configuration);
                break;

            case StorageType.AzureStorage:
                ConfigureForAzureStorage(builder, configuration, telemetryClient);
                defaultAuditingService = GetAuditingServiceForAzureStorage(builder, configuration);
                break;
            }

            RegisterAsynchronousValidation(builder, configuration, secretInjector);

            RegisterAuditingServices(builder, defaultAuditingService);

            RegisterCookieComplianceService(builder, configuration, diagnosticsService);

            // todo: bind all package curators by convention
            builder.RegisterType <WebMatrixPackageCurator>()
            .AsSelf()
            .As <IAutomaticPackageCurator>()
            .InstancePerLifetimeScope();

            builder.RegisterType <Windows8PackageCurator>()
            .AsSelf()
            .As <IAutomaticPackageCurator>()
            .InstancePerLifetimeScope();

            // todo: bind all commands by convention
            builder.RegisterType <AutomaticallyCuratePackageCommand>()
            .AsSelf()
            .As <IAutomaticallyCuratePackageCommand>()
            .InstancePerLifetimeScope();

            if (configuration.Current.Environment == Constants.DevelopmentEnvironment)
            {
                builder.RegisterType <AllowLocalHttpRedirectPolicy>()
                .As <ISourceDestinationRedirectPolicy>()
                .InstancePerLifetimeScope();
            }
            else
            {
                builder.RegisterType <NoLessSecureDestinationRedirectPolicy>()
                .As <ISourceDestinationRedirectPolicy>()
                .InstancePerLifetimeScope();
            }

            ConfigureAutocomplete(builder, configuration);
        }