Run background tasks in aspnet applications with dotnet Framework 4.5.2

May 13, 2014 -

Few days ago Microsoft has announced the availability of the .NET Framework 4.5.2. This new version brings some new features and improvements to ASP.NET. It is now very easy to run some background tasks from an ASP.NET Web app, using the new HostingEnvironment API. It allows to enqueue background tasks (as simple as working with the thread pool) and avoids IIS app pools shutdown until the tracked tasks are completed.

For a complete description of this new .NET Framework version, go to this page. You can find the runtime and developer pack installers here.

Once you’ve installed .NET Framework 4.5.2 and the developer tools, open Visual Studio and create a new web application project that targets the .NET Framework 4.5.2 :


In this post, I have choose to use the HostingEnvironment api to queue thumbnail generation when the user uploads a picture (really simple scenario). The HostingEnvironment class defines a method QueueBackgroundWorkItem that takes an Action<CancellationToken> or a Func<CancellationToken,Task> as parameters.

To generate the thumbnail I have used the System.Drawing API and created the following simple helper :

public class ThumbnailHelper
    public static Task CreateThumbnail(string filePath)
        return Task.Factory.StartNew(() =>
            Image originalImage = Bitmap.FromFile(filePath);

            int thumbWidth, thumbHeight;

            thumbWidth = 250;
            thumbHeight = (originalImage.Height * 250) / originalImage.Width;

            Image thumbnail = originalImage.GetThumbnailImage(thumbWidth, thumbHeight, null, IntPtr.Zero);

            string thumbFileName = string.Format("{0}_thumb{1}", Path.GetFileNameWithoutExtension(filePath), Path.GetExtension(filePath));
            string thumbFilePath = Path.Combine(Path.GetDirectoryName(filePath), thumbFileName);


The cshtml code :

    ViewBag.Title = "Upload a new picture";


@using(Html.BeginForm("Upload", "Picture", FormMethod.Post, new{ enctype = "multipart/form-data"}))
    <div class="form-group">
        <label for="file">Choose a picture</label>
        <input type="file" name="file" id="file" />
    <div class="form-group">
        <input type="submit" value="Upload and create thumbnail" class="btn btn-primary"/>

and the controller code :

public class PictureController : Controller
    public ActionResult Upload()
        return View();

    public ActionResult Upload(HttpPostedFileBase file)
        var appDataPath = Server.MapPath("~/App_Data");
        string filePath = Path.Combine(appDataPath, file.FileName);

        HostingEnvironment.QueueBackgroundWorkItem(cancellationToken =>
            return ThumbnailHelper.CreateThumbnail(filePath);

        return RedirectToAction("Upload");

As you can see in the code above, just after that the picture is saved in the App_Data folder, a task is queued using the HostingEnvironment api. Now, you can be sure that the app pool of your application will not stop until all the thumbnails are generated.

Hope this helps Winking smile






#Docker Container Images are now out for #WindowsServer version 1709!

October 20, 2017 16:42

Getting started with #Traefik and #Kubernetes using #Azure Container Service

October 20, 2017 16:40

What’s new in the #Windows 10 Fall Creators Update

October 17, 2017 15:36

#DockerCon: #Kubernetes orchestration also comes with the latest beta of #Docker for Mac and Windows

October 17, 2017 09:13

RT @sferquel: Docker supports the real #kubernetes orchestrator. So it does not lock you in. Kubectl with docker ee ? Yes we can ! #DockerC

October 17, 2017 09:11

#DockerCon: @Docker Platform and #Moby Project add #Kubernetes

October 17, 2017 09:07

Whoa! Big announcement today at #DockerCon: Extending #Docker Enterprise Edition to Support #Kubernetes

October 17, 2017 09:04

#Nodejs dev? Learn how to host your apps on #Azure with @John_Papa

October 17, 2017 06:28

Cloud Service Map for AWS and Azure Available Now

October 17, 2017 06:19

Check this video to discover how to use #Jenkins Plugins for #Kubernetes w/ @Azure #ContainerService

October 17, 2017 06:06

Edge Device Discovery - an Unfinished Project

October 17, 2017 05:57

Zip Push Deployment for #Azure Web Apps, #Functions and #WebJobs

October 17, 2017 05:41

Migration tips from IaaS to Web App for #Containers on @Azure

October 16, 2017 08:14

Microsoft Cognitive Services - How Content Moderator helps to boost online safety

October 16, 2017 08:13

@mandinette77 Il a plutôt parlé d'info sorties de leurs contexte avec comme exemple Twitter. Ce tweet en est une belle illustration btw :-)

October 15, 2017 22:20