Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Why is jumping to MultiDbContextConnectionStringResolver, GetNameOrConnectionString method? #6400


User avatar
0
namaro created
var task = Task.Run(async () => await SendEmail(solicitud, tipoDeLaSolicitud));

I want to send an email in the background so that the user doesn't need to wait for the email to be sent.

In order to achieve this I have run a Task.Run() for the process runs in the background and not in the same thread. When SendEmailSolicitanteAsync calls GetUserDomainByUserName and finishes is jumping to MultiDbContextConnectionStringResolver.GetNameOrConnectionString method causing an object null reference exception and crashing the program. I don't understand why is jumping to that context because I'm not doing interacting with the database.

Please help and thank you before hand.

private async Task SendEmail(Solicitud solicitud, TipoSolicitud tipoDeLaSolicitud)
{
    Logger.Info("Evento crear una solicitud fue ejecutado.");

    await _solicitudEmailer.SendEmailSolicitanteAsync(solicitud.EmailSolicitante,
        tipoDeLaSolicitud.Nombre,
        solicitud.CodigoSolicitud,
        solicitud.Aprobadores.Split(","), true);
}

public async Task SendEmailSolicitanteAsync(string to, string tipoSolicitud, string codigoSolicitud, string[] aprobadores, bool appendLinkUrl)
{
    var tasks = aprobadores.Select(aprobador => _userDomainAppService.GetUserDomainByUserName(aprobador));
    var listAprobadores = await Task.WhenAll(tasks);
    // more code but it is not necessary for me to put it
}

public async Task<UsersDomain> GetUserDomainByUserName(string userName)
{
    UsersDomain userDomain = null;
    var response = await _clientHttp.GetAsync(_urlGetUser + userName);

    if (response.IsSuccessStatusCode)
    {
        userDomain = await response.Content.ReadAsAsync<UsersDomain>();
    }

    return userDomain;
}

public class MultiDbContextConnectionStringResolver : DefaultConnectionStringResolver
{
    private readonly IAbpStartupConfiguration _configuration;
    
    public MultiDbContextConnectionStringResolver(IAbpStartupConfiguration configuration)
        : base(configuration)
    {
        _configuration = configuration;
    }

    public override string GetNameOrConnectionString(ConnectionStringResolveArgs args)
    {
        if (args["DbContextConcreteType"] as Type == typeof(SICIDbContext))
        {
            // var configuration = AppConfigurations.Get(WebContentDirectoryFinder.CalculateContentRootFolder());
            return (string)_configuration.Get(TestConsts.ConnextionStringNameSici);
            // configuration.GetConnectionString(TestConsts.ConnextionStringNameSici);
        }

        return base.GetNameOrConnectionString(args);
    }
}

2 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    ~~Please format your question properly.~~ Formatted for you.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @namaro

    Have you solved your problem ?