locked
Background job run in asp.net mvc RRS feed

  • Question

  • User-454825017 posted

    i read this article on background job running from asp.net mvc application.

    https://www.hanselman.com/blog/how-to-run-background-tasks-in-aspnet

    https://github.com/NuGet/WebBackgrounder/tree/master/src

    https://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx/

    few line not clear

    public class JobHost : IRegisteredObject
    {
        private readonly object _lock = new object();
        private bool _shuttingDown;
    
        public JobHost()
        {
            HostingEnvironment.RegisterObject(this);
        }
    
        public void Stop(bool immediate)
        {
            lock (_lock)
            {
                _shuttingDown = true;
            }
            HostingEnvironment.UnregisterObject(this); 
        }
    
        public void DoWork(Action work)
        {
            lock (_lock)
            {
                if (_shuttingDown)
                {
                    return;
                }
                work();
            }
        }
    }

    1) what is IRegisteredObject and what reference need to add to work with IRegisteredObject

    2) what RegisterObject() and UnregisterObject() function does. please give me some details of this function activity.

    using System;
    using System.Threading;
    using WebBackgrounder;
    
    [assembly: WebActivator.PreApplicationStartMethod(
      typeof(SampleAspNetTimer), "Start")]
    
    public static class SampleAspNetTimer
    {
        private static readonly Timer _timer = new Timer(OnTimerElapsed);
        private static readonly JobHost _jobHost = new JobHost();
    
        public static void Start()
        {
            _timer.Change(TimeSpan.Zero, TimeSpan.FromMilliseconds(1000));
        }
    
        private static void OnTimerElapsed(object sender)
        {
            _jobHost.DoWork(() => { /* What is it that you do around here */ });
        }
    }

    1) to work with WebBackgrounder namespace what dll or reference need to add ?

    2) what the meaning of the below line

    [assembly: WebActivator.PreApplicationStartMethod(
      typeof(SampleAspNetTimer), "Start")]

    3) _timer.Change(TimeSpan.Zero, TimeSpan.FromMilliseconds(1000)); this line not clear. what this line will do ? set 1 second time ?

    4) how timer will start and stop ? before calling Start() function OnTimerElapsed() event will fire and start the job. does it make sense ?

    here i asked few small questions. Answer will help me to understand how the above code will work ?

    thanks

    Sunday, October 18, 2020 7:00 AM

Answers

  • User1686398519 posted

    Hi TDP, 

    1. TDP

      1) what is IRegisteredObject and what reference need to add to work with IRegisteredObject

      • You need to use "using System.Web.Hosting;".
    2. TDP

      what RegisterObject() and UnregisterObject() function does. please give me some details of this function activity.
      • RegisterObject:
        • Places an object in the list of registered objects for the application.
      • UnregisterObject:
        • Removes an object from the list of registered objects for the application.
    3. TDP

      to work with WebBackgrounder namespace what dll or reference need to add ?
      • You need to install WebBackgrounder through NuGet.
    4. TDP

      [assembly: WebActivator.PreApplicationStartMethod(
        typeof(SampleAspNetTimer), "Start")]
      • WebActivator  is a neat little dll which allows you to specify anywhere in your code that a certain method is to be called on application startup. 
    5. TDP

      _timer.Change(TimeSpan.Zero, TimeSpan.FromMilliseconds(1000));
      • Timer.Change:Changes the start time and the interval between method invocations for a timer.
    6. You need to add a class in the App_Start folder of your application that will start the Job and manage it's lifetime.
      • You can check this link to help you use WebBackgrounder.
    7. You can also use hangfire to perform background processing. This is a detailed document that can help you use hangfire. 

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 19, 2020 8:38 AM
  • User1686398519 posted

    Hi TDP, 

    1. [assembly: WebActivator.PreApplicationStartMethod(typeof(SampleAspNetTimer), "Start")]
      1. WebActivator allows us to execute code long before the application starts.
        • You can use WebActivator when you need to load external assemblies or instantiate specific objects before the application starts.
      2. This line of code means:
        • Before executing Global.asax, the Start method under the SampleAspNetTimer class will be executed.
      3. In WebActivator:
        1. PreApplicationStartMethod:
          • Use PreApplicationStartMethod before application start method
          • In other words, the SampleAspNetTimer class will be executed before Global.asax.
        2. PostApplicationStartMethod:
          • Use PostApplicationStartMethod after the application is started
        3. ApplicationShutdownMethod:
          • Use ApplicationShutdownMethod after the application is closed
    2. _timer.Change(TimeSpan.Zero, TimeSpan.FromMilliseconds(1000)); does it means OnTimerElapsed() will be called after every i second ?
      • Yes, it will be executed every 1 second.
    3. JobHost and SampleAspNetTimer do not use WebBackgrounder.
      1. In other words, you can execute some methods in the background just by using JobHost and SampleAspNetTimer.
      2. If you want to use WebBackgrounder, you need to add a class in the App_Start folder of your application that will start the Job and manage it's lifetime.
        • You can check this link to help you use WebBackgrounder.

    Here is the result.

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 20, 2020 2:41 AM

All replies

  • User1686398519 posted

    Hi TDP, 

    1. TDP

      1) what is IRegisteredObject and what reference need to add to work with IRegisteredObject

      • You need to use "using System.Web.Hosting;".
    2. TDP

      what RegisterObject() and UnregisterObject() function does. please give me some details of this function activity.
      • RegisterObject:
        • Places an object in the list of registered objects for the application.
      • UnregisterObject:
        • Removes an object from the list of registered objects for the application.
    3. TDP

      to work with WebBackgrounder namespace what dll or reference need to add ?
      • You need to install WebBackgrounder through NuGet.
    4. TDP

      [assembly: WebActivator.PreApplicationStartMethod(
        typeof(SampleAspNetTimer), "Start")]
      • WebActivator  is a neat little dll which allows you to specify anywhere in your code that a certain method is to be called on application startup. 
    5. TDP

      _timer.Change(TimeSpan.Zero, TimeSpan.FromMilliseconds(1000));
      • Timer.Change:Changes the start time and the interval between method invocations for a timer.
    6. You need to add a class in the App_Start folder of your application that will start the Job and manage it's lifetime.
      • You can check this link to help you use WebBackgrounder.
    7. You can also use hangfire to perform background processing. This is a detailed document that can help you use hangfire. 

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 19, 2020 8:38 AM
  • User-454825017 posted

    Sir Thanks a lot for your reply.

    1) This part is not clear. what will be "Start" ? what is second argument ?

    [assembly: WebActivator.PreApplicationStartMethod(
      typeof(SampleAspNetTimer), "Start")]

     WebActivator  is a neat little dll which allows you to specify anywhere in your code that a certain method is to be called on application startup.

    2) _timer.Change(TimeSpan.Zero, TimeSpan.FromMilliseconds(1000)); does it means OnTimerElapsed() will be called after every i second ?

    3) You need to add a class in the App_Start folder of your application that will start the Job and manage it's lifetime.

    if i add any call on App_Start folder then how can i hook that class to inform asp.net engine to run the specific function of that class?

    please advise how to do it.

    Monday, October 19, 2020 8:56 AM
  • User1686398519 posted

    Hi TDP, 

    1. [assembly: WebActivator.PreApplicationStartMethod(typeof(SampleAspNetTimer), "Start")]
      1. WebActivator allows us to execute code long before the application starts.
        • You can use WebActivator when you need to load external assemblies or instantiate specific objects before the application starts.
      2. This line of code means:
        • Before executing Global.asax, the Start method under the SampleAspNetTimer class will be executed.
      3. In WebActivator:
        1. PreApplicationStartMethod:
          • Use PreApplicationStartMethod before application start method
          • In other words, the SampleAspNetTimer class will be executed before Global.asax.
        2. PostApplicationStartMethod:
          • Use PostApplicationStartMethod after the application is started
        3. ApplicationShutdownMethod:
          • Use ApplicationShutdownMethod after the application is closed
    2. _timer.Change(TimeSpan.Zero, TimeSpan.FromMilliseconds(1000)); does it means OnTimerElapsed() will be called after every i second ?
      • Yes, it will be executed every 1 second.
    3. JobHost and SampleAspNetTimer do not use WebBackgrounder.
      1. In other words, you can execute some methods in the background just by using JobHost and SampleAspNetTimer.
      2. If you want to use WebBackgrounder, you need to add a class in the App_Start folder of your application that will start the Job and manage it's lifetime.
        • You can check this link to help you use WebBackgrounder.

    Here is the result.

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 20, 2020 2:41 AM
  • User-454825017 posted

    Sir would you please share the sample project code whose screen shot attached.

    you said - If you want to use WebBackgrounder, you need to add a class in the App_Start folder of your application that will start the Job and manage it's lifetime.

    if i add my class in app_start folder the code will run when IIS will be restart or worker process, app domain recycle ?

    my routine will fire when web application running but no request is coming for web site and web site running in idle mode ?

    please guide me. thanks

    Tuesday, October 20, 2020 9:18 AM