public void Configure(EmailOptions options) { options.EnableDailySummary = _configuration.GetValue(nameof(options.EnableDailySummary), _appOptions.Value.AppMode == AppMode.Production); options.AllowedOutboundAddresses = _configuration.GetValueList(nameof(options.AllowedOutboundAddresses)).Select(v => v.ToLowerInvariant()).ToList(); options.TestEmailAddress = _configuration.GetValue(nameof(options.TestEmailAddress), "*****@*****.**"); string emailConnectionString = _configuration.GetConnectionString("Email"); if (!String.IsNullOrEmpty(emailConnectionString)) { var uri = new SmtpUri(emailConnectionString); options.SmtpHost = uri.Host; options.SmtpPort = uri.Port; options.SmtpUser = uri.User; options.SmtpPassword = uri.Password; } options.SmtpFrom = _configuration.GetValue(nameof(options.SmtpFrom), "Exceptionless <*****@*****.**>"); options.SmtpEncryption = _configuration.GetValue(nameof(options.SmtpEncryption), GetDefaultSmtpEncryption(options.SmtpPort)); if (String.IsNullOrWhiteSpace(options.SmtpUser) != String.IsNullOrWhiteSpace(options.SmtpPassword)) { throw new ArgumentException("Must specify both the SmtpUser and the SmtpPassword, or neither."); } }
public static EmailOptions ReadFromConfiguration(IConfiguration config, AppOptions appOptions) { var options = new EmailOptions(); options.EnableDailySummary = config.GetValue(nameof(options.EnableDailySummary), appOptions.AppMode == AppMode.Production); options.AllowedOutboundAddresses = config.GetValueList(nameof(options.AllowedOutboundAddresses)).Select(v => v.ToLowerInvariant()).ToList(); options.TestEmailAddress = config.GetValue(nameof(options.TestEmailAddress), appOptions.AppMode == AppMode.Development ? "test@localhost" : ""); string emailConnectionString = config.GetConnectionString("Email"); if (!String.IsNullOrEmpty(emailConnectionString)) { var uri = new SmtpUri(emailConnectionString); options.SmtpHost = uri.Host; options.SmtpPort = uri.Port; options.SmtpUser = uri.User; options.SmtpPassword = uri.Password; } options.SmtpFrom = config.GetValue(nameof(options.SmtpFrom), appOptions.AppMode == AppMode.Development ? "Exceptionless <noreply@localhost>" : ""); options.SmtpEncryption = config.GetValue(nameof(options.SmtpEncryption), GetDefaultSmtpEncryption(options.SmtpPort)); if (String.IsNullOrWhiteSpace(options.SmtpUser) != String.IsNullOrWhiteSpace(options.SmtpPassword)) { throw new ArgumentException("Must specify both the SmtpUser and the SmtpPassword, or neither."); } return(options); }
public override void Init(IDictionary <string, string> jobArgsDictionary) { _whatIf = JobConfigurationManager.TryGetBoolArgument(jobArgsDictionary, JobArgumentNames.WhatIf); var databaseConnectionString = JobConfigurationManager.GetArgument(jobArgsDictionary, JobArgumentNames.GalleryDatabase); _galleryDatabase = new SqlConnectionStringBuilder(databaseConnectionString); _galleryBrand = JobConfigurationManager.GetArgument(jobArgsDictionary, MyJobArgumentNames.GalleryBrand); _galleryAccountUrl = JobConfigurationManager.GetArgument(jobArgsDictionary, MyJobArgumentNames.GalleryAccountUrl); _mailFrom = JobConfigurationManager.GetArgument(jobArgsDictionary, JobArgumentNames.MailFrom); var smtpConnectionString = JobConfigurationManager.GetArgument(jobArgsDictionary, JobArgumentNames.SmtpUri); var smtpUri = new SmtpUri(new Uri(smtpConnectionString)); _smtpClient = CreateSmtpClient(smtpUri); _warnDaysBeforeExpiration = JobConfigurationManager.TryGetIntArgument(jobArgsDictionary, MyJobArgumentNames.WarnDaysBeforeExpiration) ?? _warnDaysBeforeExpiration; _allowEmailResendAfterDays = JobConfigurationManager.TryGetIntArgument(jobArgsDictionary, MyJobArgumentNames.AllowEmailResendAfterDays) ?? _allowEmailResendAfterDays; var storageConnectionString = JobConfigurationManager.GetArgument(jobArgsDictionary, JobArgumentNames.DataStorageAccount); var storageContainerName = JobConfigurationManager.GetArgument(jobArgsDictionary, JobArgumentNames.ContainerName); var storageAccount = CloudStorageAccount.Parse(storageConnectionString); var storageFactory = new AzureStorageFactory(storageAccount, storageContainerName, LoggerFactory); _storage = storageFactory.Create(); }
private SmtpClient GetOrCreateSmtpClient() { if (_smtpClient != null) { return(_smtpClient); } var smtpUri = new SmtpUri(new Uri(_smtpUri)); _smtpClient = new SmtpClient(); _smtpClient.Host = smtpUri.Host; _smtpClient.Port = smtpUri.Port; _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; _smtpClient.EnableSsl = smtpUri.Secure; if (!string.IsNullOrEmpty(smtpUri.UserName)) { _smtpClient.UseDefaultCredentials = false; _smtpClient.Credentials = new NetworkCredential( smtpUri.UserName, smtpUri.Password); } return(_smtpClient); }
public void CanParseSmtpUri() { var uri = new SmtpUri("smtps://test%40test.com:[email protected]:587"); Assert.NotNull(uri); Assert.True(uri.IsSecure); Assert.Equal("smtp.test.com", uri.Host); Assert.Equal(587, uri.Port); Assert.Equal("*****@*****.**", uri.User); Assert.Equal("testpass", uri.Password); }
private static void RegisterSmtpEmailMessagingService(ContainerBuilder builder, ConfigurationService configuration) { 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>() .InstancePerDependency(); builder.RegisterType <BackgroundMarkdownMessageService>() .AsSelf() .As <IMessageService>() .InstancePerDependency(); }
/// <summary> /// 通过url形式的格式构造配置信息 /// </summary> /// <param name="smtpUri">smtp地址信息 /// 格式形式如: smtps://username:password@host:port. /// 如果包含特殊符号,则需要外部先进行uri编码 /// </param> public SmtpConfig(string smtpUri) { var tmpSmtpUri = new SmtpUri(new Uri(smtpUri)); this.DeliveryMethod = SmtpDeliveryMethod.Network; this.Host = tmpSmtpUri.Host; this.Port = tmpSmtpUri.Port; this.EnableSsl = tmpSmtpUri.Secure; if (!string.IsNullOrWhiteSpace(tmpSmtpUri.UserName)) { this.UseDefaultCredentials = false; this.Credentials = new NetworkCredential(tmpSmtpUri.UserName, tmpSmtpUri.Password); } }
public override bool Init(IDictionary <string, string> jobArgsDictionary) { try { var instrumentationKey = JobConfigurationManager.TryGetArgument(jobArgsDictionary, JobArgumentNames.InstrumentationKey); ApplicationInsights.Initialize(instrumentationKey); var loggerConfiguration = LoggingSetup.CreateDefaultLoggerConfiguration(ConsoleLogOnly); var loggerFactory = LoggingSetup.CreateLoggerFactory(loggerConfiguration); _logger = loggerFactory.CreateLogger <Job>(); _whatIf = JobConfigurationManager.TryGetBoolArgument(jobArgsDictionary, JobArgumentNames.WhatIf); var databaseConnectionString = JobConfigurationManager.GetArgument(jobArgsDictionary, JobArgumentNames.GalleryDatabase); _galleryDatabase = new SqlConnectionStringBuilder(databaseConnectionString); _galleryBrand = JobConfigurationManager.GetArgument(jobArgsDictionary, MyJobArgumentNames.GalleryBrand); _galleryAccountUrl = JobConfigurationManager.GetArgument(jobArgsDictionary, MyJobArgumentNames.GalleryAccountUrl); _mailFrom = JobConfigurationManager.GetArgument(jobArgsDictionary, JobArgumentNames.MailFrom); var smtpConnectionString = JobConfigurationManager.GetArgument(jobArgsDictionary, JobArgumentNames.SmtpUri); var smtpUri = new SmtpUri(new Uri(smtpConnectionString)); _smtpClient = CreateSmtpClient(smtpUri); _warnDaysBeforeExpiration = JobConfigurationManager.TryGetIntArgument(jobArgsDictionary, MyJobArgumentNames.WarnDaysBeforeExpiration) ?? _warnDaysBeforeExpiration; _allowEmailResendAfterDays = JobConfigurationManager.TryGetIntArgument(jobArgsDictionary, MyJobArgumentNames.AllowEmailResendAfterDays) ?? _allowEmailResendAfterDays; var storageConnectionString = JobConfigurationManager.GetArgument(jobArgsDictionary, JobArgumentNames.DataStorageAccount); var storageContainerName = JobConfigurationManager.GetArgument(jobArgsDictionary, JobArgumentNames.ContainerName); var csa = CloudStorageAccount.Parse(storageConnectionString); var storageFactory = new AzureStorageFactory(csa, storageContainerName, loggerFactory); _storage = storageFactory.Create(); } catch (Exception exception) { _logger.LogCritical(LogEvents.JobInitFailed, exception, "Failed to initialize job!"); return(false); } return(true); }
private SmtpClient CreateSmtpClient(SmtpUri smtpUri) { var smtpClient = new SmtpClient(smtpUri.Host, smtpUri.Port) { EnableSsl = smtpUri.Secure }; if (!string.IsNullOrWhiteSpace(smtpUri.UserName)) { smtpClient.UseDefaultCredentials = false; smtpClient.Credentials = new NetworkCredential( smtpUri.UserName, smtpUri.Password); } return(smtpClient); }
protected override void Load(ContainerBuilder builder) { var configuration = new ConfigurationService(); builder.RegisterInstance(configuration) .AsSelf() .As <PoliteCaptcha.IConfigurationSource>(); builder.Register(c => configuration.Current) .AsSelf() .As <IAppConfiguration>(); builder.RegisterInstance(LuceneCommon.GetDirectory(configuration.Current.LuceneIndexLocation)) .As <Lucene.Net.Store.Directory>() .SingleInstance(); ConfigureSearch(builder, configuration); if (!string.IsNullOrEmpty(configuration.Current.AzureStorageConnectionString)) { builder.RegisterInstance(new TableErrorLog(configuration.Current.AzureStorageConnectionString)) .As <ErrorLog>() .SingleInstance(); } else { builder.RegisterInstance(new SqlErrorLog(configuration.Current.SqlConnectionString)) .As <ErrorLog>() .SingleInstance(); } builder.RegisterType <HttpContextCacheService>() .AsSelf() .As <ICacheService>() .InstancePerLifetimeScope(); builder.RegisterType <ContentService>() .AsSelf() .As <IContentService>() .SingleInstance(); builder.Register(c => new EntitiesContext(configuration.Current.SqlConnectionString, readOnly: configuration.Current.ReadOnlyMode)) .AsSelf() .As <IEntitiesContext>() .As <DbContext>() .InstancePerLifetimeScope(); builder.RegisterType <EntityRepository <User> >() .AsSelf() .As <IEntityRepository <User> >() .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 <PackageStatistics> >() .AsSelf() .As <IEntityRepository <PackageStatistics> >() .InstancePerLifetimeScope(); builder.RegisterType <EntityRepository <PackageDelete> >() .AsSelf() .As <IEntityRepository <PackageDelete> >() .InstancePerLifetimeScope(); builder.RegisterType <EntityRepository <Credential> >() .AsSelf() .As <IEntityRepository <Credential> >() .InstancePerLifetimeScope(); builder.RegisterType <EntityRepository <PackageOwnerRequest> >() .AsSelf() .As <IEntityRepository <PackageOwnerRequest> >() .InstancePerLifetimeScope(); builder.RegisterType <CuratedFeedService>() .AsSelf() .As <ICuratedFeedService>() .InstancePerLifetimeScope(); builder.RegisterType <UserService>() .AsSelf() .As <IUserService>() .InstancePerLifetimeScope(); builder.RegisterType <PackageService>() .AsSelf() .As <IPackageService>() .InstancePerLifetimeScope(); builder.RegisterType <PackageDeleteService>() .AsSelf() .As <IPackageDeleteService>() .InstancePerLifetimeScope(); builder.RegisterType <EditPackageService>() .AsSelf() .InstancePerLifetimeScope(); builder.RegisterType <FormsAuthenticationService>() .As <IFormsAuthenticationService>() .InstancePerLifetimeScope(); builder.RegisterType <CookieTempDataProvider>() .As <ITempDataProvider>() .InstancePerLifetimeScope(); builder.RegisterType <NuGetExeDownloaderService>() .AsSelf() .As <INuGetExeDownloaderService>() .InstancePerLifetimeScope(); builder.RegisterType <StatusService>() .AsSelf() .As <IStatusService>() .InstancePerLifetimeScope(); var mailSenderThunk = new Lazy <IMailSender>( () => { 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 => mailSenderThunk.Value) .AsSelf() .As <IMailSender>() .InstancePerLifetimeScope(); builder.RegisterType <MessageService>() .AsSelf() .As <IMessageService>() .InstancePerLifetimeScope(); builder.Register(c => HttpContext.Current.User) .AsSelf() .As <IPrincipal>() .InstancePerLifetimeScope(); switch (configuration.Current.StorageType) { case StorageType.FileSystem: case StorageType.NotSpecified: ConfigureForLocalFileSystem(builder); break; case StorageType.AzureStorage: ConfigureForAzureStorage(builder, configuration); break; } builder.RegisterType <FileSystemService>() .AsSelf() .As <IFileSystemService>() .SingleInstance(); builder.RegisterType <PackageFileService>() .AsSelf() .As <IPackageFileService>() .InstancePerLifetimeScope(); builder.RegisterType <UploadFileService>() .AsSelf() .As <IUploadFileService>() .InstancePerLifetimeScope(); // 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(); ConfigureAutocomplete(builder, configuration); builder.RegisterType <DiagnosticsService>() .AsSelf() .As <IDiagnosticsService>() .SingleInstance(); }
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(new SecretReaderFactory(diagnosticsService)); UrlExtensions.SetConfigurationService(configuration); builder.RegisterInstance(configuration) .AsSelf() .As <PoliteCaptcha.IConfigurationSource>(); 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(); builder.Register(c => new EntitiesContext(configuration.Current.SqlConnectionString, readOnly: 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 <AccountDelete> >() .AsSelf() .As <IEntityRepository <AccountDelete> >() .InstancePerLifetimeScope(); builder.RegisterType <EntityRepository <Credential> >() .AsSelf() .As <IEntityRepository <Credential> >() .InstancePerLifetimeScope(); builder.RegisterType <EntityRepository <PackageOwnerRequest> >() .AsSelf() .As <IEntityRepository <PackageOwnerRequest> >() .InstancePerLifetimeScope(); builder.RegisterType <CuratedFeedService>() .AsSelf() .As <ICuratedFeedService>() .InstancePerLifetimeScope(); builder.Register(c => new SupportRequestDbContext(configuration.Current.SqlConnectionStringSupportRequest)) .AsSelf() .As <ISupportRequestDbContext>() .InstancePerLifetimeScope(); builder.RegisterType <SupportRequestService>() .AsSelf() .As <ISupportRequestService>() .InstancePerLifetimeScope(); builder.RegisterType <UserService>() .AsSelf() .As <IUserService>() .InstancePerLifetimeScope(); builder.RegisterType <PackageNamingConflictValidator>() .AsSelf() .As <IPackageNamingConflictValidator>() .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 <SecurePushSubscription>() .SingleInstance(); builder.RegisterType <RequireSecurePushForCoOwnersPolicy>() .SingleInstance(); var mailSenderThunk = new Lazy <IMailSender>( () => { 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 => mailSenderThunk.Value) .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); 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); }
public override void Load() { var configuration = new ConfigurationService(); Bind <ConfigurationService>().ToMethod(context => configuration); Bind <IAppConfiguration>().ToMethod(context => configuration.Current); Bind <IConfigurationSource>().ToMethod(context => configuration); Bind <AuditingService>().ToConstant(AuditingService.None); Bind <ErrorLog>() .ToMethod(_ => new SqlErrorLog(configuration.Current.SqlConnectionString)) .InSingletonScope(); Bind <ICacheService>().To <HttpContextCacheService>().InRequestScope(); Bind <ICustomerService>().To <CustomerService>().InRequestScope(); Bind <IProductService>().To <ProductService>().InRequestScope(); Bind <IUserService>().To <UserService>().InRequestScope(); Bind <IControllerFactory>() .To <CustomControllerFactory>() .InRequestScope(); var mailSenderThunk = new Lazy <IMailSender>( () => { var settings = Kernel.Get <ConfigurationService>(); 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)); } }); Bind <IMailSender>().ToMethod(context => mailSenderThunk.Value); Bind <IMessageService>().To <MessageService>(); Bind <IPrincipal>().ToMethod(context => HttpContext.Current.User); // ViewModel bindings Bind <INewLicenseViewModel>().To <CustomerSelectionViewModel>(); Bind <INewLicenseViewModel>().To <FeatureSelectionViewModel>(); Bind <INewLicenseViewModel>().To <LicenseExpirationViewModel>(); //Bind<INewLicenseViewModel>().To<ProductNameSelectionViewModel>(); }
public override void Load() { var configuration = new ConfigurationService(); Bind <ConfigurationService>() .ToMethod(context => configuration); Bind <IAppConfiguration>() .ToMethod(context => configuration.Current); Bind <PoliteCaptcha.IConfigurationSource>() .ToMethod(context => configuration); Bind <Lucene.Net.Store.Directory>() .ToMethod(_ => LuceneCommon.GetDirectory(configuration.Current.LuceneIndexLocation)) .InSingletonScope(); ConfigureSearch(configuration); if (!String.IsNullOrEmpty(configuration.Current.AzureStorageConnectionString)) { Bind <ErrorLog>() .ToMethod(_ => new TableErrorLog(configuration.Current.AzureStorageConnectionString)) .InSingletonScope(); } else { Bind <ErrorLog>() .ToMethod(_ => new SqlErrorLog(configuration.Current.SqlConnectionString)) .InSingletonScope(); } Bind <ICacheService>() .To <HttpContextCacheService>() .InRequestScope(); Bind <IContentService>() .To <ContentService>() .InSingletonScope(); Bind <IEntitiesContext>() .ToMethod(context => new EntitiesContext(configuration.Current.SqlConnectionString, readOnly: configuration.Current.ReadOnlyMode)) .InRequestScope(); Bind <IEntityRepository <User> >() .To <EntityRepository <User> >() .InRequestScope(); Bind <IEntityRepository <CuratedFeed> >() .To <EntityRepository <CuratedFeed> >() .InRequestScope(); Bind <IEntityRepository <CuratedPackage> >() .To <EntityRepository <CuratedPackage> >() .InRequestScope(); Bind <IEntityRepository <PackageRegistration> >() .To <EntityRepository <PackageRegistration> >() .InRequestScope(); Bind <IEntityRepository <Package> >() .To <EntityRepository <Package> >() .InRequestScope(); Bind <IEntityRepository <PackageDependency> >() .To <EntityRepository <PackageDependency> >() .InRequestScope(); Bind <IEntityRepository <PackageStatistics> >() .To <EntityRepository <PackageStatistics> >() .InRequestScope(); Bind <IEntityRepository <Credential> >() .To <EntityRepository <Credential> >() .InRequestScope(); Bind <ICuratedFeedService>() .To <CuratedFeedService>() .InRequestScope(); Bind <IUserService>() .To <UserService>() .InRequestScope(); Bind <IPackageService>() .To <PackageService>() .InRequestScope(); Bind <EditPackageService>().ToSelf(); Bind <IFormsAuthenticationService>() .To <FormsAuthenticationService>() .InSingletonScope(); Bind <IControllerFactory>() .To <NuGetControllerFactory>() .InRequestScope(); Bind <INuGetExeDownloaderService>() .To <NuGetExeDownloaderService>() .InRequestScope(); Bind <IStatusService>() .To <StatusService>() .InRequestScope(); var mailSenderThunk = new Lazy <IMailSender>( () => { var settings = Kernel.Get <ConfigurationService>(); 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)); } }); Bind <IMailSender>() .ToMethod(context => mailSenderThunk.Value); Bind <IMessageService>() .To <MessageService>(); Bind <IPrincipal>().ToMethod(context => HttpContext.Current.User); switch (configuration.Current.StorageType) { case StorageType.FileSystem: case StorageType.NotSpecified: ConfigureForLocalFileSystem(); break; case StorageType.AzureStorage: ConfigureForAzureStorage(configuration); break; } Bind <IFileSystemService>() .To <FileSystemService>() .InSingletonScope(); Bind <IPackageFileService>() .To <PackageFileService>(); Bind <IEntityRepository <PackageOwnerRequest> >() .To <EntityRepository <PackageOwnerRequest> >() .InRequestScope(); Bind <IUploadFileService>() .To <UploadFileService>(); // todo: bind all package curators by convention Bind <IAutomaticPackageCurator>() .To <WebMatrixPackageCurator>(); Bind <IAutomaticPackageCurator>() .To <Windows8PackageCurator>(); // todo: bind all commands by convention Bind <IAutomaticallyCuratePackageCommand>() .To <AutomaticallyCuratePackageCommand>() .InRequestScope(); Bind <IPackageIdsQuery>() .To <PackageIdsQuery>() .InRequestScope(); Bind <IPackageVersionsQuery>() .To <PackageVersionsQuery>() .InRequestScope(); }
private void ConfigureJobServices(IServiceCollection services, IConfigurationRoot configurationRoot) { services.Configure <ValidationConfiguration>(configurationRoot.GetSection(ConfigurationSectionName)); services.Configure <VcsConfiguration>(configurationRoot.GetSection(VcsSectionName)); services.Configure <ProcessSignatureConfiguration>(configurationRoot.GetSection(PackageSigningSectionName)); services.Configure <ValidateCertificateConfiguration>(configurationRoot.GetSection(PackageCertificatesSectionName)); services.Configure <OrchestrationRunnerConfiguration>(configurationRoot.GetSection(RunnerConfigurationSectionName)); services.Configure <GalleryDbConfiguration>(configurationRoot.GetSection(GalleryDbConfigurationSectionName)); services.Configure <ValidationDbConfiguration>(configurationRoot.GetSection(ValidationDbConfigurationSectionName)); services.Configure <ServiceBusConfiguration>(configurationRoot.GetSection(ServiceBusConfigurationSectionName)); services.Configure <SmtpConfiguration>(configurationRoot.GetSection(SmtpConfigurationSectionName)); services.Configure <EmailConfiguration>(configurationRoot.GetSection(EmailConfigurationSectionName)); services.AddTransient <ConfigurationValidator>(); services.AddTransient <OrchestrationRunner>(); services.AddScoped <NuGetGallery.IEntitiesContext>(serviceProvider => new NuGetGallery.EntitiesContext( serviceProvider.GetRequiredService <IOptionsSnapshot <GalleryDbConfiguration> >().Value.ConnectionString, readOnly: false)); services.AddScoped(serviceProvider => new ValidationEntitiesContext( serviceProvider.GetRequiredService <IOptionsSnapshot <ValidationDbConfiguration> >().Value.ConnectionString)); services.AddScoped <IValidationEntitiesContext>(serviceProvider => serviceProvider.GetRequiredService <ValidationEntitiesContext>()); services.AddScoped <IValidationStorageService, ValidationStorageService>(); services.Add(ServiceDescriptor.Transient(typeof(NuGetGallery.IEntityRepository <>), typeof(NuGetGallery.EntityRepository <>))); services.AddTransient <NuGetGallery.ICorePackageService, NuGetGallery.CorePackageService>(); services.AddTransient <ISubscriptionClient>(serviceProvider => { var configuration = serviceProvider.GetRequiredService <IOptionsSnapshot <ServiceBusConfiguration> >().Value; return(new SubscriptionClientWrapper(configuration.ConnectionString, configuration.TopicPath, configuration.SubscriptionName)); }); services.AddTransient <ITopicClient>(serviceProvider => { var configuration = serviceProvider.GetRequiredService <IOptionsSnapshot <ServiceBusConfiguration> >().Value; return(new TopicClientWrapper(configuration.ConnectionString, configuration.TopicPath)); }); services.AddTransient <IPackageValidationEnqueuer, PackageValidationEnqueuer>(); services.AddTransient <IValidatorProvider, ValidatorProvider>(); services.AddTransient <IValidationSetProvider, ValidationSetProvider>(); services.AddTransient <IMessageHandler <PackageValidationMessageData>, ValidationMessageHandler>(); services.AddTransient <IServiceBusMessageSerializer, ServiceBusMessageSerializer>(); services.AddTransient <IBrokeredMessageSerializer <PackageValidationMessageData>, PackageValidationMessageDataSerializationAdapter>(); services.AddTransient <IPackageCriteriaEvaluator, PackageCriteriaEvaluator>(); services.AddTransient <VcsValidator>(); services.AddTransient <IProcessSignatureEnqueuer, ProcessSignatureEnqueuer>(); services.AddTransient <NuGetGallery.ICloudBlobClient>(c => { var configurationAccessor = c.GetRequiredService <IOptionsSnapshot <ValidationConfiguration> >(); return(new NuGetGallery.CloudBlobClientWrapper( configurationAccessor.Value.ValidationStorageConnectionString, readAccessGeoRedundant: false)); }); services.AddTransient <NuGetGallery.ICoreFileStorageService, NuGetGallery.CloudBlobCoreFileStorageService>(); services.AddTransient <IValidationPackageFileService, ValidationPackageFileService>(); services.AddTransient <IPackageDownloader, PackageDownloader>(); services.AddTransient <IPackageStatusProcessor, PackageStatusProcessor>(); services.AddTransient <IValidationSetProvider, ValidationSetProvider>(); services.AddTransient <IValidationSetProcessor, ValidationSetProcessor>(); services.AddTransient <IBrokeredMessageSerializer <SignatureValidationMessage>, SignatureValidationMessageSerializer>(); services.AddTransient <IBrokeredMessageSerializer <CertificateValidationMessage>, CertificateValidationMessageSerializer>(); services.AddTransient <IValidatorStateService, ValidatorStateService>(); services.AddTransient <ISimpleCloudBlobProvider, SimpleCloudBlobProvider>(); services.AddTransient <PackageSigningValidator>(); services.AddTransient <MailSenderConfiguration>(serviceProvider => { var smtpConfigurationAccessor = serviceProvider.GetRequiredService <IOptionsSnapshot <SmtpConfiguration> >(); var smtpConfiguration = smtpConfigurationAccessor.Value; if (string.IsNullOrWhiteSpace(smtpConfiguration.SmtpUri)) { return(new MailSenderConfiguration()); } var smtpUri = new SmtpUri(new Uri(smtpConfiguration.SmtpUri)); return(new MailSenderConfiguration { DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network, Host = smtpUri.Host, Port = smtpUri.Port, EnableSsl = smtpUri.Secure, UseDefaultCredentials = false, Credentials = new NetworkCredential( smtpUri.UserName, smtpUri.Password) }); }); services.AddTransient <IMailSender>(serviceProvider => { var mailSenderConfiguration = serviceProvider.GetRequiredService <MailSenderConfiguration>(); return(string.IsNullOrWhiteSpace(mailSenderConfiguration.Host) ? (IMailSender) new DiskMailSender() : (IMailSender) new MailSender(mailSenderConfiguration)); }); services.AddTransient <ICoreMessageServiceConfiguration, CoreMessageServiceConfiguration>(); services.AddTransient <ICoreMessageService, CoreMessageService>(); services.AddTransient <IMessageService, MessageService>(); services.AddTransient <ICommonTelemetryService, CommonTelemetryService>(); services.AddTransient <ITelemetryService, TelemetryService>(); services.AddTransient <ITelemetryClient, TelemetryClientWrapper>(); services.AddTransient <IDiagnosticsService, LoggerDiagnosticsService>(); services.AddSingleton(new TelemetryClient()); services.AddTransient <IValidationOutcomeProcessor, ValidationOutcomeProcessor>(); services.AddSingleton(p => { var assembly = Assembly.GetEntryAssembly(); var assemblyName = assembly.GetName().Name; var assemblyVersion = assembly.GetCustomAttribute <AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "0.0.0"; var client = new HttpClient(new WebRequestHandler { AllowPipelining = true, AutomaticDecompression = (DecompressionMethods.GZip | DecompressionMethods.Deflate), }); client.Timeout = configurationRoot.GetValue <TimeSpan>(PackageDownloadTimeoutName); client.DefaultRequestHeaders.Add("User-Agent", $"{assemblyName}/{assemblyVersion}"); return(client); }); }
public override void Load() { var configuration = new ConfigurationService(); Bind <ConfigurationService>() .ToMethod(context => configuration); Bind <IAppConfiguration>() .ToMethod(context => configuration.Current); Bind <IConfigurationSource>() .ToMethod(context => configuration); if (!String.IsNullOrEmpty(configuration.Current.AzureStorageConnectionString)) { Bind <ErrorLog>() .ToMethod(_ => new TableErrorLog(configuration.Current.AzureStorageConnectionString)) .InSingletonScope(); } else { Bind <ErrorLog>() .ToMethod(_ => new SqlErrorLog(configuration.Current.SqlConnectionString)) .InSingletonScope(); } Bind <ICacheService>() .To <HttpContextCacheService>() .InRequestScope(); Bind <IContentService>() .To <ContentService>() .InSingletonScope(); Bind <IEntitiesContext>() .ToMethod(context => new EntitiesContext(configuration.Current.SqlConnectionString, readOnly: configuration.Current.ReadOnlyMode)) .InRequestScope(); Bind <IEntityRepository <User> >() .To <EntityRepository <User> >() .InRequestScope(); Bind <IEntityRepository <ProjectRegistration> >() .To <EntityRepository <ProjectRegistration> >() .InRequestScope(); Bind <IEntityRepository <Project> >() .To <EntityRepository <Project> >() .InRequestScope(); Bind <IEntityRepository <Credential> >() .To <EntityRepository <Credential> >() .InRequestScope(); Bind <IUserService>() .To <UserService>() .InRequestScope(); Bind <IProjectService>() .To <ProjectService>() .InRequestScope(); Bind <IFormsAuthenticationService>() .To <FormsAuthenticationService>() .InSingletonScope(); Bind <IControllerFactory>() .To <OssFinderControllerFactory>() .InRequestScope(); var mailSenderThunk = new Lazy <IMailSender>( () => { var settings = Kernel.Get <ConfigurationService>(); 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)); } }); Bind <IMailSender>() .ToMethod(context => mailSenderThunk.Value); Bind <IMessageService>() .To <MessageService>(); Bind <IPrincipal>().ToMethod(context => HttpContext.Current.User); switch (configuration.Current.StorageType) { case StorageType.FileSystem: case StorageType.NotSpecified: ConfigureForLocalFileSystem(); break; case StorageType.AzureStorage: ConfigureForAzureStorage(configuration); break; } Bind <IFileSystemService>() .To <FileSystemService>() .InSingletonScope(); }