Tuesday, January 7, 2014

Known Issue GetOwinContext() return System.NullReferenceException

HttpContext.Current.GetOwinContext().Authenticate returns System.NullReferenceException when used in web api ie. Apicontroller.

The reason is the original context is not restored after leaving the await block. This is when used with task parallelism await async block.

So switching to .NET 4.5 solved the problem. But why? It seems that in ASP.NET 4.5, a task friendly synchronization context got introduced. This synchronization context ensures that the originel context is restored after leaving the await block.
So make sure that you either:
  • Set httpRuntime.targetFramework to 4.5, or
  • In your appSettings, set aspnet:UseTaskFriendlySynchronizationContext to true.
http://bartwullems.blogspot.se/2013/09/aspnet-web-api-httpcontextcurrent-is.html

http://vegetarianprogrammer.blogspot.se/2012/12/understanding-synchronizationcontext-in.html

Monday, January 6, 2014

Variable Scoping in Javascript

It is always important to make use of VAR to declare variable identifiers. If you miss doing this , there are two implications.

1. It is then by default act as GLOBAL variable.

2. Chances are these variables will be overwritten everytime developers uses same variable name across the modules. This is worst defect and sometimes difficult to figure it out.

/* Declared globally. */

function FindTruth(id) {


}


// Later in your page, another programmer adds…

var FindTruth= $(‘Overwritten-False-Fact’); // The FindTruth function just got

// overwritten.

Namespacing- Best Practice



/* Using a namespace. */

var MyNamespace = {

FindTruth: function(id) {


},

// Other methods can go here as well.

}

// Later in your page, another programmer adds…
var Findtruth= $(‘Overwritten-False-Fact’);// Nothing was overwritten.

In JavaScript Object Oriented programming this is Singleton pattern.

Now one can access method using MyNamespace.Findtruth(Id);

We can even structure it separately and then later add group of methods to decorate as library.

/* Super namespace. */

var SuperLib = {};

SuperLib.Common = {

// A singleton with common methods used by all objects and modules.

};

SuperLib.DataAccess= {

//Hold and transfer data

};

SuperLib.Helper= {

//Html Helper

};

!! Negation in Javascript

Double negation casts a string or a number to a boolean:
var bool = !!num;
The following values are equivalent to false in conditional statements:
  • false
  • null
  • undefined
  • The empty string "" (\ '')
  • The number 0
  • The number NaN
All other values are equivalent to true.
var x = "somevalue" var isNotEmpty = !!x.length;
Let’s break it to pieces:
x.length   // 9
!x.length  // false
!!x.length // true
 

Some Useful Basic Different ways of Javascript Coding

Style 1 Procedural Way

/* Start and stop animations using functions. */

function startAnimation() {


}

function stopAnimation() {


}

Style 2: using Prototype to define Class function

/* Anim class. */

var Anim = function() {


};

Anim.prototype.start = function() {


};

Anim.prototype.stop = function() {


};

/* Usage. */

var myAnim = new Anim();

myAnim.start();

myAnim.stop();

Style 3: Encapsulated Prototyping

/* Anim class, with a slightly different syntax for declaring methods. */

var Anim = function() {


};

Anim.prototype = {

                start: function() {

               …

              },

               stop: function() {

              …

 }

};

Style 4: Function.prototype.method

/* Add a method to the Function object that can be used to declare methods. */

Function.prototype.method = function(name, fn) {

                this.prototype[name] = fn;

};

/* Anim class, with methods created using a convenience method. */

var Anim = function() {


};

Anim.method(‘start’, function() {


});

Anim.method(‘stop’, function() {


});



Style 5: Chain Function Approach

/* This version allows the calls to be chained. */

Function.prototype.method = function(name, fn) {

                     this.prototype[name] = fn;

                    return this;

};

/* Anim class, with methods created using a convenience method and chaining. */

var Anim = function() {


};

Anim.method(‘start’, function() {


})

.method(‘stop’, function() {


});