简体   繁体   English

HostingEnvironment.QueueBackgroundWorkItem和HostingEnvironment.RegisterObject之间的区别

[英]Difference between HostingEnvironment.QueueBackgroundWorkItem and HostingEnvironment.RegisterObject

Currently I am using HostingEnvironment.RegisterObject to run my background work in my MVC 5 app. 目前我正在使用HostingEnvironment.RegisterObject在我的MVC 5应用程序中运行我的后台工作。 Specifically I have, 具体来说,我有,

public class BackgroundWorker
{
    /// <summary>
    /// Runs a background task that is registered with the hosting environment
    /// so it is guaranteed to finish executing.
    /// </summary>
    /// <param name="action">The lambda expression to invoke.</param>
    public static void Run(Action action)
    {
        new IISBackgroundTask().DoWork(action);
    }

    /// <summary>
    /// Generic object for completing tasks in a background thread
    /// when the request doesn't need to wait for the results 
    /// in the response.
    /// </summary>
    class IISBackgroundTask : IRegisteredObject
    {
        /// <summary>
        /// Constructs the object and registers itself with the hosting environment.
        /// </summary>
        public IISBackgroundTask()
        {
            HostingEnvironment.RegisterObject(this);
        }

        /// <summary>
        /// Called by IIS, once with <paramref name="immediate"/> set to false
        /// and then again with <paramref name="immediate"/> set to true.
        /// </summary>
        void IRegisteredObject.Stop(bool immediate)
        {
            if (_task.IsCompleted || _task.IsCanceled || _task.IsFaulted || immediate)
            {
                // Task has completed or was asked to stop immediately, 
                // so tell the hosting environment that all work is done.
                HostingEnvironment.UnregisterObject(this);
            }
        }

        /// <summary>
        /// Invokes the <paramref name="action"/> as a Task.
        /// Any exceptions are logged
        /// </summary>
        /// <param name="action">The lambda expression to invoke.</param>
        public void DoWork(Action action)
        {
            try
            {
                _task = Task.Factory.StartNew(action);
            }
            catch (AggregateException ex)
            {
                // Log exceptions
                foreach (var innerEx in ex.InnerExceptions)
                {
                    Logger.Log(innerEx);
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
            }
        }

        private Task _task;
        private static readonly ILogger Logger = Loggers.Logger.Instance;
    }
}

usage, 用法,

 BackgroundWorker.Run(() => 
       BackGroundMethod();
 );// run this in a background thread

So, using HostingEnvironment.QueueBackgroundWorkItem has any benefit over HostingEnvironment.RegisterObject 因此,使用HostingEnvironment.QueueBackgroundWorkItemHostingEnvironment.RegisterObject有任何好处。

QueueBackgroundWorkItem is really just a glorified call to RegisterObject under the covers. QueueBackgroundWorkItem实际上只是对RegisterObject一个美化调用。 It handles converting the call to Stop into a CancellationToken that can be consumed by asynchronous work items, writing to the event log in the case of failures, and a few other bookkeeping items. 它处理将Stop调用转换为CancellationToken ,可以由异步工作项使用,在发生故障时写入事件日志,以及其他一些簿记项。 There's nothing terribly interesting about it. 它没有什么特别有意思的。 You can use Reflector or ILSpy to browse the source until http://referencesource.microsoft.com/ is updated with the 4.5.2 sources. 您可以使用Reflector或ILSpy浏览源代码,直到使用4.5.2源更新http://referencesource.microsoft.com/

If you're comfortable with the complexity of performing all the bookkeeping yourself, there's no reason you can't continue to use RegisterObject . 如果您对自己执行所有簿记的复杂性感到满意,那么您无法继续使用RegisterObject The new API is for consumers who want something a bit simpler. 新API适用于想要更简单一点的消费者。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HostingEnvironment.QueueBackgroundWorkItem - 澄清? - HostingEnvironment.QueueBackgroundWorkItem - Clarification? HostingEnvironment.QueueBackgroundWorkItem真的会延迟回收吗? - Does HostingEnvironment.QueueBackgroundWorkItem really delay recycling? HostingEnvironment.QueueBackgroundWorkItem中的事务范围超时 - Transaction scope in HostingEnvironment.QueueBackgroundWorkItem time out WCF 中带有 HostingEnvironment.QueueBackgroundWorkItem 的后台线程 - Background thread with HostingEnvironment.QueueBackgroundWorkItem in WCF .NET Core 中 HostingEnvironment.QueueBackgroundWorkItem 的替代解决方案 - Alternative solution to HostingEnvironment.QueueBackgroundWorkItem in .NET Core HostingEnvironment.QueueBackgroundWorkItem支持多个后台进程 - HostingEnvironment.QueueBackgroundWorkItem support for multiple background processs ASP.NET中的HostingEnvironment.QueueBackgroundWorkItem()用于小型后台任务 - HostingEnvironment.QueueBackgroundWorkItem() in ASP.NET for small background tasks 在一定时间后强制取消HostingEnvironment.QueueBackgroundWorkItem - Forcing HostingEnvironment.QueueBackgroundWorkItem to cancel after a certain time HostingEnvironment.QueueBackgroundWorkItem 永远不会在 azure 部署的 asp.net mvc 4.5 应用程序上执行 - HostingEnvironment.QueueBackgroundWorkItem never executes on azure deployed asp.net mvc 4.5 application Server.MapPath 和 HostingEnvironment.MapPath 有什么区别? - What is the difference between Server.MapPath and HostingEnvironment.MapPath?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM