【问题标题】:Using webbackgrounder nuget in MVC to run background task for long time在MVC中使用webbackgrounder nuget长时间运行后台任务
【发布时间】:2014-11-13 19:39:31
【问题描述】:

我需要在后台执行一项任务,那么我的任务是什么?我有一个表格存储每个客户的租金金额,所以我需要在特定的datetimeackf 之后计算每个月的租金价格,所以我用谷歌搜索它,我找到了一段代码(它是 nuget,称为@987654324 @) 我将它添加到我的解决方案中,它为我提供了这部分代码来处理我的任务:

using System;
using System.Threading;
using System.Threading.Tasks;

    namespace WebBackgrounder.DemoWeb
    {
        public class SampleJob : Job
        {
            public SampleJob(TimeSpan interval, TimeSpan timeout)
                : base("Sample Job", interval, timeout)
            {
            }

            public override Task Execute()
            {
                return new Task(() => Thread.Sleep(3000));
            }
        }
    }

我想知道如何对我的任务进行编程?

更多详情:Here

我找到this article 但其实我不知道我可以长期使用这种方法吗? 最好的问候。

任何想法都会受到赞赏。

【问题讨论】:

    标签: c# multithreading asp.net-mvc-4 scheduled-tasks nuget


    【解决方案1】:

    您还需要在应用程序的 App_Start 文件夹中添加一个类,该类将启动 Job 并管理其生命周期。你可以在这里看到一个例子……https://github.com/NuGet/WebBackgrounder/tree/master/src/WebBackgrounder.DemoWeb

    这是演示应用程序的代码

    using System;
    using Elmah;
    using WebBackgrounder.Jobs;
    
    [assembly: WebActivator.PostApplicationStartMethod(typeof(WebBackgrounder.DemoWeb.App_Start.WebBackgrounderSetup), "Start")]
    [assembly: WebActivator.ApplicationShutdownMethod(typeof(WebBackgrounder.DemoWeb.App_Start.WebBackgrounderSetup), "Shutdown")]
    
    namespace WebBackgrounder.DemoWeb.App_Start
    {
        public static class WebBackgrounderSetup
        {
            static readonly JobManager _jobManager = CreateJobWorkersManager();
    
            public static void Start()
            {
                _jobManager.Start();
            }
    
            public static void Shutdown()
            {
                _jobManager.Dispose();
            }
    
            private static JobManager CreateJobWorkersManager()
            {
                var jobs = new IJob[]
                {
                    new SampleJob(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(20)),
                    /* new ExceptionJob(TimeSpan.FromSeconds(15)), */
                    new WorkItemCleanupJob(TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(5), new WorkItemsContext())
                };
    
                var coordinator = new WebFarmJobCoordinator(new EntityWorkItemRepository(() => new WorkItemsContext()));
                var manager = new JobManager(jobs, coordinator);
                manager.Fail(ex => Elmah.ErrorLog.GetDefault(null).Log(new Error(ex)));
                return manager;
            }
        }
    }
    

    但是我发现只使用我需要的 Webbackgrounder 部分更简单,如下所示。将此类放在 App_Start 文件夹中

    using System;
    using BombaySapphireCds.Jobs;
    using Elmah;
    
    [assembly: WebActivator.PostApplicationStartMethod(typeof(BombaySapphireCds.App_Start.PodMonitorConfig), "Start")]
    [assembly: WebActivator.ApplicationShutdownMethod(typeof(BombaySapphireCds.App_Start.PodMonitorConfig), "Shutdown")]
    
    namespace BombaySapphireCds.App_Start
    {
        public static class PodMonitorConfig
        {
            private static PodMonitorJob m_job;
    
            public static void Start()
            {
                m_job = new PodMonitorJob(TimeSpan.FromSeconds(20));
            }
    
            public static void Shutdown()
            {
                m_job.Dispose();
            }
        }
    }
    

    和做实际工作的班级......(把它放在你喜欢的任何地方)

    using System;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace BombaySapphireCds.Jobs
    {
        public class PodMonitorJob : IDisposable
        {
            private CancellationTokenSource m_cancel;
            private Task m_task;
            private TimeSpan m_interval;
            private bool m_running;
    
            public PodMonitorJob(TimeSpan interval)
            {
                m_interval = interval;
                m_running = true;
                m_cancel = new CancellationTokenSource();
                m_task = Task.Run(() => TaskLoop(), m_cancel.Token);
            }
    
            private void TaskLoop()
            {
                while (m_running)
                {
                    //
                    // Do monitoring work here.
                    //
    
                    Thread.Sleep(m_interval);
                }
            }
    
            public void Dispose()
            {
                m_running = false;
    
                if (m_cancel != null)
                {
                    try
                    {
                        m_cancel.Cancel();
                        m_cancel.Dispose();
                    }
                    catch
                    {
                    }
                    finally
                    {
                        m_cancel = null;
                    }
                }
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      这已成为 Web 上后台任务执行的新标准。这是一个 NuGet 包,名为 HangFire - https://github.com/HangfireIO/Hangfire。这些任务甚至在应用程序池回收之后仍然存在。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-26
        • 2018-01-12
        • 2017-12-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多