Skip to content

dlopezb93/DLB.AspNetCore.BackgroundTasks

Repository files navigation

DLB.AspNetCore.BackgroundTasks

DLB. AspNetCore. Background Tasks allow generate a new task in AspNetCore 2.2+ with the new scope. This behavior is useful when you need to generate new task and free the actual request. This functionality is so easy to implement, we only need to call a service collection extensions method.

Requirements

AspNetCore 2.2+ Microsoft.Extentions.Logging

Installation

DLB.AspNetCore.BackgroundTasks is avaible in nugget gallery.

Usage

First, we need register necessary dependencies, this is achieved calling a method in Startup. case class.

public IServiceProvider ConfigureServices(IServiceCollection services)
{
   //HostedServices and Queue
    services.AddBackgroundQueue();
}

That is all! If we need to generate a new task in the background, only need insect in the constructor IBackgroundTaskQueue interface.

Samples:

  1. Task with additional parameters
  2. Task without parameters

Background Task with parameters

public UserController(IBackgroundTaskQueue)
{
    _taskQueue = taskQueue ?? throw new NullException(..);
}

[Get]
public async Task<IActionResult> TestMethod(string info)
{
	//this block request
	await GetUsersAsync();
	
	// Raise background task
	var payload = new BackgrounData(info);
	_taskQueue.QueueBackgroundTask<BackgroundTask,BackgroundData>(payload);
	
	return Ok();
}

public class BackgroundTask : IBackgroundDataTask<BackgrounData>
{
    public BackgroundData Payload {get; set;}

    public async Task ExecuteAsync(IServiceScope scope, CancellationToken token)
    {
        var userAppService = scope.ServiceProvider.GetService<IUserAppService>();

        await userAppService.SomeMethod(Payload.Info)
    }
}

public class BackgroundData
{
    public BackgroundData(string info)
    {
	    Info = info
    }
    
    public string Info {get; set;}
}

Background task without parameters

public UserController(IBackgroundTaskQueue taskQueue)
    {
	    _taskQueue = taskQueue ?? throw new NullException(..);
    }
    
    [Get]
    public async Task<IActionResult> TestMethod(string info)
    {
	    //this block request
	    await GetUsersAsync();
	    
	    // Raise background task
	    _taskQueue.QueueBackgroundTask<SimpleBackgroundTask>();
	    
	    return Ok();
    }
}

public class SimpleBackgroundTask : IBackgroundTask
{
	public async Task ExecuteAsync(IServiceScope scope, CancellationToken token)
    {
        var userAppService = scope.ServiceProvider.GetService<IUserAppService>();

        await userAppService.SomeMethod()
    }
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages