Saturday, 31 December 2011

Microsoft Managed Extensibility Framework (MEF)

Managed Extensibility Framework (MEF) is a composition layer for .NET that improves the flexibility, maintainability and testability of large applications. MEF can be used for third-party plugin extensibility, or it can bring the benefits of a loosely-coupled plugin-like architecture to regular applications.


MEF is a part of the Microsoft .NET Framework, with types primarily under theSystem.ComponentModel.Composition.* namespaces.

  • MEF has shipped with .NET 4.0 and Silverlight 4
  • MEF 2 is under development and can be downloaded in source code and binary form from this site

For more information, click here.



Saturday, 24 December 2011

Useful Git Terms



  • master: this is the main code branch, equivalent to trunk in Subversion. Branches are generally created off of master.
  • origin: the default remote repository that all your branches are pull'ed from and push'ed to. This is defined when you execute the initial git clone command.
  • fast-forward: the process of bringing a branch up-to-date with another branch, by fast-forwarding the commits in one branch onto the other.
  • rebase: the process by which you cut off the changes made in your local branch, and graft them onto the end of another branch.
  • unpublished vs. published branches: an unpublished branch is a branch that only exists on your local workstation, in your local repository. Nobody but you know that branch exists. A published branch is one that has beenpush'ed up to github, and is available for other developers to checkout and work on.

Thursday, 22 December 2011

NuGet Install Castle.Windsor and factilies for NLog or log4net

Install the following Castle packages from NuGet command line.
  1. PM>  install-package Castle.Windsor 
  2. PM>  install-package Castle.Core-NLog 
  3. PM> install-package Castle.Core-log4net
  4. PM> install-package Castle.LoggingFacility

 Notes: that Castle.Core-NLog has the Castle.Facilities for NLog.

Thursday, 8 December 2011

Validation of viewstate MAC failed,in hosting on a web farm

If a web application is hosted by a web farm or cluster, you would experience this exception if you didn't set up the web.config correctly. The solution for resolving this exception is to ensure the configuration specifies the same validation key and validation algorithm in every single web.config in your deployment. AutoGeneration by default cannot be used in a cluster environment. Below is the sample machinekey config entry for your web.config
 
<configuration>
    <appSettings/>
    <connectionStrings/>
    <system.web>
        <machineKey  validationKey='44DB9132AD56D581F6DBFDD8F09EDFD167B75283771DB3241804B0F03602EBACF6349C724FAC64785F60653EC9F9CA20A4D377387A1677704525FE9147CE6AC3' decryptionKey='C77E66D32F8F554C507DDEC6DA2A1A470B5EE195DA584EDD' validation='SHA1'/>
    </system.web>
</configuration>
 

Sunday, 13 November 2011

Manage IIS Application Pool from .Net C# code

  .Net framework provides a very easy-to-use API wrapper class, ServerManager, to help manage IIS system from any .Net application using either C# or VB.net. Below C# code snippet demonstrates how to restart/recycle a given AppPool in IIS.
     
    public void RestartAppPool(string appPool)
        {
            try
            {
                logger.Info("Restarting app pool: " + appPool);

                // Recycle app pool
                using (var manager = new ServerManager())
                {
                    var pool = manager.ApplicationPools[appPool];

                    if (pool != null) 
                    {
                        Process process = null;

                        if (pool.WorkerProcesses.Count > 0)
                        {
                            process = Process.GetProcessById(pool.WorkerProcesses[0].ProcessId);
                        }

                        if (pool.State == ObjectState.Stopped)
                            pool.Start();
                        else
                            pool.Recycle();

                        // clean up any worker thread created by app pool
                        if (process != null)
                        {
                            while (!process.HasExited)
                            {
                                Thread.Sleep(10);
                            }

                            process.Dispose();
                        }
                    }
                }

                //logger.Info("Successfully restarted app pool: " + appPool);
            }
            catch (Exception ex)
            {
               // logger.Error("Restart app pool error: " + ex);
            }
        }
 

Tuesday, 8 November 2011

JQuery extension: numeric only input text

Here is the simply but re-usable approach, IMO: create an jquery numeric extension and then use the numeric extension in your input text box:

 
// numeric extension
jQuery.fn.onlyNumeric = function () {
    return this.each(function () {
        $(this).keydown(function (e) {
            var key = e.which || e.keyCode;
            if (!e.shiftKey && !e.altKey && !e.ctrlKey &&
                key >= 48 && key <= 57 || key >= 96 && key <= 105 ||
                key == 8 || key == 13 || key == 46 ||
                key == 35 || key == 36 || key == 37 || key == 39)
                return true;
            return false;
        });
    });
};
// use it in your input text box
$('#input_box').onlyNumeric();
 

Wednesday, 2 November 2011

HTML meta tags for mobile or regular sites

For mobile site specifically:
<meta content='HandheldFriendly' name='true' />
<meta content='width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;' name='viewport' />
<meta name="viewport" content="width=device-width" />
<meta content='MobileOptimized' name='width' />
<meta content='HandheldFriendly' name='true' />
<meta name="google-site-verification" content="wYqVbCtxN_YM1tLTZmGkUDbEmNrSjpB7hBNp5_Avsvs" />


General meta tags for all sites:

<meta name="keywords" content="key words of you site" />
<meta name="description" content="description of your site" />

<meta http-equiv="Content-Typ" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=8" />