Beispiel #1
0
 /// <summary>
 /// Performs constructor injection for repository interfaces used in this service.
 /// </summary>
 /// <param name="userManager"></param>
 /// <param name="signInManager"></param>
 /// <param name="tokenManagement"></param>
 /// <param name="httpClient"></param>
 /// <param name="contextRepository"></param>
 /// <param name="userRepository"></param>
 /// <param name="httpContextAccessor"></param>
 /// <param name="milvaMailSender"></param>
 /// <param name="sharedLocalizer"></param>
 /// <param name="milvaLogger"></param>
 /// <param name="redisCacheService"></param>
 public AccountService(UserManager <AppUser> userManager,
                       SignInManager <AppUser> signInManager,
                       ITokenManagement tokenManagement,
                       HttpClient httpClient,
                       IContextRepository <EducationAppDbContext> contextRepository,
                       IBaseRepository <AppUser, Guid, EducationAppDbContext> userRepository,
                       IHttpContextAccessor httpContextAccessor,
                       IMilvaMailSender milvaMailSender,
                       IStringLocalizer <SharedResource> sharedLocalizer,
                       IMilvaLogger milvaLogger,
                       IRedisCacheService redisCacheService) : base(userManager, signInManager, tokenManagement, contextRepository, sharedLocalizer, httpContextAccessor, true)
 {
     _userName            = httpContextAccessor.HttpContext.User.Identity.Name;
     _redisCacheService   = redisCacheService;
     _milvaLogger         = milvaLogger;
     _milvaMailSender     = milvaMailSender;
     _userRepository      = userRepository;
     _userManager         = userManager;
     _signInManager       = signInManager;
     _httpClient          = httpClient;
     _contextRepository   = contextRepository;
     _httpContextAccessor = httpContextAccessor;
     _localizer           = sharedLocalizer;
     _tokenManagement     = tokenManagement;
     _loginProvider       = tokenManagement.LoginProvider;
     _tokenName           = tokenManagement.TokenName;
 }
    /// <summary>
    /// It performs the requested redis action in try catch blocks. If redis client not connected, connects.
    /// If an error occurs when performing action or connecting to redis, it throws the <see cref="MilvaUserFriendlyException"/> error along with the message key.
    /// Fatal logging if <paramref name="milvaLogger"/> object is not null.
    /// </summary>
    /// <param name="action"></param>
    /// <param name="userFriendlyMessageLocalizerKey"></param>
    /// <param name="milvaLogger"></param>
    /// <returns></returns>
    public async Task <T> PerformRedisActionAsync <T>(Func <Task <T> > action, string userFriendlyMessageLocalizerKey, IMilvaLogger milvaLogger = null)
    {
        try
        {
            await CheckClientAndConnectIfNotAsync().ConfigureAwait(false);

            if (IsConnected())
            {
                return(await action().ConfigureAwait(false));
            }
            else
            {
                throw new MilvaUserFriendlyException(userFriendlyMessageLocalizerKey);
            }
        }
        catch (MilvaUserFriendlyException)
        {
            if (milvaLogger != null)
            {
                _ = milvaLogger.LogFatalAsync("Cannot reach redis server.", MailSubject.Error);
            }

            throw;
        }
    }