public void ConfigureServices(IServiceCollection services) { ILogger logger = InitLogger(services); IMongoDatabase mongodb = InitMongoDb(); ConnectionMultiplexer redisMultiplexer = InitRedis(); IConnection rabbitMqConn = InitRabbitMqConn(); IBusControl massTransitBus = InitMassTransit(); IClientFactory massTransitClientFactory = massTransitBus.CreateClientFactory(); #region Various //ITokenExtractor services.AddTransient(typeof(ITokenExtractor), (serviceProvider) => { return(new TokenExtractor(_rTokenConfig, new SymmetricKeyProvider(_rTokenKeyConfig))); }); #endregion #region Commands //IGenerateRefreshTokenCommand services.AddTransient(typeof(IGenerateRefreshTokenCommand), (serviceProvider) => { IMongoCollection <AccountRTokenInfo> rtokensRepo = mongodb.GetCollection <AccountRTokenInfo>(_accountRTokensCollectionConfig.Name); return(new GenerateRefreshTokenCommand(_rTokenConfig, new SymmetricKeyProvider(_rTokenKeyConfig), new GuidBasedSecretGenerator(), rtokensRepo, logger)); }); //IGenerateShortTokenCommand services.AddTransient(typeof(IGenerateShortTokenCommand), (serviceProvider) => { var revokedTokensRedisCollectionConfigs = new RedisCollectionConfig(_config.GetSection("redis").GetSection("revokedTokensCollection")); RedisCachedRepo <string> revokedTokensRedisRepo = new RedisCachedRepo <string>(redisMultiplexer.GetDatabase(), revokedTokensRedisCollectionConfigs.CollectionName, new DefaultSerializer(), logger); return(new GenerateShortTokenCommand(new TokenExtractor(_rTokenConfig, new SymmetricKeyProvider(_rTokenKeyConfig)), revokedTokensRedisRepo, new SymmetricKeyProvider(_sTokenKeyConfig), new GuidBasedSecretGenerator(), _sTokenConfig, logger)); }); //IRevokeAllTokensForAccountCommand services.AddTransient(typeof(IRevokeAllTokensForAccountCommand), (serviceProvider) => { IMongoCollection <AccountRTokenInfo> rtokensRepo = mongodb.GetCollection <AccountRTokenInfo>(_accountRTokensCollectionConfig.Name); var revokedTokensRedisCollectionConfigs = new RedisCollectionConfig(_config.GetSection("redis").GetSection("revokedTokensCollection")); RedisCachedRepo <string> revokedTokensRedisRepo = new RedisCachedRepo <string>(redisMultiplexer.GetDatabase(), revokedTokensRedisCollectionConfigs.CollectionName, new DefaultSerializer(), logger); var command = new RevokeAllTokensForAccountCommand(rtokensRepo, logger); var exchangeConfigs = new RabbitMqExchangeConfigs(_config.GetSection("rabbitMq").GetSection("refreshTokenRevokedEventExchange")); command.AddSubsciber(new RabbitMqEventPublisher <RefreshTokenRevokedEvent>( new RabbitMqProducer <RefreshTokenRevokedEvent>(rabbitMqConn, exchangeConfigs, logger), logger)); command.AddSubsciber(new RevokedTokenRedisCacher(revokedTokensRedisRepo, logger)); return(command); }); //IRevokeTokenCommand services.AddTransient(typeof(IRevokeTokenCommand), (serviceProvider) => { IMongoCollection <AccountRTokenInfo> rtokensRepo = mongodb.GetCollection <AccountRTokenInfo>(_accountRTokensCollectionConfig.Name); var revokedTokensRedisCollectionConfigs = new RedisCollectionConfig(_config.GetSection("redis").GetSection("revokedTokensCollection")); RedisCachedRepo <string> revokedTokensRedisRepo = new RedisCachedRepo <string>(redisMultiplexer.GetDatabase(), revokedTokensRedisCollectionConfigs.CollectionName, new DefaultSerializer(), logger); var command = new RevokeTokenCommand(rtokensRepo, logger); var exchangeConfigs = new RabbitMqExchangeConfigs(_config.GetSection("rabbitMq").GetSection("refreshTokenRevokedEventExchange")); command.AddSubsciber(new RabbitMqEventPublisher <RefreshTokenRevokedEvent>( new RabbitMqProducer <RefreshTokenRevokedEvent>(rabbitMqConn, exchangeConfigs, logger), logger)); command.AddSubsciber(new RevokedTokenRedisCacher(revokedTokensRedisRepo, logger)); return(command); }); #endregion #region Queries //IGetAllTokensForAccountQuery services.AddTransient(typeof(IGetAllTokensForAccountQuery), (serviceProvider) => { IMongoCollection <AccountRTokenInfo> rtokensRepo = mongodb.GetCollection <AccountRTokenInfo>(_accountRTokensCollectionConfig.Name); return(new GetAllTokensForAccountQuery(rtokensRepo, logger)); }); #endregion #region MassTransit services.AddTransient(typeof(IRequestClient <UserClaimsRequest>), (serviceProvider) => { var massTransitChannelName = _config.GetSection("rabbitMq").GetSection("getUserClaimsMassTransitChannel")["Name"]; var requestClient = massTransitClientFactory.CreateRequestClient <UserClaimsMQRequest>(new Uri($"rabbitmq://{_rabbitmqConfigs.Host}/{massTransitChannelName}")); return(requestClient); }); services.AddTransient(typeof(IRequestClient <AuthValidationRequest>), (serviceProvider) => { var massTransitChannelName = _config.GetSection("rabbitMq").GetSection("validateCredentialsMassTransitChannel")["Name"]; var requestClient = massTransitClientFactory.CreateRequestClient <AuthValidationMQRequest>(new Uri($"rabbitmq://{_rabbitmqConfigs.Host}/{massTransitChannelName}")); return(requestClient); }); #endregion #region Mvc Framework var mvcBuilder = services.AddMvc(); MvcConfigProvider mvcConfigProvider = new MvcConfigProvider(); mvcBuilder.AddMvcOptions(mvcConfigProvider.GetMvcOptionsConfigurer()) .AddJsonOptions(mvcConfigProvider.GetJsonOptionsConfigurer()); services.AddSession(); #endregion }
public void ConfigureServices(IServiceCollection services) { #region Init logger var logger = LoggerBuilder.Logger() .ForAllLogs() .Console().WithSimpleTemplateLogText("[$LogDate(HH:mm:ss) $Source] - $Description").Add() .BuildLogger(); #endregion //ILogger services.AddTransient(typeof(ILogger), (serviceProvider) => logger); //IValidatorFactory services.AddTransient(typeof(IValidatorFactory), (serviceProvider) => { return(new ValidatorFactory(new StubCultureProvider())); }); //RabbitMq RabbitMQ.Client.IConnectionFactory factory = new ConnectionFactory { HostName = _rabbitmqConfigs.Host, Port = _rabbitmqConfigs.Port, UserName = _rabbitmqConfigs.User, Password = _rabbitmqConfigs.Password, }; RabbitMQ.Client.IConnection rabbitmqConn = factory.CreateConnection(); MongoClient userSysClient = new MongoClient(_mongoDbUserSysDbConfigs.ConnectionString); MappingsInitializer.InitMappings(); IMongoDatabase userSysDb = userSysClient.GetDatabase(_mongoDbUserSysDbConfigs.Database); #region Commands services.AddTransient(typeof(ICreateAccountCommand), (serviceProvider) => { IMongoCollection <Account> accountCollection = userSysDb.GetCollection <Account>(_accountsCollectionConfig.Name); // Email confirmation strategy. IMongoCollection <ConfirmEmailRequest> emailConfirmCollection = userSysDb.GetCollection <ConfirmEmailRequest>(_emailConfirmRequestsCollectionConfig.Name); var emailConfirmationStrategy = new ConfirmLinkEmailConfirmationStrategy(emailConfirmCollection, accountCollection, new SecretSha256Helper(), new GuidBasedSecretGenerator(32), _emailConfirmUrlBase, logger); //Add event subscribers to the email confirmation strategy. var emailConfirmationByLinkRequestEventExchange = new RabbitMqExchangeConfigs(_config.GetSection("rabbitMq").GetSection("emailConfirmationByLinkRequestEventExchange")); emailConfirmationStrategy.AddSubsciber(new RabbitMqEventPublisher <EmailConfirmationByLinkRequestEvent>( new RabbitMqProducer <EmailConfirmationByLinkRequestEvent>(rabbitmqConn, emailConfirmationByLinkRequestEventExchange, logger), logger)); // Create password set strategy. var passwordStrategy = new UserAssignedPasswordSetStrategy(accountCollection, new SecretSha256Helper(), new GuidBasedSecretGenerator(32), logger); // Create the command. var command = new CreateAccountCommand(accountCollection, emailConfirmationStrategy, passwordStrategy, new NewAccountValidationStrategy(), logger); // Add event subscribers to the command. var accountCreatedEventExchangeConfigs = new RabbitMqExchangeConfigs(_config.GetSection("rabbitMq").GetSection("accountCreatedEventExchange")); command.AddSubsciber(new RabbitMqEventPublisher <NewAccountCreatedEvent>( new RabbitMqProducer <NewAccountCreatedEvent>(rabbitmqConn, accountCreatedEventExchangeConfigs, logger), logger)); return(command); }); services.AddTransient(typeof(IConfirmEmailCommand), (serviceProvider) => { IMongoCollection <Account> accountCollection = userSysDb.GetCollection <Account>(_accountsCollectionConfig.Name); IMongoCollection <ConfirmEmailRequest> emailConfirmCollection = userSysDb.GetCollection <ConfirmEmailRequest>(_emailConfirmRequestsCollectionConfig.Name); return(new ConfirmEmailCommand(emailConfirmCollection, accountCollection, new SecretSha256Helper(), 30, logger)); }); services.AddTransient(typeof(ICreateRoleCommand), (serviceProvider) => { IMongoCollection <Role> roleCollection = userSysDb.GetCollection <Role>(_roleCollectionConfig.Name); var command = new CreateRoleCommand(roleCollection, new NewRoleValidationStrategy(), logger); // Add event subscribers to the command. var eventExchageConfigs = new RabbitMqExchangeConfigs(_config.GetSection("rabbitMq").GetSection("roleCreatedEventExchange")); command.AddSubsciber(new RabbitMqEventPublisher <RoleCreatedOrEditedEvent>( new RabbitMqProducer <RoleCreatedOrEditedEvent>(rabbitmqConn, eventExchageConfigs, logger), logger)); return(command); }); services.AddTransient(typeof(IEditRoleCommand), (serviceProvider) => { IMongoCollection <Role> roleCollection = userSysDb.GetCollection <Role>(_roleCollectionConfig.Name); var command = new EditRoleCommand(roleCollection, new NewRoleValidationStrategy(), logger); // Add event subscribers to the command. var eventExchageConfigs = new RabbitMqExchangeConfigs(_config.GetSection("rabbitMq").GetSection("roleEditedEventExchange")); command.AddSubsciber(new RabbitMqEventPublisher <RoleCreatedOrEditedEvent>( new RabbitMqProducer <RoleCreatedOrEditedEvent>(rabbitmqConn, eventExchageConfigs, logger), logger)); return(command); }); services.AddTransient(typeof(ICreateUserCommand), (serviceProvider) => { IMongoCollection <User> userCollection = userSysDb.GetCollection <User>(_userCollectionConfig.Name); IMongoCollection <Account> accountCollection = userSysDb.GetCollection <Account>(_accountsCollectionConfig.Name); IMongoCollection <Role> roleCollection = userSysDb.GetCollection <Role>(_roleCollectionConfig.Name); var command = new CreateUserCommand(userCollection, accountCollection, roleCollection, new NewUserValidationStrategy(new NewContactValidationStrategy()), logger); // Add event subscribers to the command. var eventExchageConfigs = new RabbitMqExchangeConfigs(_config.GetSection("rabbitMq").GetSection("userCreatedEventExchange")); command.AddSubsciber(new RabbitMqEventPublisher <UserCreatedEvent>( new RabbitMqProducer <UserCreatedEvent>(rabbitmqConn, eventExchageConfigs, logger), logger)); return(command); }); services.AddTransient(typeof(IEditUserRolesCommand), (serviceProvider) => { IMongoCollection <User> userCollection = userSysDb.GetCollection <User>(_userCollectionConfig.Name); IMongoCollection <Account> accountCollection = userSysDb.GetCollection <Account>(_accountsCollectionConfig.Name); IMongoCollection <Role> roleCollection = userSysDb.GetCollection <Role>(_roleCollectionConfig.Name); var command = new EditUserRolesCommand(userCollection, accountCollection, roleCollection, logger); //Add event subscribers to this command. var exchangeConfigs = new RabbitMqExchangeConfigs(_config.GetSection("rabbitMq").GetSection("userRolesChangedEventExchange")); command.AddSubsciber(new RabbitMqEventPublisher <UserRolesChangedEvent>( new RabbitMqProducer <UserRolesChangedEvent>(rabbitmqConn, exchangeConfigs, logger), logger)); return(command); }); services.AddTransient(typeof(IEditUserDataAndContactsCommand), (serviceProvider) => { IMongoCollection <User> userCollection = userSysDb.GetCollection <User>(_userCollectionConfig.Name); IMongoCollection <Account> accountCollection = userSysDb.GetCollection <Account>(_accountsCollectionConfig.Name); IMongoCollection <Role> roleCollection = userSysDb.GetCollection <Role>(_roleCollectionConfig.Name); var command = new EditUserDataAndContactsCommand(userCollection, accountCollection, new UserDataAndContactsEditValidationStrategy(new NewContactValidationStrategy()), logger); //Add event subscribers to this command. var exchangeConfigs = new RabbitMqExchangeConfigs(_config.GetSection("rabbitMq").GetSection("userDataChangedEventExchange")); command.AddSubsciber(new RabbitMqEventPublisher <UserDataChangedEvent>( new RabbitMqProducer <UserDataChangedEvent>(rabbitmqConn, exchangeConfigs, logger), logger)); return(command); }); services.AddTransient(typeof(IChangeAccountPasswordCommand), (serviceProvider) => { IMongoCollection <Account> accountCollection = userSysDb.GetCollection <Account>(_accountsCollectionConfig.Name); var command = new ChangeAccountPasswordCommand(accountCollection, new PasswordChangeValidationStrategy(), new SecretSha256Helper(), new GuidBasedSecretGenerator(), logger); //Add event subscribers to this command. var exchangeConfigs = new RabbitMqExchangeConfigs(_config.GetSection("rabbitMq").GetSection("passwordChangedEventExchange")); command.AddSubsciber(new RabbitMqEventPublisher <PasswordChangedEvent>( new RabbitMqProducer <PasswordChangedEvent>(rabbitmqConn, exchangeConfigs, logger), logger)); return(command); }); //IChangeAccountEmailCommand services.AddTransient(typeof(IChangeAccountEmailCommand), (serviceProvider) => { IMongoCollection <Account> accountCollection = userSysDb.GetCollection <Account>(_accountsCollectionConfig.Name); IMongoCollection <EmailChangeRecord> emailChangedRecordCollection = userSysDb.GetCollection <EmailChangeRecord>("emailChangeRecords"); IMongoCollection <ConfirmEmailRequest> emailConfirmCollection = userSysDb.GetCollection <ConfirmEmailRequest>(_emailConfirmRequestsCollectionConfig.Name); var emailConfirmationStrategy = new ConfirmLinkEmailConfirmationStrategy(emailConfirmCollection, accountCollection, new SecretSha256Helper(), new GuidBasedSecretGenerator(32), _emailConfirmUrlBase, logger); //Add event subscribers to this strategy. var newEmailAddedEventExchageConfigs = new RabbitMqExchangeConfigs(_config.GetSection("rabbitMq").GetSection("emailConfirmationByLinkRequestEventExchange")); emailConfirmationStrategy.AddSubsciber(new RabbitMqEventPublisher <EmailConfirmationByLinkRequestEvent>( new RabbitMqProducer <EmailConfirmationByLinkRequestEvent>(rabbitmqConn, newEmailAddedEventExchageConfigs, logger), logger)); var command = new ChangeAccountEmailCommand(accountCollection, emailChangedRecordCollection, new EmailValidationStrategy(), emailConfirmationStrategy, logger); //Add event subscribers to this command. var exchangeConfigs = new RabbitMqExchangeConfigs(_config.GetSection("rabbitMq").GetSection("accountEmailChangedEventExchange")); command.AddSubsciber(new RabbitMqEventPublisher <AccountEmailChangedEvent>( new RabbitMqProducer <AccountEmailChangedEvent>(rabbitmqConn, exchangeConfigs, logger), logger)); return(command); }); //IUndoChangeAccountEmailCommand services.AddTransient(typeof(IUndoChangeAccountEmailCommand), (serviceProvider) => { IMongoCollection <Account> accountCollection = userSysDb.GetCollection <Account>(_accountsCollectionConfig.Name); IMongoCollection <EmailChangeRecord> emailChangedRecordCollection = userSysDb.GetCollection <EmailChangeRecord>("emailChangeRecords"); var command = new UndoChangeAccountEmailCommand(accountCollection, emailChangedRecordCollection, logger); //Add event subscribers to this command. var exchangeConfigs = new RabbitMqExchangeConfigs(_config.GetSection("rabbitMq").GetSection("accountEmailChangeUndoEvent")); command.AddSubsciber(new RabbitMqEventPublisher <AccountEmailChangeUndoEvent>( new RabbitMqProducer <AccountEmailChangeUndoEvent>(rabbitmqConn, exchangeConfigs, logger), logger)); return(command); }); #endregion #region Queries services.AddTransient(typeof(IGetUsersQuery), (serviceProvider) => { IMongoCollection <User> userCollection = userSysDb.GetCollection <User>(_userCollectionConfig.Name); return(new GetUsersQuery(userCollection, logger)); }); services.AddTransient(typeof(IGetUserDetailsQuery), (serviceProvider) => { IMongoCollection <User> userCollection = userSysDb.GetCollection <User>(_userCollectionConfig.Name); IMongoCollection <Role> roleCollection = userSysDb.GetCollection <Role>(_roleCollectionConfig.Name); return(new GetUserDetailsQuery(userCollection, roleCollection, logger)); }); services.AddTransient(typeof(IGetRolesQuery), (serviceProvider) => { IMongoCollection <Role> roleCollection = userSysDb.GetCollection <Role>(_roleCollectionConfig.Name); return(new GetRolesQuery(roleCollection, logger)); }); #endregion #region MVC Framework var mvcBuilder = services.AddMvc(opt => { opt.ModelBinderProviders.Insert(0, new IntToBoolModelBinderProvider()); }) .AddSessionStateTempDataProvider(); services.AddSession(); MvcConfigProvider mvcConfigProvider = new MvcConfigProvider(); mvcBuilder.AddMvcOptions(mvcConfigProvider.GetMvcOptionsConfigurer()) .AddJsonOptions(mvcConfigProvider.GetJsonOptionsConfigurer()); #endregion services.AddLogging(); }