

Public class MyEmailAppService : ApplicationService, IMyEmailAppService Let's add a new job for SimpleSendEmailJob, as we defined before: Instantiate and execute the TestJob with 42 as the argument. We sent 42 as an argument while enqueuing. _backgroundJobManager = backgroundJobManager Public MyService(IBackgroundJobManager backgroundJobManager) Private readonly IBackgroundJobManager _backgroundJobManager TestJob as defined above: public class MyService IBackgroundJobManager to add a job to the queue. Add a New Job To the QueueĪfter defining a background job, we can inject and use NET's built-in binary serialization.Įntities or other non-serializable objects.Īs shown in the SimpleSendEmailJob sample, we can only store the Id ofĪn entity and get the entity from the repository inside the job. attribute), it's better to define the Īttribute since we may switch to another job manager in the future, for which we may use Job manager uses JSON serialization (which does not need the While ASP.NET Boilerplate's default background SimpleSendEmailJobArgs is the job argument here and defined as shown below: Ī job argument should be serializable, because it's serialized and


We injected the user repository to get user emails, and injected the email sender (a service to send emails) and simply sent the email. _emailSender.Send(senderUser.EmailAddress, targetUser.EmailAddress, args.Subject, args.Body) Var targetUser = _userRepository.Get(args.TargetUserId) Var senderUser = _userRepository.Get(args.SenderUserId) Public override void Execute(SimpleSendEmailJobArgs args) Public SimpleSendEmailJob(IRepository userRepository, IEmailSender emailSender) Private readonly IEmailSender _emailSender Private readonly IRepository _userRepository Queue: public class SimpleSendEmailJob : BackgroundJob, ITransientDependency Let's define a more realistic job which sends emails in a background ITransientDependency is the simplest way. The argument type is defined as a generic classĪ background job must be registered via dependency Public override async Task ExecuteAsync(int number)Ī background job defines an Execute or ExecuteAsync method that gets an inputĪrgument. Here is the most simple async background job: public class TestJob : AsyncBackgroundJob, ITransientDependency Here is the most simple background job: public class TestJob : BackgroundJob, ITransientDependency We can create a background job class by either inheriting from theīackgroundJob class or AsyncBackgroundJob class or by directly implementing the See the Background Job Store section for more information on job Overcome temporary failures and guarantee that itĮventually will be sent. For example, you can send emails in a background job to
DELAYED JOB ENQUEUE CODE
To create re-trying and persistent tasks to guarantee code will be successfullyĮxecuted.You add this job to the queue and send the report's result to User presses a 'report' button to start a long-running reporting To perform long-running tasks without having the users wait.You may need background jobs for several reasons.

In a queued and persistent manner, background jobs are used to queue some tasks to be executed in theīackground. To execute some tasks in the background threads of an application.
