Closed tiagosarri closed 1 year ago
There is no HttpContext can be retrieved in background since it is not a http request when Hangfire execute the job. That's why we need the Postal.RequestPath class to create the DefaultHttpContext object in Postal library.
@tiagosarri Best way around this is to create the email using Postal and then to serialise it so that hangfire can de-serialise it. Using the smtp client you can then construct a MailMessage which will then allow you to send emails via HangFire.
@tiagosarri alternatively simply get what you need out of the httpcontext and pass it as a parameter to the method that creates your email, and use the param instead of the context inside your email. ie:
BackgroundJob.Enqueue(() => SendWelcomeEmail(context.whatever).
public Task SendWelcomeEmail(string username){...send the email in here...}
I'm trying to send emails using Hangfire and Postal, but I'm having problems with the HTTPContext. If I execute the shooting through a controller the sending is done without problems, now if I execute a job via Hangfire an expection is generated.
Code startup.cs
Code NotificationServices.cs
The error is in HttpContext: _httpContextAccessor.HttpContext.Request.PathBase.ToString() Object reference not set to an instance of an object
Is it possible to send e-mails via background with Postal?