Saturday, September 6, 2014

How to Install a Custom Blogger Template

This article will provide a step-by-step guide on how to correctly apply a new xml Blogger template to your blog. This process can be tricky depending on several factors (quality of the code in the new template, your existing widgets, etc) so you need to be careful when changing your template. Don’t worry though, just follow these step-by-step instructions below and we’ll hopefully get you through it without much pain.

Sign in to your blogger blog and head to “design” => “Edit HTML”
Before uploading new template backup your previous template.
Now browse your computer and select and upload your desired template.
After this process, blogger will ask whether to keep or delete your widgets.
Now preview your blog and if everything is fine hit 'Save Template' button.
That's it, your new template is live now.

How to run Background Tasks in ASP.NET

A few years back Phil Haack wrote a great article on the dangers of recurring background tasks in ASP.NET. In it he points out a few gotchas that are SO common when folks try to do work in the background. Read it, but here's a summary from his post.
  • An unhandled exception in a thread not associated with a request will take down the process.
  • If you run your site in a Web Farm, you could end up with multiple instances of your app that all attempt to run the same task at the same time.
  • The AppDomain your site runs in can go down for a number of reasons and take down your background task with it.
If you think you can just write a background task yourself, it's likely you'll get it wrong. I'm not impugning your skills, I'm just saying it's subtle. Plus, why should you have to?
There's LOT of great ways for you to do things in the background and a lot of libraries and choices available.
Some ASP.NET apps will be hosted in IIS in your data center and others will be hosted in the Azure cloud. The spectrum of usage is roughly this, in my opinion:
  • General: Hangfire (or similar similar open source libraries)
    • used for writing background tasks in your ASP.NET website
  • Cloud: Azure WebJobs 
    • A formal Azure feature used for offloading running of background tasks outside of your Website and scale the workload
  • Advanced: Azure Worker Role in a Cloud Service
    • scale the background processing workload independently of your Website and you need control over the machine
There's lots of great articles and videos on how to use Azure WebJobs, and lots of documentation on how Worker Roles in scalable Azure Cloud Services work, but not a lot about how your hosted ASP.NET application and easily have a background service. Here's a few.

WebBackgrounder

As it says "WebBackgrounder is a proof-of-concept of a web-farm friendly background task manager meant to just work with a vanilla ASP.NET web application." Its code hasn't been touched in years, BUT the WebBackgrounder NuGet package has been downloaded almost a half-million times.
The goal of this project is to handle one task only, manage a recurring task on an interval in the background for a web app.
If your ASP.NET application just needs one background task to runs an a basic scheduled interval, than perhaps you just need the basics of WebBackgrounder.
using System;
using System.Threading;
using System.Threading.Tasks;
 
namespace WebBackgrounder.DemoWeb
{
    public class SampleJob : Job
    {
        public SampleJob(TimeSpan interval, TimeSpan timeout)
            : base("Sample Job", interval, timeout)
        {
        }
 
        public override Task Execute()
        {
            return new Task(() => Thread.Sleep(3000));
        }
    }
}

Built in: QueueBackgroundWorkItem - Added in .NET 4.5.2

Somewhat in response to the need for WebBackgrounder, .NET 4.5.2 added QueueBackgroundWorkItem as a new API. It's not just a "Task.Run," it tries to be more:
QBWI schedules a task which can run in the background, independent of any request. This differs from a normal ThreadPool work item in that ASP.NET automatically keeps track of how many work items registered through this API are currently running, and the ASP.NET runtime will try to delay AppDomain shutdown until these work items have finished executing.
It can try to delay an AppDomain for as long as 90 seconds in order to allow your task to complete. If you can't finish in 90 seconds, then you'll need a different (and more robust, meaning, out of process) technique.
The API is pretty straightforward, taking  Func<CancellationToken, Task>. Here's an example that kicks of a background work item from an MVC action:
public ActionResult SendEmail([Bind(Include = "Name,Email")] User user)
{
    if (ModelState.IsValid)
    {
       HostingEnvironment.QueueBackgroundWorkItem(ct => SendMailAsync(user.Email));
       return RedirectToAction("Index", "Home");
    }
 
    return View(user);
}

FluentScheduler

FluentScheduler is a more sophisticated and complex scheduler that features a (you guessed it) fluent interface. You have really explicit control over when your tasks run.
using FluentScheduler;
 
public class MyRegistry : Registry
{
    public MyRegistry()
    {
        // Schedule an ITask to run at an interval
        Schedule<MyTask>().ToRunNow().AndEvery(2).Seconds();
 
        // Schedule a simple task to run at a specific time
        Schedule(() => Console.WriteLine("Timed Task - Will run every day at 9:15pm: " + DateTime.Now)).ToRunEvery(1).Days().At(21, 15);
 
        // Schedule a more complex action to run immediately and on an monthly interval
        Schedule(() =>
        {
            Console.WriteLine("Complex Action Task Starts: " + DateTime.Now);
            Thread.Sleep(1000);
            Console.WriteLine("Complex Action Task Ends: " + DateTime.Now);
        }).ToRunNow().AndEvery(1).Months().OnTheFirst(DayOfWeek.Monday).At(3, 0);
    }
}
FluentScheduler also embraces IoC and can easily plug into your favorite Dependency Injection tool of choice by just implementing their ITaskFactory interface.

Quartz.NET

Quartz.NET is a .NET port of the popular Java job scheduling framework of the (almost) same name. It's very actively developed. Quartz has an IJob interface with just one method, Execute, to implement.
using Quartz;
using Quartz.Impl;
using System;
 
namespace ScheduledTaskExample.ScheduledTasks
{
    public class JobScheduler
    {
        public static void Start()
        {
            IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
            scheduler.Start();
 
            IJobDetail job = JobBuilder.Create<MyJob>().Build();
 
            ITrigger trigger = TriggerBuilder.Create()
                .WithDailyTimeIntervalSchedule
                  (s =>
                     s.WithIntervalInHours(24)
                    .OnEveryDay()
                    .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(0, 0))
                  )
                .Build();
 
            scheduler.ScheduleJob(job, trigger);
        }
    }
}
Then, inside your Application_Start, you call JobScheduler.Start(). There's a great getting started article on Quartz at Mikesdotnetting you should check out.

Hangfire

And last but definitely not least, the most polished (IMHO) of the group, Hangfire by @odinserj. It's a fantastic framework for background jobs in ASP.NET. It's even optionally backed by Redis, SQL Server, SQL Azure, MSMQ, or RabbitMQ for reliability.
The Hangfire documentation is amazing, really. Every open source project's document should be this polished. Heck, ASP.NET's documentation should be this good.
The best feature from Hangfire is its built in /hangfire dashboard that shows you all your scheduled, processing, succeeded and failed jobs. It's really a nice polished addition.
image
You can enqueue "fire and forget" jobs easily and they are backed by persistent queues:
BackgroundJob.Enqueue(() => Console.WriteLine("Fire-and-forget"));
You can delay them...
BackgroundJob.Schedule(() => Console.WriteLine("Delayed"), TimeSpan.FromDays(1));
Or great very sophisticated CRON style recurrent tasks:
RecurringJob.AddOrUpdate(() => Console.Write("Recurring"), Cron.Daily);
Hangfire is just a joy.
Check out the Hangfire Highlighter Tutorial for a sophisticated but easy to follow real-world example.
There's a rich ecosystem out there ready to help you with your background tasks. All these libraries are excellent, are open source, and are available as NuGet Packages.

Big Thanks to Scott Hanselman
Source: http://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx

Friday, September 5, 2014

How to Install WAMP Server on Windows 8.1

Introduction

This article explains how to easily install WAMP Server in Windows 8.1.

WAMP install issues

WAMP install issues 403 Forbidden
Download and Install WAMP Service using the following:

Step 1: Click "Next".

Click Next

Step 2: Click "Next".

Again Click Next

Step 3: Click "Next".

Click Next Again

Step 4: Click "Next".

Select Additional Task

Step 5: "Install"

Install

Step 6: "Yes"

Installing

Step 7: Click "Next".

Smtp Name

Step 8: "Finish"

Installing Finish

Step 9: WampServer red:

WampServer

Step 10:

Customize

Step 11: Changes in httpd.conf file open:

Changes in httpd conf file

Step 12: Click the Wamp server icon then seelct "Apache" > "httpd.conf open" and "Find Local host: 80"
httpd conf open

Step 13: Change the line "Local host: 8080"

Change the line Local host

Step 14: Change the line "Find Listen 80" to "Listen 8080"

change the line Find Listen

Step 15: Now restart all services.

restart all services

Step 16: wampserver open:

wampserver open

Step 17: Click "phpMyAdmin".

phpMyAdmin

Step 18: Type "http://localhost:8080/" or "http://localhost:8080/phpmyadmin/Error 403 Forbidden".

Error 403 Forbidden

Step 19: "C:\wamp\alias Open File phpmyadmin.conf"

Open File phpmyadmin

Step 20: Open the file "phpmyadmin.conf". Edit the "deny" keyword and change to "Allow Form All".

Edit deny keyword change

Step 21: Type "http://localhost:8080/" or "http://localhost:8080/phpmyadmin/".

Address

Step 22: "http://localhost:8080/phpmyadmin/"

WAMP Server installed

Now the WAMP Server is installed in Windows 8.1 I hope you enjoyed this article.