Quartz.net Schedulling Jobs

Introduction:

Sometimes when you develop an application, you are going to face some tasks needs to perform a certain action at a given time without any user interaction, example for that a User wants to set an alert in specific time to remind him with a date Or something else like updating records in database periodically. All this tasks needs non-human interaction, so how could you tell the machine to do this job ? that’s the question. Cron Job is the solution for this tasks. Quartz.net has presented an excellent open source job scheduler that can be used for these purposes.

What is Quartz ?

Quartz is a job scheduling system that can be integrated with, or used along side virtually any other software system. The term “job scheduler” seems to conjure different ideas for different people. As you read this tutorial, you should be able to get a firm idea of what we mean when we use this term, but in short, a job scheduler is a system that is responsible for executing (or notifying) other software components when a pre-determined (scheduled) time arrives.

Job , Schedule & Trigger:

At the beginning we have to recognize how the Quartz works. Jobs are Scheduled to run when a given Trigger occurs. Triggers can be created with nearly any combination of the following directives:

  • at a certain time of day (to the millisecond)
  • on certain days of the week
  • on certain days of the month
  • on certain days of the year
  • not on certain days listed within a registered Calendar (such as business holidays)
  • repeated a specific number of times
  • repeated until a specific time/date
  • repeated indefinitely
  • repeated with a delay interval

Quartz in Action:

Let’s go inside the code and see how Quartz works. First we will create a new Console application and get Quartz.net from Nuget Packages.

Now create a new class let’s name it MyJob, this class will be the job that we want it to be done in specific time, as we mention above it has to inherit from IJob interface.

Back to the Program.cs class in the Main method we are going to Schedule our Job with a Trigger as we said above.

Let’s explain our code. in the first two lines of code we are instantiating the Schedule which will going to register our job in it.

After that we will specify what time we want this job to run. The DateBuilder class is responsible to create our DateTimeOffset which will meet our need.

This line means that we want our Job to run at the beginning of the next minute. And now we have to build our Job by using JobBuilder class.

Here we are building a job and specify a name for this job – “job1- ” and group name – “group1”.

JJobs and Triggers are given identifying names as they are registered with the Quartz.NET scheduler. Jobs and triggers can also be placed into ‘groups’ which can be useful for organizing your jobs and triggers into categories for later maintenance. The name of a job or trigger must be unique within its group – or in other words, the true identifier of a job or trigger is its name + group. If you leave the group of the Job or Trigger ‘null’, it is equivalent to having specified SchedulerConstants.DefaultGroup.

After that we have to create Trigger for this job by using TriggerBuilder class.

Here we are saying to the trigger to start this job at the time which we were specifying before.

And then just we will schedule our job and start it. Notice that there is a line of code

this line is just to ensure that a minute will pass, because in our example we want the job to run at the beginning of the next minute. So this line is related to our example not always required.

At last we shut down the schedule. This was a simple example for Quartz.net, I’ll let you discover the whole features in this great library.

References

Quartz.net

FAQs

Download Sample

 

Using Signalr with Asp.net MVC to create simple chat application part II

As we mentioned in the previous example here, we can create a simple asynchronous actions with the signalr library like chat, real-time streaming and so on. in this blog i am going to go with another library from signalr its PersistentConnection. A PersistentConnection is the base class that has an api for exposing a SignalR service over http.

we are going to complete in the the previous application we created

Step 1:Add AdvChatController

we are going to create a new controller called AdvChatController with two action methods Index and Adv. then add index view

Step 2: Create Signalr PersistentConnection

Create a class the derives from PersistentConnection

Step 3:Setup Routing

Make a route for your connection in Global.asax

If you are setting up other routes, the SignalR mapping needs to be done before the other ones.

Step 4: Adding receiving functionality in MyConnection

Add the following functions to MyConnection class

here as you notice we can manage the users that are connected to server first on connect action that action will be called when the user connects to the application so we can add the user to a certain group also on disconnect action that action will be called when user leaves the page so we can remove the user from group. also on received function to perform the sending action to certain group

step 5: Adding Client code

Append javascript references to the head of the page as follows

Append the following javascript

Append the following Html to your page

that’s all you can now run the application now in multiple browsers and test it

You can download the sample from here

 

Using Signalr with Asp.net MVC to create simple chat application

In this tutorial I am going to talk about creating a simple chat application using signalr library with asp.net MVC. If we are trying to do a chat application or any asynchronous actions with client so we have a lot of work to do using ajax and javascript. Now with signalr it’s so simple and easy.

Step 1: Create an Empty Asp.Net MVC 3 Application

Go to File –> New Project –> Web –> Asp.Net MVC 3 Application (Make sure it’s .Net Framework 4).

Step 2: Add SignalR Package

Right click on references and click Manage NuGet Packages. Search for SignalR and click install. Or use NuGet package manager concole and install the SignalR package using

Step 3: Add HomeController

Right click on Controllers folder and click Add –> Controller. Then add index view to index action result by right click on index action method –> add view.

Step 4: Create Signalr Hub

Create a class that derives from Hub

Step 5: Adding Client

Append javascript references to the head of the page as follows

As you can notice there is a /signalr/hubs file included but its not in the project because its dynamically generated file in run time

Append the following javascript

I added commented lines above each line to declare how it works.

Append the following Html to your page

And that’s it you can run the application now in multiple browsers and all magic starts

You can download a sample from here

 

Login via Twitter inside your application step by step

In this blog post i am going to explain how to login via twitter inside your web application step by step

Introduction:

  • Twitter uses OAuth to provide authorized access to its information and data which gives
    • Secure – Users are not required to share their passwords with 3rd party applications, increasing account security.
    • Standard – A wealth of client libraries and example code are compatible with Twitter’s OAuth implementation.

Implementation:

  1. Create twitter application in order to connect to twitter with
    1. Go to Create a new application in twitter
    2. After creation completed successfully twitter will redirected to the main application page the most important information is “Consumer key”,”Consumer secret”
  2. Create a new MVC application “if you have already existing application skip this step”.
  3. Open Application Web.config file and add to appSettings section the following
  4. Make sure that Authentication section is configured like that
  5. I’ve created three controllers “if you have already existing application just add the code in the appropriate place in your application”
    1. AuthController: To perform all authentication actions including normal login and login via twitter.
    2. HomeController: Landing page for the application with no Authentication
    3. AccountController:include pages that requires authorization
  6. At AuthController we will create all twitter login functionality
    1. First we need to create loginToTwitter() method which check if the current request is coming from twitter callback or its calling the authentication
    2. if user is authenticated we have to get AccessTokenForTheFirstTime
    3. if user request authorization so we have to redirect user to authorization page in twitter
    4. make sure that after Authorization you have to redirect user again using TwitterCallbackAddress that we specified before in web.config here i am redirecting the user to Auth/LoginWithTwitter action method
    5. then we link that action method with a button in our view
      Explanation: first the user will go to action method LoginWithTwitter for the first time with no “oauth_token”,”oauth_verifier” so user will redirected to twitter login page according to LoginToTwitter function then returns again to the same action method LoginWithTwitter according to the link we specifie in web.config but this time with “oauth_token” and “oauth_verifier” so user will be authenticated according to LoginToTwitter function through getting GetAccessToken function if user authenticated successfully Twitter API will return OAuthTokenResponse in this step we can set user as authenticated user and with getting user information from twitter using GetsTwitterUser function and redirect to AccountController
  7. At AccountController make sure that the controller is Authorized to allow only authorized users to see its content
  8. at Index page in Account use ViewBag To view user Twitter Screen Name

You can download the full sample from here