Tuesday, March 26, 2013

Create database from existing mdf file in sql server

EXEC sp_attach_single_file_db @dbname = 'AdventureWorks2012_Data',

@physname = 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\data\AdventureWorks2012_Data.mdf'


Monday, March 18, 2013

DataContract and MessageContract

1. Comparison

Data Contracts

WCF data contracts provide a mapping function between .NET CLR types that are defined in code and XML Schemas Definitions defined by the W3C organization (www.w3c.org/) that are used for communication outside the service.

you can say “Data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged”. That is, to communicate, the client and the service do not have to share the same types, only the same data contracts. A data contract precisely defines, for each parameter or return type, what data is serialized (turned into XML) to be exchanged.

Message Contracts

Message contracts describe the structure of SOAP messages sent to and from a service and enable you to inspect and control most of the details in the SOAP header and body. Whereas data contracts enable interoperability through the XML Schema Definition (XSD) standard, message contracts enable you to interoperate with any system that communicates through SOAP.

Using message contracts gives you complete control over the SOAP message sent to and from a service by providing access to the SOAP headers and bodies directly. This allows use of simple or complex types to define the exact content of the SOAP parts.

2. Why use MessageContract when DataContract is there?

Data contracts are used to define the data structure. Messages that are simply a .NET type, lets say in form of POCO (plain old CLR object), and generate the XML for the data you want to pass.

Message contracts are preferred only when there is a need to “control” the layout of your message(the SOAP message); for instance, adding specific headers/footer/etc to a message.

Sometimes complete control over the structure of a SOAP message is just as important as control over its contents. This is especially true when interoperability is important or to specifically control security issues at the level of the message or message part. In these cases, you can create a message contract that enables you to use a type for a parameter or return value that serializes directly into the precise SOAP message that you need.

3. Why we use MessageContract to pass SOAP headers ?

Passing information in SOAP headers is useful if you want to communicate information “out of band” from the operation signature.

For instance, session or correlation information can be passed in headers, rather than adding additional parameters to operations or adding this information as fields in the data itself.

Another example is security, where you may want to implement a custom security protocol (bypassing WS-Security) and pass credentials or tokens in custom SOAP headers.

A third example, again with security, is signing and encrypting SOAP headers, where you may want to sign and/or encrypt some or all header information. All these cases can be handled with message contracts. The downside with this technique is that the client and service must manually add and retrieve the information from the SOAP header, rather than having the serialization classes associated with data and operation contracts do it for you.

4. Can’t mix datacontracts and messagecontracts.

Because message-based programming and parameter-based programming cannot be mixed, so you cannot specify a DataContract as an input argument to an operation and have it return a MessageContract, or specify a MessageContract as the input argument to an operation and have it return a DataContract. You can mix typed and untyped messages, but not messageContracts and DataContracts. Mixing message and data contracts will cause a runtime error when you generate WSDL from the service.


Wednesday, March 13, 2013

ASP.NET 4 Caching


Caching Overview

A cache is made up of a pool of entries. Each entry has data along with a tag, which specifies the identity of the data in the backing store of which the entry is a copy.

In real life, Cache is very simple. If you take the life cycle of an ant, it reserves the food during the summer days for the usage of winter. In a similar line, IT system reserves the frequent usage content for the future request/kit to avoid the redundant operation.

When the cache client (a CPU, web browser, operating system) needs to access the data presumed to exist in the backing store, it first checks the cache. If an entry can be found with a tag matching that of the desired data, the data in the entry is used instead. This situation is known as a cache hit.

CPUs and hard drives frequently use a cache, as do web browsers and web servers. Let's see the caching mechanism at Core level.

Caching at Core Level

A CPU cache is a cache used by the central processing unit of a computer to reduce the average time to access memory. The cache is a smaller, faster memory which stores copies of the data from the most frequently used main memory locations. As long as most memory accesses are cached memory locations, the average latency of memory accesses will be closer to the cache latency than to the latency of main memory. Core/Primary cache is the fastest form of storage. Because it's built in to the chip with a zero wait-state (delay) interface to the processor's execution unit, it is limited in size. With this concept, 3 levels of Core Caching is represented from the OS point of view.

There are few popular Caching algorithms based on key parameters (like hit rate, recent usage, frequent usage). The most popular ones are Belady optimal algorithm, Least recent used (LRU), Random Replacement, Most recently used (MRU), Multi Queue Caching, Segmented LRU, etc.

ASP.NET Caching

With these fundamentals, ASP.NET Framework enables programmers to take advantage of caching without requiring to write a lot of code to deal with the complexities of caching, such as cache expiration, updates, and memory management. There are two different types of caching in ASP.NET:

·         Application caching

·         Page output caching

Application Caching

Application caching is nothing but a collection of in memory objects. It's automatically deallocated based on memory limitation, time limit and few other vital dependencies. It's also termed as application data caching because the scope of these in memory objects are at the application level. In ASP.NET web based context, this cache object exists for the entire web application, can be shared across the user sessions and web requests.

In simple terms, Application caching can be called as Global Data Container (GDC). As the name stands, Global Data Container is in the memory storage box to hold the shared data across the application.

A similar concept is extended to the latest computing, i.e., Cloud Computing. Microsoft Azure platform executes the application caching to improve the system performance. The concept is drafted in the below diagram:

Storing into Application Cache

ASP.NET provides built in Cache object from System.Web.Caching namespace. This object contains 'Insert' method to store any collection into the application cache.

Let's see the key parameters for Insert method of Cache object.

·         Key: This is the primary name to access the cached object in the Cache collection. The key must be unique in the cache.

·         Value: This is the data as an Object that you want to cache.

·         Dependencies: It is the associated item for Cache. Framework will trigger the signal during the changes in this dependency object.

·         AbsoluteExpiration: This is the time as a DateTime object at which the object should be removed from the cache.

·         SlidingExpiration: This is the time as a TimeSpan object after which the object should be removed from the cache if it has not been accessed by a user.

·         Priority: This is a CacheItemPriority enumeration value that you can use to determine which objects are removed first during scavenging (i.e., when memory starts to run low). Lower priority objects are removed sooner.

·         onRemoveCallback: This is an event handler that is called when the object is removed from the cache. This can be null if you don’t want to specify a callback method.

Code Snippet

 Collapse | Copy Code

using System.Web.Caching;
public partial class _Default : System.Web.UI.Page
    protected override void OnPreInit(EventArgs e)
        Cache["CompName"] = "ABCD Corporation";
        Cache["CompSymbol"] = "ABC";
        Cache["CEO"] = "First Last";
        // Programmatically Insert operation
        Cache.Insert("LastTrade", ReadFeed(LastTradePrice), 
                 null, DateTime.Now.AddHours(8), Cache.NoSlidingExpiration);
        //Cache Dependency operation
        System.Web.Caching.CacheDependency FeedDepend = 
            new System.Web.Caching.CacheDependency(Server.MapPath("Feed.xml"));
        Cache.Insert("MarketPrice", ReadFeed(CurrentPrice), 
                 FeedDepend, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration);

Code Summary

System.Web.Caching is the namespace supplied by ASP.NET Framework to manipulate the caching operations. It has been included in the first line. Cache content is getting loaded in PreInit state of the page. Cache storage functionality starts from the fourth line of the source code.

Based on the working knowledge with Session or similar objects, Cache object would be similar. Programmers can assign items directly to the cache by giving them a name (key) and assigning them an object (value).

In the fourth line of the above code, Company Name is stored in the cache with the key 'CompName'. Cache object is the global container object suppliant by Framework at application level. In the similar way, 'CompSymbol' and 'CEO' info are stored in global Cache object.

By using the Insert method, add an item to the cache and control how that item gets removed from the cache. This functionality includes automatic removal based on a specific period of time. Insert has a number of overloads based on the many parameters you can set when adding an item to the cache. LastTrade key insert method reserves the content from LastTradePrice, for the next 8 hours. Later, LastTrade cache automatically expires.

In ASP.NET Framework, a cache dependency links a cached item to something else such as a file or another item in the cache. ASP.NET monitors the dependency and invalidates the cache if the dependent item changes. In the above code sample, cache dependency is based on Feed.xml file. When this dependent feed file of the cache get updated, the object is removed from the cache. Apparently, we can use 'OnRemoveCallback' delegate to execute the operation on removal event.

Reading from Application Cache

It's a very easy job to retrieve the item from application cache. It's very similar to other collection objects. Programmer needs to pass the key to retrieve the value.

 Collapse | Copy Code


Code Snippet

 Collapse | Copy Code

lblName.Text = Cache["CompName"];
lblSymbol.Text = Cache["CompSymbol"];
lblCeo.Text = Cache["CEO"];                 

Code Summary

Objects can be retrieved from the cache by checking for the key. In the above example, Company Name is retrieved from Cache object CompName key and lblName is refreshed with the loaded data. In the similar way, Company Symbol and CEO info are read from System.Web.Caching.Cache global application object and the retrieved info are assigned in the appropriate label controls.

It is good practice to always verify that an item is not null. If a value is null, that value either hasn’t been cached or it has expired from the cache. If an item is null, you should retrieve it from the original source and, in most cases, place it into the cache.

Page Output Caching

In general, web browser often keeps a copy of the page on the local computer. On next time user requests, the browser simply verifies that the cached version is still valid, and then displays the cached page to the user. This methodology improves the responsiveness using the client side efficiency.

With page output caching concept, ASP.NET can keep a copy of a rendered ASP.NET webpage in memory on the server. If web requests come from the same user or different user, ASP.NET can return the page almost instantly. Let's assume the same page takes a long time to render due to the process intensive like heavy business logic, multiple database queries, etc. ASP.NET page output caching improves significant performance and reduces rendering time. If you have a lot of activity on your server, it can also increase your scalability, because resources used to retrieve data can be freed.

Execution of page output caching is drawn as:

Declarative Page Output Caching

To enable Page Output Caching, ASP .NET provides declarative approach at each page in your site. Using this approach, the programmer will get the granular control over which pages get cached and how they get cached. It's possible by adding the @ OutputCache directive to the top of a page’s markup. Programmers can configure this directive by using the below attributes.

·         Duration: The number of seconds to cache the page. It's the only mandatory parameter.

·         Location: One of the OutputCacheLocation enumeration values, such as Any (Default), Client,Downstream, Server, None, or ServerAndClient.

·         CacheProfile: The name of the cache settings to associate with the page.

·         NoStore: Flag to determine whether to prevent secondary storage of sensitive information.

·         Shared: Flag to determine whether user control output can be shared with multiple pages. The default isfalse.

·         VaryByParam: A semicolon-separated list of strings used to vary the output cache.

·         VaryByControl: A semicolon-separated list of strings used to vary a user control’s output cache.

·         SqlDependency: It identifies a set of database and table name pairs on which a page or control’s output cache depends.

Code Snippet

 Collapse | Copy Code

< %@ OutputCache Duration="20" VaryByParam="search;none" %>           

Code Summary

The above code example demonstrates how to cache a page for 20 minutes, regardless of the parameters passed to the page. At the end of this time span, Cache get expired automatically.

 Collapse | Copy Code

< %@ OutputCache Duration="15" VaryByParam="marketprice" %>           

Code Summary

If the page might display differently based on parameters, provide the names of those query string parameters in the VaryByParam attribute. The above code example caches a different copy of the page for different values provided in the marketprice query string parameters.

In a similar line, other attributes of OutputCache directive performs the declarative page output caching process in ASP.NET Framework.

Advantages of Caching

·         In terms of advantage, Cache concept can make or break the performance of your computer system. Smart buffer algorithm will make the site loading faster than traditional approach.

·         Cache takes the heavy load / execution from the server for the repeated operations. By doing so, Servers can be efficiently used.

Disadvantages of Caching

·         In terms of Caching approach, it will not be coherent with the actual data content in the delayed writes.

·         Apparently, Cache isn't always the freshest information.

Points of Interest

Hope it will be interesting to also know the core cache concepts and ASP.NET caching features, instead of just the regular abstracted code level implementation.


Tuesday, March 12, 2013

Singleton Design Pattern in C#

Design Patterns provide solutions to common recurring problems. Design patterns can be classified as creational, structural or behavioral .Singleton and Factory patterns are two of the creational pattern.Here we will discuss these two patterns .In the later posts we will discuss other design patterns.

Singleton Design Pattern

We use this design pattern when we want to restrict the number of instances of any object to one at max at any instant of time. The practical implementation of this design pattern can be seen in applications that we use daily. Take the example of Microsoft word .What happens when you open multiple word documents and navigate between them?. There is only one instance of the word application active at a time ,you can verify this by checking the active processes .So all the requests for handling the different word documents is handled by a single application instance.

Here is an implementation of the singleton design pattern in C#. We will use a console application for this example. We will create a class and define a private constructor in that class so that the instances of that class can be created from only within that class.

       private Singleton()


Since we declare our class constructor as shown above we can not access it from outside the class .The below statement will give compilation error 

Singleton obj = new Singleton();  

To access the single object of the Singleton class we will create a static variable of type Singleton and a property to access that private variable.

Here is the complete source code of the above example :

   class Singleton
private Singleton()

//private static variable of Singleton type.
        private static Singleton objSingle = new Singleton();

        //declare a public static property that returns the
        //Singleton object as we can not call the private 
        //constructor from outside the class.
        public static Singleton ObjSingle
get { return Singleton.objSingle; }
set { Singleton.objSingle = value; }

        private string name;

        public string Name
get { return name; }
set { name = value; }
class Program

        static void Main(string[] args)
//the below line create an object of singleton type
            Singleton objFirstObject = Singleton.ObjSingle;
            objFirstObject.Name = 
//if we access the objSingle property from another variable we will get the same object
            Singleton objSecondObject = Singleton.ObjSingle;
//We have assigned the name in first object,we will get the same value in second object since both points 
            //to the same instance
            Console.WriteLine("objFirstObject.Name={0} ,objSecondObject.Name={1}",objFirstObject.Name,objSecondObject.Name);


Factory Method Pattern

In the Factory method pattern there is Factory method which decides which subclass to instantiate depending on the information passed by the client. All the subclasses implement a common interface.

The client declares a variable of interface type and calls the Factory method that returns an instance of the subclass to the client code.So the client is decoupled from the implementation details of subclass creation.

Following is an example of factory method pattern.

interface IGreet
string Hello();
class WeekStart : IGreet
public string Hello()
return " Week Started";
class WeekEnd : IGreet
public string Hello()
return "Happy Weekend";

class Creator
public IGreet FactoryMethod()
if (DateTime.Now.DayOfWeek == DayOfWeek.Monday || DateTime.Now.DayOfWeek ==DayOfWeek.Tuesday
DateTime.Now.DayOfWeek == DayOfWeek.Wednesday || DateTime.Now.DayOfWeek == DayOfWeek.Thursday
return new WeekStart();
                    return new WeekEnd();

        class Program
private DateTime dt = DateTime.Now;

            static void Main(string[] args)
Creator ct = new Creator();
IGreet weekDayStatus = ct.FactoryMethod();