Base solution for your next web application
Open Closed

CurrentUnitOfWork becomes null after sending email through sendgrid #6768


User avatar
0
firnas created

I have a written a background worker to send email notifications to my users periodically using send grid. I followed the following example in your documetation.

https://aspnetboilerplate.com/Pages/Documents/v1.0.0.0/Background-Jobs-And-Workers

I am using the Send Grid nuget package to send emails. The CurrentUnitOfWork becomes null after i call the sendgrids,

await client.SendEmailAsync(message);

method. There for the below line fails in the worker class,

await CurrentUnitOfWork.SaveChangesAsync();

Can you please let me know why this happens and how to overcome this problem.

Thanks, Firnas


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

    You are on ABP 1.0.0?

    Show code of your worker class.

  • User Avatar
    0
    maliming created
    Support Team

    Please share your relevant code?

    And try to add the unitofwork attribute to the method https://aspnetboilerplate.com/Pages/Documents/Unit-Of-Work#unitofwork-attribute

  • User Avatar
    0
    firnas created
    [UnitOfWork]
    protected override async void DoWork()
    {
    	//disable filter to get all user data
    	using (CurrentUnitOfWork.DisableFilter(AbpDataFilters.MayHaveTenant))
    	{
    		//list to hold the missing documents
    		IList<MissingDocumentDto> missingDocumentDtos = new List<MissingDocumentDto>();
    
    		//get all non profits and uploaded documents
    		IList<NonProfit> nonProfits = _nonProfitRepository.GetAllIncluding(np => np.Documents).ToList();
    
    		//loop through the non profits
    		foreach (NonProfit nonProfit in nonProfits)
    		{
    			//get tenant id for the non profit.
    			int nonProfitTenantId = nonProfit.TenantId;
    
    			//get the required document types for the tenant
    			IList<DocumentType> requiredDocumentTypes = _documentTypeRepository.GetAllList(np =>
    															np.TenantId == nonProfitTenantId &&
    															np.Required == true);
    
    			IList<long> missingDocumentIds = new List<long>();
    
    			//get the list of required documents not uploaded or commented by the non profit
    			requiredDocumentTypes.ToList().ForEach(rd =>
    			{
    				var document = nonProfit.Documents.Where(d => d.DocumentTypeId == rd.Id && d.IsDeleted == false).FirstOrDefault();
    				if (document == null)
    				{
    					//has not uploaded the document. add to the list
    					missingDocumentIds.Add(rd.Id);
    				}
    				else
    				{
    					//check if the document has been marked does not exist
    					if (document.DoesNotExist)
    					{
    						//not uploaded the document. add to the list
    						missingDocumentIds.Add(rd.Id);
    					}
    				}
    			});
    
    			//add to the missing list
    			if (missingDocumentIds.Count > 0)
    			{
    				missingDocumentDtos.Add(new MissingDocumentDto
    				{
    					NonProfit = nonProfit,
    					MissingDoucmentsIds = missingDocumentIds
    				});
    			}
    		}
    
    		//send the email
    		//await SendEmailAsync(missingDocumentDtos);
    		string apiKey = await _settingManager.GetSettingValueAsync(AppSettings.SendGridManagement.SendGridAPIKey);
    		string adminEmailAddress = await _settingManager.GetSettingValueAsync(AppSettings.SendGridManagement.AdminEmailAddress);
    		string subject = await _settingManager.GetSettingValueAsync(AppSettings.SendGridManagement.Subject);
    
    		//get the email template
    		string emailTemplate = GetEmailTemplate();
    
    		//replace the body
    		emailTemplate = emailTemplate.Replace("{EMAIL_TITLE}", subject)
    			.Replace("{EMAIL_SUB_TITLE}", subject)
    			.Replace("{EMAIL_BODY}", "test");
    
    		SendGridMessage message = MailHelper.CreateSingleEmail(
    			new EmailAddress(adminEmailAddress),
    			new EmailAddress("[email protected]")
    			, subject
    			, "Email"
    			, emailTemplate);
    
    		var client = new SendGridClient(apiKey);
    		**await client.SendEmailAsync(message);**
    
    		await CurrentUnitOfWork.SaveChangesAsync();
    
    	}
    }
    

    shared the code

  • User Avatar
    0
    aaron created
    Support Team

    Don't do async void.

  • User Avatar
    0
    firnas created

    if i dont use the async then the await operators thorws a error.

    I changed my code to send the email after the using block. Nowit works fine. But is that a good way to do a task outside the using statement?

  • User Avatar
    1
    aaron created
    Support Team

    if i dont use the async then the await operators thorws a error.

    So don't use await. Use the sync methods or check out AsyncHelper.RunSync.

    I changed my code to send the email after the using block. Nowit works fine. But is that a good way to do a task outside the using statement?

    It is good to not disable filters unnecessarily.

  • User Avatar
    0
    firnas created

    Thanks aaron for ths support.