Memory allocation – static class

When we create an instance of a normal class, the memory of that instance lives on the heap.

A reference to this instance might be in :

  1. An object on the heap.
    • If we set a member variable inside a different instance of an object to it.
  2. A stack variable
    • If we declared a variable to the object inside a method or passed it to a function call.
  3. It may be in the list of global roots,
  4. for static reference.

Static classes are not instantiated but are “stored” on what is called a Loader Heap. Its methods are just functions loaded into memory when the CLR loads the assembly. Static classes are guaranteed to be loaded and to have their fields initialized and their static constructor called before the class is referenced for the first time in your program. Once created a static class remains in memory until your application domain is shut down.

Loader heaps are special, non-GC heaps that have extremely predictable and strict growth rates.  A reference to every type, static or otherwise, is held on a loader heap. The entire type system is managed on a loader heap, with references to types and their members.

When a .NET application starts, several AppDomains are actually created. In addition to the primary app domain, there are system and shared app domains that contain the system namespaces and mscorelib, special heaps (such as the loader heaps), and the CLR itself.

For a fully detailed explanation, read the following MSDN Magazine article:
Drill Into .NET Framework Internals to See How the CLR Creates Runtime Objects


Difference between a static class and a singleton pattern – Interview Question

This is one of the favorite questions asked by interviewers.

I thought of putting my opinions on this topic.

  1. Most important singletons can be handled polymorphically without forcing their users to assume that there is only one instance.
  2. Singletons can implement interfaces (or derive from useful base classes).
  3. Singleton class follow the object oriented principles but not static class.
  4. A singleton can be initialized lazily or asynchronously while a static class is generally initialized when it is first loaded, leading to potential class loader issues.

My rule for choosing between static and singleton:

If there are bunch of functions should be kept together, then static is the choice.
Anything else which needs single access to some resources, could be implemented singleton.


In my current company we are going to start with Web API’s so I just thought of doing some reading and figure out questions for below answers.

Difference between WCF and ASP.Net Web API?
Why not SOAP? – it is also XML based.
Is WCF dead? If not, when to use WCF or ASP.Net Web API?

Difference between WCF and ASP.Net Web API?
  • WCF was conceived back in its Indigo and .NET 3 days, the main goal was to support/exchange SOAP-based messages using variety of transport protocols like HTTP, TCP, NamedPipes or MSMQ etc.
  • ASP.NET API is a framework for building non-SOAP based services over HTTP only.

Points of interest

  • In .NET 3.5, WCF got a new binding, “WebHttpBinding” — a new binding that helped creating non-SOAP service over HTTP, better known as a RESTful service. (WCF Rest)
    • The WebHttpBinding was not enough, and after WCF 3.5 was released, a new set of tools was created – the WCF REST Starter Kit.
    • REST starter kit was an attempt to enrich the support of WCF 3.5 for HTTP services
      • Add better client-side support for .NET apps
      • Extend the server side support for other content types
      • Enable response and request caching
      • Inspection of messages and so forth.
    • Unfortunately, this great toolkit was never officially released
  • In .NET 4, Some of the server side features of REST Starter Kit was integrated in WCF 4 but we didn’t get any of the client-side libraries.
    • We did get most of the service-side features (excluding the new inspectors). Some were well-integrated into WCF while others required the use of ASP.NET (by turning on the ASP.NET compatibility mode).

So with WCF 4 we had some support for “Web” HTTP services, but it wasn’t that perfect – to get some of the features you needed IIS hosting and ASP.NET, not all types of requests were supported easily.

Why not SOAP? – it is also XML based.
  • SOAP allows us to place all the knowledge required by our service in the message itself, disregarding its transport protocol, whether it is TCP, HTTP, UDP, PGM, Named Pipes etc
  • HTTP is an application-level protocol, and as such it offers a wide variety of features:
    • It supports verbs that define the action, GET, POST/PUT, DELETE
    • Message headers that are very meaningful and descriptive – headers that suggest the content type of the message’s body, headers that explain how to cache information, how to secure it etc.
    • Message body that can be used for any type of content, not just XML content as SOAP enforces (and if you want something else – encode it to base64 strings and place it in the SOAP’s XML content).
      • The body of HTTP messages can be anything you want – HTML, plain XML, JSON, binary files (images, videos, documents…)

Points of interest

  • The use of HTTP has evolved over the years. Application-level protocol architectural styles such as Hypermedia APIs or this link have emerged on top of HTTP.
    • These, in turn, harness the power of HTTP to create resource-oriented services, and better define the stateless interaction between clients and services

SOAP is still useful for building messages when you don’t have an alternative application-level protocol at your disposal, or when you want to use SOAP across the board while considering HTTP as no more than another way to pass messages (for example, use HTTP because it can cross firewalls more easily than TCP).

Is WCF dead? If not, when to use WCF or ASP.Net Web API?
  • ASP.NET Web API framework is not going to replace WCF. It’s just another way of building services that are non-SOAP based e.g. plain XML, JSON string etc. – This mean WCF certification still holds value J
  • WCF is still the best choice for
    • Messag Queuing scenario using MSMQ.
    • One-way communication or Duplex communication
    • If we intended to use transport other than HTTP e.g. TCP, UDP or Named Pipes.

Above information is best as per knowledge and readings. My understanding might be wrong at few places.

What is OAuth? – Basic Understanding

What is OAuth?

  • OAuth is a simple way to publish and interact with protected data.
  • It is a safer way to give people access to this data when they are calling an API, as each request to the API is signed with encrypted details that only last for a defined duration (e.g. 2 Hours)
  • There are quite a few services out there that use the OAuth standard and some of the big ones are Twitter, Twitpic, Digg and Flickr.

High level OAuth process

  • Register application with the service that you are developing it for. e.g. Twitter, Twitpic, SoundCloud etc.
    • You will receive a consumer key and secret.
  • The application then initiates the OAuth process by passing the consumer key and the consumer secret.
  • The service will return a Request Token.
  • The user now needs to grant approval for the application to run requests.
  • Once the user has granted permission application need to exchange the request token for an access token.
  • Once an access token is received, application use this to sign all http requests with application credentials and access token.

Geeks and Repetitive Tasks

This originally posted by JOEY DEVILLA at this link.

The idea here is if we have any repetitive task to be performed it is better to write a script or program to do the job. But key decision point will be when to start thinking about writing a script?

This could very to subjective to the person, in below graph the decision is made when person starts getting annoyed.

Assembly InsideOut – Part 2 – NetModule

This post is in continuation to my previous post

In this post we will look how to create multifile  assembly. This brings us to the concept of NetModule.

Netmodule is a unit of compilation.  A compiler may give the option to compile a collection of source files into an assembly, or a netmodule. Netmodule contains type metadata and compiled code. It does not contain an assembly manifest.

A netmodule can not be deployed alone. It has to be linked into an assembly. This can be done by using compiler’s /addmodule switch (if the compiler supports it), al.exe, or in .Net framework 2.0, link.exe. After linking, the netmodule can be deployed with the assembly. The .Net framework 2.0 link.exe can produce a single file assembly, while all the other approaches produce multi-file assemblies.

I recommend reading this article for further more information.

Process of creating multi-file assembly:

Let’s use the MSDN sample as an example.

C:\ stringer.vb

' Assembly building example in the .NET Framework SDK.
Imports System
Namespace myStringer
 Public Class Stringer
   Public Sub StringerMethod()
     Console.WriteLine("This is a line from StringerMethod.")
   End Sub
 End Class
End Namespace


using System;
using myStringer; //The namespace created in Stringer.netmodule.
class MainClientApp
  // Static method Main is the entry point method.
  public static void Main()
    Stringer myStringInstance = new Stringer();
    Console.WriteLine("Client code executes");

C:\ >vbc /t:module C:\Stringer.vb

Microsoft (R) Visual Basic .NET Compiler version 8.0.40607.16
for Microsoft (R) .NET Framework version 2.0.40607.16
Copyright (C) Microsoft Corporation 1987-2003. All rights reserved.

C:\ >csc /addmodule:Stringer.netmodule /t:module C:\Client.cs

Microsoft (R) Visual C# .NET Compiler version 8.00.40607.16
for Microsoft (R) Windows (R) .NET Framework version 2.0.40607
Copyright (C) Microsoft Corporation 2001-2003. All rights reserved.

C:\> al c:\stringer.netmodule c:\client.netmodule /main:MainClientApp.Main /out:c:\main.exe /target:exe

Microsoft (R) Assembly Linker version 10.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.

Manifest of created assembly,  Blue statements shows linked modules:

// Metadata version: v4.0.30319
.module extern client.netmodule
.assembly extern mscorlib
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0
.assembly extern Microsoft.VisualBasic
.publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A ) // .?_….:
.ver 10:0:0:0
.assembly main
.custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ….T..WrapNonEx
63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows.
.hash algorithm 0x00008004
.ver 0:0:0:0
.file stringer.netmodule
.hash = (FD 0C A9 48 5D 21 80 69 7E DB EF 48 77 69 C7 A0 // …H]!.i~..Hwi..
E4 81 DD 50 ) // …P
.file client.netmodule
.hash = (FA D3 E6 75 57 7D F7 11 B8 0D B2 0D 75 32 2A BD // …uW}……u2*.
20 01 41 0D ) // .A.
.class extern public myStringer.Stringer
.file stringer.netmodule
.class 0x02000002
.module main.exe
// MVID: {2C26C760-9259-4860-A2E3-3B92C68F3882}
.imagebase 0x00400000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x03300000