jamesmh / coravel

Near-zero config .NET library that makes advanced application features like Task Scheduling, Caching, Queuing, Event Broadcasting, and more a breeze!
https://docs.coravel.net/Installation/
MIT License
3.63k stars 243 forks source link

Sample to show how to execute Task on demand in .NET core 7 #361

Open nssidhu opened 3 months ago

nssidhu commented 3 months ago

Describe the solution you'd like I am using Blazor server .NET 7 My requirement is to run the long running process in the background thread. This long running code should only run once and only when user submits the form by clicking the button.

Here is sample of my code.

public class SubmitBackgroundTask : IInvocable { private readonly ILogger _logger; private readonly string _storyID; IDbContextFactory _DbFactory; LexIADBContext _dbContext; public SubmitBackgroundTask(ILogger logger, IDbContextFactory DbFactory, string StoryId) { _DbFactory = DbFactory; _logger = logger; _storyID = StoryId; } public async Task Invoke() { using var _dbContext = await _DbFactory.CreateDbContextAsync(); _logger.LogInformation($"Invocable task running. StoryId is {_storyID}"); await Task.CompletedTask; } } Describe alternatives you've considered I tried to use DI, but i am getting run time error for my second which i cannot provide during registration in program.cs, that userID string parameter is only available when user Click the submit button on index page