Quantcast
Channel: All About ASP.NET and ASP.NET Core 2 Hosting BLOG
Viewing all articles
Browse latest Browse all 427

ASP.NET 4.5.2 Hosting in Saudi Arabia with ASPHostPortal.com :: What’s New in ASP.NET 4.5.2? – QueueBackgroundWorkItem

$
0
0

ASP.NET is built on the .NET framework, which provides an application program interface (API) for software programmers. The .NET development tools can be used to create applications for both the Windows operating system and the Web. You've probably heard the word ASP.net fairly often these days, especially on developer sites and news. This article will explain what the fuss is all about. ASP.NET is not just the next version of ASP; it is the next era of web development. ASP.NET allows you to use a full featured programming language such as C# (pronounced C-Sharp) or VB.NET to build web applications easily.

New version of ASP.NET, .Net 4.5.2 was released on May 5th. Starting with the recently released version 4.5.2 of the .NET Framework, ASP.NET now supports the HostingEnvironment.QueueBackgroundWorkItem which lets you queue background threads from within an ASP.Net web application.

Well, the new HostingEnvironment.QueueBackgroundWorkItem method that lets you schedule small background work items. ASP.NET tracks these items and prevents IIS from abruptly terminating the worker process until all background work items have completed. These will enable ASP.NET applications to reliably schedule Async work items.

Remember that the QueueBackgroundWorkItem can only be called inside an ASP.NET managed app domain. It won't work if the runtime host is either Internet Explorer or some Windows shell. This is useful for long running tasks that don’t need to complete before returning a response to the user.

 using System;
 using System.Diagnostics;
 using System.Threading;
 using System.Threading.Tasks;
 using System.Web.Hosting;
 using System.Web.Mvc; 
 namespace QueueBackgroundWorkItem.Controllers
 {
    public class HomeController : Controller
    {
        // notice that the action did need to be declared async
        public ActionResult Index()
        {
            Func<CancellationToken, Task> workItem = DelayWrite;
            HostingEnvironment.QueueBackgroundWorkItem(workItem);
             // the view is returned before the work item is complete
           return View();
        } 
        // Background work items should use async/await to avoid tying up IIS threads
        // the runtime will provide the cancellation token
        private async Task DelayWrite(CancellationToken cancellationToken)
        {
            // perform a long running operation, e.g. network service call, computation, file IO
            await Task.Delay(5000);
            Trace.WriteLine("Executed from a background work item");         
        }  
    }
 }

Viewing all articles
Browse latest Browse all 427

Trending Articles