GithubHelp home page GithubHelp logo

appstartuptasks's Introduction

AppStartupTasks

Offers a clean solution to be able to register asynchronous startup tasks, and then execute those at a point wherever you chose in your application, like:-

  • In Program.cs before starting the main web host
  • In a console app before executing a particular command
  • or wherever.

This problem is blogged about previously see https://dev-sock.netlify.app/running-async-tasks-on-app-startup-in-asp-net-core-part-1/

However the authors solution isn't what I was after because:

  1. It depends on IServer being registered - e.g the solution will only work within the context of something like a WebHost server
  2. There is a lack of control over then the tasks get executed precisely - in above case they will be executed then the IServer is started. In a console app or a test you may not be using IHosting but you are still likely to be using dependency injection.

Solution

The solution is:

  1. Provide a way to register start up tasks in ServiceCollection
  2. Provide an extension method on IServiceProvider to execute those tasks.

This way, you can register all the tasks, and then execute them from the DI container at whatever point you chose and the baseline requirement is just that you use DI, and no longer coupled to other infrastructure like a hosting layer.

   [Fact]
   public async Task Docs_Usage()
   {
        var services = new ServiceCollection();

        // 1. Register the various app start up tasks in order. They won't be executed yet.
        // .. you can optionaly create extension methods on this builder, to improve fluency. 
        services.AddAppStartupTasks(builder =>
        {
            // Add some async delegate
            builder.Add(async (sp) =>
                {
                    // var someDep = sp.GetRequiredService<ILogger<Program>>()>()
                    await Task.Delay(100);
                })
                .Add<WriteSomethingToConsole>() // register your own implementation of IAppStartupTask - supports DI.
                .Add<WriteMessageToConsole>(sp =>
                    new WriteMessageToConsole("Yo")); // shows registering your IAppStartupTask using a factory method
        });

        // Also you can bypass the builder and just register IAppStartupTask implementation directly.
        services.AddScoped<IAppStartupTask, WriteSomethingToConsole>();
        
        
        // 2. At some point in your application infrastructure, once the `IServiceProvider` (container) is built,
       // execute the tasks - a convenience extension method on `IServiceProvider` is provided.       
        var sp = await services.BuildServiceProvider()
                               .StartupTasksAsync(CancellationToken.None); // if your infra provides a useful cancel token, pass it through.
   
       // Now proceed to continue spinning up your application, or starting your web host or whaatever.
   }

appstartuptasks's People

Contributors

dazinator avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.