Friday, September 5, 2014

Wow ! Background Task in Separate thread in Asp.net web form

Its unfortunate that I never used threading to that extent as most of my work involved in web platform with very little to do with threading and task parallelism. Still there are cases where this Background Task can come handy. Now it depends.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.IO;

using System.Threading;



 
 
public partial class _Default : System.Web.UI.Page



{
 
protected void Page_Load(object sender, EventArgs e)



{
 
//We started this task in separate thread which will not interfere in web page usual activity.

//We can still perform all operations and this below task will run in background.

BackgroundTest longTest = new BackgroundTest(50);

Thread backgroundThread = new Thread(new ThreadStart(longTest.RunLoop));

backgroundThread.Name = "BackgroundThread";

backgroundThread.IsBackground = true;



backgroundThread.Start();
 
Label1.Text = "end";



}
 
///


/// Non Stoppable Button click..I mean we can still perform this operation

/// independent of below background thread.

///

///

///

protected void Button1_Click(object sender, System.EventArgs e)



{
 
Response.Write("Som Button Click");



}
 
///


/// Background Thread

///

class BackgroundTest



{
 
int maxIterations;

public BackgroundTest(int maxIterations)



{
 
this.maxIterations = maxIterations;
}
 
public void RunLoop()



{
 
String threadName = Thread.CurrentThread.Name;

for (int i = 0; i < maxIterations; i++)



{
 
Thread.Sleep(25000);



}
 
System.IO.File.AppendAllText("E:/test", DateTime.Now.ToString());



}

}

}
 

Unity Dependency Injection Resolve

Unity Dependency Injection has given us lot of flexibility in terms of customizations and suits to our requirement. There has been good reference and api for Unity MVC , Web API and WCF service. Here is the good free online e-book to start from scratch. It covers everything from DI, AOP, exception handling, caching and customization.


http://blogs.msdn.com/b/agile/archive/2013/08/20/new-guide-dependency-injection-with-unity.aspx

If you looking for creating instance with Unity DI for any class this is what we must do.

Injection constructor
Injection Methods


Few things to keep in mind.
1. We register interface/Class for object reference.
2. We set resolver for each class object
3. We get the resolver.

Option 1
public class MyObject
{

  public MyObject(SomeClassA objA, SomeClassB objB)
  {
    ...
  }

  [InjectionConstructor]
  public MyObject(DependentClassA depA, DependentClassB depB)
  {
    ...
  }

}
IUnityContainer uContainer = new UnityContainer();
MyObject myInstance = uContainer.Resolve();
Option 2


IUnityContainer container= new UnityContainer();
container.RegisterType<MyObject>(
               new InjectionConstructor(
               new ResolvedParameter<DependentClassA>(), new ResolvedParameter<DependentClassB>()));
               var instance= container.Resolve<MyObject>();
 
We can even have global configuration to resolve all our object instance for any class library.