open-source

Scheduling Background Tasks in ASP.NET using Quartz.NET

August 27, 2015 ASP.NET, ASP.NET vNext, Visual Studio , ,

 

Ever wondered how we can have critical tasks (not just sending out newsletters, tweeting, etc.) scheduled as background tasks our an ASP.NET website? This post outlines a quick way to use an open-source tooling to schedule background tasks in not more than 3 steps.

For scheduling background tasks, I recommend using one of the two open-source libraries – Quartz or Hangfire.

If we don’t need any monitoring, are a fan of zero/minimal configuration, don’t have any database or cache, etc. we can choose Quartz otherwise we can use Hangfire. In this post, we will use Quartz as a scheduling framework. To start with, we need to add reference of the NuGet package ‘Quartz’ to our Web Application by typing following command in Package Manager Console (or we can also use the UI in Visual Studio)

Install-Package Quartz

Now, there are 3 steps to adding a background tasks

 

Defining a Job

The beauty about Quartz is that it allows us to define the job in a separate class which ensures right abstraction and encapsulation. So let’s define a MaintenanceJob that inherits the interface IJob which has one member Execute. The method defines the action to be performed when the scheduled job is triggered

using Quartz;
namespace Tasks
{
    public class MaintenanceJob : IJob
    {
       public void Execute(IJobExecutionContext context)
       {
            var ftpLocation = context.JobDetail.JobDataMap.Get("ftp.location");
            // zip old log files
            // delete the files once zip is completed
            // sftp the zip to another location
        }
    }
}

Here, the IJobExecutionContext provides us the context of the job like – JobRunTime, PreviousFireTimeUtc, NextFireTimeUtc, Trigger, JobDetail, etc. JobDetail can be set from Trigger or Scheduler as additional data passed to the scheduled jobs.

Defining a Job Trigger

 

Trigger defines when a Job should be executed. There are different types of triggers – Time based, CRON, Calendar, Schedule, etc.

We want to run this MaintenanceJob once daily (at interval of 24 hours) at midnight. So our trigger definition will look like,

        public static ITrigger TimeTrigger()
        {
            return TriggerBuilder.Create()
               .WithDailyTimeIntervalSchedule
                 (s =>
                    s.WithIntervalInHours(24)
                   .OnEveryDay()
                   .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(0, 0))
                 )
               .Build();
        }

 

When we use Time Interval Schedule, we have many options to schedule this job on weekdays OnMondayThroughFriday or on weekends OnSaturdayAndSunday, every minute/second, etc. If we were to define the trigger using CRON expressions, our trigger will look like,

        public static ITrigger CRONTrigger()
        {
            return TriggerBuilder.Create()
               .WithCronSchedule("0 57 23 ? * *", s=> s.WithMisfireHandlingInstructionDoNothing())
               .Build();
        }

When we use CRON based trigger, we can use CRON expressions to define interval and can also actions to handle failures, etc.  The above expression will run the job every day at 23:57 (server time)

 

Defining a Job Schedule

A scheduler acts as an entry point and definition for all Quartz jobs. So we need to derive a correlation between the job we created and the trigger we defined. This scheduler should be called in the Application Start of our ASP.NET website (Webforms or MVC). Now we can either write the below code directly in Application_Start method in Global.asax or we can structure it in a new class called JobScheduler

    public class JobScheduler
    {
        public static void Start()
        {
            IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
            scheduler.Start();

            IJobDetail job = JobBuilder.Create<MaintenanceJob>().Build();
            job.JobDataMap.Add("ftp.location", "ftp://SomeFileLocation");
            scheduler.ScheduleJob(job, Triggers.TimeTrigger());
        }
    }

In the above code, we are using a scheduler, defining (not creating an object) MaintenanceJob using JobBuilder and associating MaintenanceJob with the TimeTrigger trigger. Next, we need to have this JobScheduler.Start method called when the application starts (i.e. at every IIS reset)

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            JobScheduler.Start();
        }

 

If we need to run tasks on a schedule as part of our ASP.NET application with reliable accuracy and without multiple Batch Scripts or PowerShell scripts running as Scheduled Tasks, choose Quartz. The code is also available as Gist on GitHub

dI.Hook – Creating a dependency injection container

September 9, 2012 Applications, CSharp, Open Source, Visual Studio , , , ,

If you are new to dI.Hook, please visit the di.Hook Product Page for Overview, Release Version and Source Code.

Some posts you might want to read would be:

  1. How to create a HookRepository?
  2. How to add hooks to HookRepository?
  3. How to add hooks via configuration file to a HookRepository?
  4. How to invoke all hooks using dI.Hook?
  5. How to invoke hooks that satisfy a condition?
  6. How to retrieve an object of hook?
  7. How to remove hooks from HookRepository?
  8. How to ignore hook on specific method?

So this is the ninth article in the series which will deal with creating a dependency injection container and using them in your applications

Creating a dependency container is very simple.  You can either choose a default implementation of Container, or you can implement the interface IContainer and write your own definition of container.    The implementation you would require to implement would be

  1. public interface IContainer
  2. {
  3.     ReadOnlyCollection<dIHook.Objects.ContainerItem> Collection { get; }
  4.     ReadOnlyCollection<Guid> Keys { get; }
  5.     void Register<TClass>() where TClass : class;
  6.     void Register<TClass>(Guid key) where TClass : class;
  7.     void Register<TInterface, TImplementation>() where TImplementation : class, TInterface;
  8.     void Register<TInterface, TImplementation>(Guid guid) where TImplementation : class, TInterface;
  9.     void Register<TInterface>(Guid key, TInterface objectValue);
  10.     void Register<TInterface>(TInterface objectValue);
  11.     TInterface Resolve<TInterface>() where TInterface : class;
  12.     TInterface Resolve<TInterface>(Guid key) where TInterface : class;
  13.     ReadOnlyCollection<Type> Types { get; }
  14. }

 

Assuming that you are not implementing this interface and are using the default implementation provided with dI.Hook, you would involve having

  1. Some business interfaces and entities
  2. Container having a definition of interface and implemented entities
  3. Container resolving these entity objects

For the sake of understanding, let’s have a eCommerce case study where we have 3 interfaces –

  • ICustomer – Represents customer and his order
  • INotifier – Notification service for the customer
  • IBillingProcessor – Processor that takes the payment type and processes the billing

So let’s quickly set the stage with these interface and their implementation

  1. public interface IBillingProcessor
  2. {
  3.     void ProcessPayment(OnlineOrder order);
  4. }
  5. public class BillingProcessor : IBillingProcessor
  6. {
  7.     private PaymentType _paymentType;
  8.     public BillingProcessor(PaymentType paymentType) {}
  9.     public void ProcessPayment(OnlineOrder order) { }
  10. }
  11. public enum PaymentType { Cash,CreditCard}
  12. public interface ICustomer
  13. {
  14.     void UpdateCustomerOrder(int customerId, string product);
  15. }
  16. public class InternetCustomer : ICustomer
  17. {
  18.     public InternetCustomer() { }
  19.     public void UpdateCustomerOrder(int customerId, string product) { }
  20. }
  21. public interface INotifier
  22. {
  23.     void SendReceipt(OnlineOrder order);
  24. }
  25. public class EmailNotifer : INotifier
  26. {
  27.     public EmailNotifer() { }
  28.     public void SendReceipt(OnlineOrder order) { }
  29. }

       

      Now, let’s use the container with these business entities

      1. Container container = new Container();
      2. container.Register<IBillingProcessor, BillingProcessor>();
      3. container.Register<ICustomer, InternetCustomer>();
      4. container.Register<INotifier, EmailNotifer>();

       

      The above code creates a default objects of the BillingProcessor, InternetCustomer and EmailNotifier.  If you want to pass your own object you could write as below (see BillingProcessor object)

      1. Container container = new Container();
      2. BillingProcessor billingProcessor = new BillingProcessor(PaymentType.CreditCard);
      3. container.Register<IBillingProcessor>(billingProcessor);
      4. container.Register<ICustomer, InternetCustomer>();
      5. container.Register<INotifier, EmailNotifer>();

       

      Now let’s place an order on an eCommerce website using this container.  An eCommerce class would be of no use without a reference to ICustomer, IBillingProcessor and INotifier.  So eCommerce class for this example would be

      1. public class ECommerce
      2. {
      3.     public void Process(OnlineOrder order)
      4.     {
      5.         _BillingProcessor.ProcessPayment(order);
      6.         _Customer.UpdateCustomerOrder(order.CustomerId,
      7.             order.Product);
      8.         _Notifier.SendReceipt(order);
      9.  
      10.         Debug.WriteLine("Process called");
      11.     }
      12.  
      13.     public ECommerce(IBillingProcessor billingProcessor,
      14.                 ICustomer customer,
      15.                 INotifier notifier)
      16.     {
      17.         _BillingProcessor = billingProcessor;
      18.         _Customer = customer;
      19.         _Notifier = notifier;
      20.  
      21.     }
      22.  
      23.     IBillingProcessor _BillingProcessor;
      24.     ICustomer _Customer;
      25.     INotifier _Notifier;
      26. }

       

      So your eCommerce takes objects of ICustomer, IBillingProcessor and INotifier in the constructor and in the Process method, it

      1. Calls BillingProcessor to ProcessPayment for the order
      2. Calls Customer to UpdateCustomerOrder
      3. Calls Notifier to send notification

      Now let’s see how this can be invoked using dI.Hook container that we created

      1. OnlineOrder onlineOrder = new OnlineOrder()
      2. {
      3.     CustomerId = 12212,
      4.     EmailAddress = "[email protected]",
      5.     Price = 400,
      6.     Product = "NewProduct"
      7. };
      8. ECommerce commerce = container.Resolve<ECommerce>();
      9. commerce.Process(onlineOrder);

      The above code creates an object of ECommerce class.  Now what is important here is to see what internally happens.  When you call Resolve method, it tries creating an object of ECommerce class.  It finds that its constructor requires objects of IBillingProcessor, ICustomer and INotifier.  Now since you have already registered objects of these interfaces the dI.Hook Container retrieves the object from the collection and then will try to create an object of ECommerce.  If, however, ECommerce constructor required an un-registered object it will try to create a default object and add it in the container.

      So if you explicitly created an object of BillingProcessor with payment type as CreditCard and added them in Container, it will use that object; otherwise, it will use default object of BillingProcessor with payment type as Cash

      dI.Hook Container ensures that it resolves the hierarchical dependencies automatically.  However if it is unable to resolve for any reason, it will throw an exception.

      Must-have Tools on Windows – Part 2 of 2

      March 7, 2012 CSharp, Open Source, Silverlight, Windows Phone , , , ,

      Every technologist has his favourite list of developer tools, applications and OS which they believe are indispensible and without them they would not be able to develop anything.  With time and changing focus, this list keeps changing.  Here’s my list of tools that I think you as a developer should have on your laptop.

      Most of them are Open-Source or Freeware as I promote using Open-Source (and donating them) than buying products at premium price.  So it is possible that many excellent products have not made it to my list!

      Must have Tools on Windows – Part 1 of 2 – Visit here

       

      Source Control and Versioning (contd)

       

      I would like to continue with the last section on Source Control of Part 1 of this article. 

      Until the announcement of TFS “Express”, I was preferred using SubVersion and Mercurial but with I think TFS Express will be a game changer.  It would be an ideal source control system for teams smaller in size.  But until Microsoft launches it officially, I would continue with Subversion and Mercurial

      Windows Shell TortoiseSVN for Subversion and TortoiseHG for Mercurial

      Visual Studio PluginAnkhSVN for Subversion and VisualHG for Mercurial

      Defect Management CollabNet allows collaboration of SVN repository with defect management tools such as JIRA, HP Quality Centre, and many more.  You can use it with Visual Studio or Eclipse as a plugin

       

      Microsoft .NET development tools (contd)

       

      SharpDevelop – If you don’t want to use Visual Studio Express editions then you can give a try to SharpDevelop.

      CoffeeScript Compiler – If you are building Web Applications and are not familiar/used to writing JavaScript (or jQuery), you should give a try to CoffeeScript.  You can create complex JavaScript files easily using CoffeeScript and its compiler.

      Storm – Other than SoapUI, Storm is a perfect tool to test cross-platform Web Services.

      CruiseControl – There are several continuous integration software available in the market, but truly CruiseControl is the leader due to some obvious reasons (read: free, no-cost, no-royalty).  You can download the CCTray to get real-time build status of your projects.

      SilverlightSpy – I have tried the Free-Edition and I must admit – If you are a Silverlight or Windows Phone developer (or designer), you must download and use this tool.

      Tombstoning for Windows Phone – Tombstoning is a an undocumented yet a required feature of every Windows Phone application.  This library is the easiest way to manage tombstoning.

      Nuget – If you are a .NET developer, having Nuget and Nuget Package Explorer are an absolute necessary tools.  You can explore various open-source frameworks/tools/packages available in the market

      CInject – CInject is my contribution to Open-Source fraternity.  CInject allows code injection into any managed assembly without disassembling and recompiling it. It eases the inevitable task of injecting any code in single or multiple methods in one or many assemblies to intercept code for almost any purpose.  Read more here

      StyleCop – StyleCop analyses C# source code to enforce a set of style and consistency rules. It can be run from inside of Visual Studio or integrated into an MSBuild project. StyleCop has also been integrated into many third-party development tools.

       

      Database and Tools

       

      MySQL – Almost all my blogging websites use MySQL as a backend.  It is light, fast, free and is most commonly available with all Website hosting packages.

      Microsoft SQL 2008 Express and Compact – The express edition of SQL 2008 is really good unless you need over 4GB of data storage

      Sterling – NoSQL database for Silverlight and Windows Phone platforms

      Neo4j Community Edition – A graph-based, high performing, NoSQL database that is now widely accepted in several organizations

      MongoDB – A document-storage based, scalable, high-performance NoSQL database

      HeidiSQL – HeidiSQL is a lightweight, Windows based interface for managing MySQL and Microsoft SQL databases. It enables you to browse and edit data, create and edit tables, views, procedures, triggers and scheduled events. Also, you can export structure and data either to SQL file, clipboard or to other servers.

       

      Other Windows Tools

       

      Audacity – A perfect sound/video editor for your webcasts and recordings.  It can convert your audio files into various formats.

      HyperCam – Screen activity and voice recorder that is an ideal for recording your webcasts.

      FlashGet – An ad-free and fast download manager, FlashGet supports HTTP, HTTPS, FTP downloads

      BullzipPDF – A PDF printer is something every person (not just a developer) would dream of.  Bullzip is one of the best PDF printers that allows you to print, merge, apply watermarks, encrypt with password and many more features.

      HandBrake – HandBrake is multiplatform, multithreaded video transcoder

      PortableApps – Carry your favourite applications on a portable or a cloud drive and make it work on any PC, any make.

      FreeMind – FreeMind is a premier free mind-mapping software written in Java. The recent development has hopefully turned it into high productivity tool.

      myTracker – myTracker is a time-tracking application.  The inevitable, effort-driven process of tracking time and analyzing is eased by this small-memory-footprint application.  The application generates reports in Microsoft Excel (.xls) format and provides pie charts and bar graphs depicting time usage, utilization.

      Eraser – Eraser is an advanced security tool for Windows which allows you to completely remove sensitive data from your hard drive by overwriting it several times with carefully selected patterns.

      PureText – Have you ever copied some text from a web page or a document and then wanted to paste it as simple text into another application without getting all the formatting from the original source? PureText makes this simple by adding a new Windows hot-key (default is WINDOWS+V) that allows you to paste text to any application without formatting.

      EaseUS Partition Manager Home Edition – Free Partition Magic alternative, free hard drive partition manager and disk management utility for Windows 7 SP1 and dual boot Windows 7 and Windows 8 users.

      CrucibleWDS is a free Windows imaging / deployment / cloning program. It supports cloning Windows XP, Vista, and 7. A free Ghost alternative. Free Ghost software. Supports unicast and multicast. Can be used with 1 computer or 1000’s. Uses a simple web interface to deploy clients, clients can also be imaged directly from a boot menu by selecting an image from a pxe menu.

       

      So in case you believe there is any Free / Open-Source tool that has been missed in my list, post it in the comments.  I will be happy to include it in my list!

      Must-have Tools on Windows – Part 1 of 2

      February 19, 2012 CSharp, Open Source, Silverlight, Visual Studio, Windows Phone , ,

      Every technologist has his favourite list of developer tools, applications and OS which they believe are indispensible and without them they would not be able to develop anything.  With time and changing focus, this list keeps changing.  Here’s my list of tools that I think you as a developer should have on your laptop.

      Most of them are Open-Source or Freeware as I promote using Open-Source (and donating them) than buying products at premium price.  So it is possible that many excellent products have not made it to my list!

      Windows Tools

      Fences – helps you organize your desktop and can hide your icons when they are not in use.  You can resize your fences (read areas) to keep your desktop clean and organized

      Dell Dock – bring greater organization, personalization and productivity to Dell customers around the globe.  You can use it not just on Dell laptops/desktops, but on any make.

      7-Zip – An Open-Source file archiver with high compression ratio.  There is a commercial license for organizations but you don’t need to pay anything for that too.

      Paint.NET – free image and photo editing software for computers that run Windows. This program is a very good alternative to Photoshop for developers (if not for professionals).

      GIMP – Photo retouching, image composition and image authoring in multiple formats is made very easy using GIMP.

      SysInternals – a service that enables you to execute Sysinternals tools directly from the Web without hunting for and manually downloading them. The one I used more is Process Explorer to get insight on internals of an executable.

      Windows Live Writer – This is by-far the best and free tool for Blogging on various platforms with a great plugin support from community.

      WinDirStat – Disk usage statistics viewer and clean up tool for Windows to analyse and free-up space.

      CCleaner – Registry cleaner and makes your computer faster. There is one free version and paid versions start from £19

      VirtualBox – If you have read my post on running Android OS on Windows, you would be aware of what VirtualBox allows you to do. It is one of the best VHD host and manager allowing you to create image of almost any OS and run it on Windows

       

      Online Meetings, Webinars and Collaboration

       

      AnyMeeting – Hold large or small meetings and web conferences.  This is one of my favourites allowing me schedule and manage my webinars.  You can pay $17pm if you want an Ad-free version, else it’s free.

      TeamViewer – A very handy tool for Remote Access and Support over Internet.  You can hold conferences for up to 25 participants, or conduct training sessions

      Skype – One of the most used video conferencing tools across globe does not need any introduction.

      Skydrive – 25GB of online space!  Only Microsoft could give you that for free. You can sign up on Windows Live and get access to an integrated eco-system of Skydrive, Live Messenger, Hotmail, Calendar, Contacts, XBOX games, Windows Phone and now Windows 8 OS.

      DropBox – free service that lets you bring your photos, docs, and videos anywhere and share them easily on platforms such as Desktop, Windows Phone, Android and iPhone.  For the first year DropBox is free, and subsequent years there is a minimal fee.

      ZoomIt – screen zoom and annotation tool for technical presentations runs unobtrusively in the tray and activates with customizable hotkeys to zoom in on an area of the screen.  I have used it in many presentations on Windows Phone, Silverlight, Performance Engineering and have found it really handy!

       

      Microsoft .NET developer tools

       

      Microsoft Visual Studio 2010 Express Editions – Unless you have MSDN license or a authorised copy of Visual Studio 2010, these express editions are really MUST-TO-HAVE products.

      Notepad++ – A great text-editor with Explorer context-menu and a IDE-like experience.  If you are using TextPad or Notepad, I would highly recommend downloading this

      FileZilla – A fast and reliable cross-platform FTP, FTPS and SFTP client with lots of useful features and graphical user interface

      WebMatrix – A free web development tool from Microsoft that includes platforms such as WordPress, Razor (ASP.NET MVC3) and other open-source blogging, wiki tools.

      LINQPad – interactively query databases in a modern query language LINQ using this tool instead of using SQL Management Studio 

      JustDecompile – new, free developer productivity tool for easy .NET assembly browsing and decompiling and a complete replacement of .NET Reflector (since its no more free).  You can also try ILSpy

      SublimeText – If you are a Web-developer you must try the evaluation version of this tool.  It is really very intuitive, prose and has a slick user interface with extraordinary features.

      Fiddler – Easy, clean and powerful web-traffic debugging proxy for HTTP

      SoapUI – This tool defines a perfect way to do functional testing using graphical interface to create automated functional, load or regression tests.  If you want to record user tests and generate some test code then you should try WatiN

      FireBug – Most powerful in-browser IDE on Mozilla Firefox allowing you to browse through HTML, CSS and Javascript.  If you want to check your web pages performance, you might want to try out YSlow by Yahoo

      NCrunch – Automated continuous testing within Visual Studio.NET that runs in background in your IDE and get information such as Code Coverage and Performance metrics.

      KDiff3 – is an alternative to BeyondCompare to compare files, directories and automate merging of code.

      HelpNDoc – Is a really great tool to generate documentation in PDF, Web-based, CHM, Word and iPhone for personal use

      LogParser – Log parser is a powerful, versatile tool that provides universal query access to text-based data such as log files, XML files and CSV files, as well as key data sources on the Windows® operating system such as the Event Log, the Registry, the file system, and Active Directory

      Source Control and Versioning

      I prefer using Subversion and Mercurial for source control and versioning. So here’s the list

      Windows Shell – TortoiseSVN for Subversion and TortoiseHG for Mercurial

      Visual Studio Plugin – AnkhSVN for Subversion and VisualHG for Mercurial

       

      That’s just half of the list, so stay tuned for the next part where tools for Silverlight, Windows Phone, XAML, Web-sharing would be the focus!

      Follow on Feedly