01 December 2005

Don't mix ExecuteScalar with For XML EXPLICIT

In doing SQL Server data retrieval, it is very handy to issue a ‘For XML EXPLICIT’ Select query in the stored procedure. Then in the DB application code use ExecuteScalar to read it.

So far so good – until recently - I decide to increase a char field length by 10 bytes. Suddenly a few unit test cases break (thanks God, they break…). Return Xml string is truncated at 2033 characters.

This behaviour occurs because, for XML results greater than 2,033 characters in length, SQL Server returns the XML in multiple rows of 2,033 characters each.

This article
ASP.NET Resources - ExecuteScalar Truncates XML at 2,033 Characters investigates and describes the symptom in detail.

17 November 2005

Unleash the power of the people

Small teams with orthogonal tasks is better than a larger team where everyone trying to/not to stand in other's way.

By orthogonal (a mathematics term) I mean a well defined responsibility: Everyone knows what he/she is or supposed to do;
Everyone knows what other would expect him/her to deliver at certain time;
Everyone knows what he/she could rely on other to deliver to get his/her work done at agreed time.

I am talking in terms of engineering (e.g. software)
In this context, small team incurs less overhead to reach same level of communication effort that larger team need.

A big thanks to Rob Styles!

05 November 2005

Determistic Finalization with IDisposable

Finalization, how it works?
Heap exhaustion/shutting down an application triggers a Garbage Collection. Class that manage external resources like DB connection, file handler should implement Finalization to improve the system performance – proactively release the (external) resource once finished use.

However, objects that require finalization complicate the collection process. An object with a finalizer is not immediately released. How is works? GC checks metadata of every object type in the scan. If the object implements the Finalize() method, GC doesn't destroy it. Instead it is marked as reachable and it moved from it's orginal graph to another object graph- a special queue called finalized queue, which establishes a reference to the object, preventing its collection. GC proceeds. Meanwhile, a seperate background thread iterates all objects in the finalized queue, calling Finalize() on each and removes the finalized object from the finalization queue.

There is a fair standard pattern on implementing Finalize, such like define it as proteced+virtual; calling parent's Finalize at the end of your call... In fact c# comes with a code template destructor (~{MyClassName}), compiler expands it to full size Finalize() method at compiling.

Only after finalization can the object be removed from memory during the *next* GC pass. As a side effect of surviving one collection pass, the object is promoted into a higher generation, further delaying its eventual collection-

Non-Determistic Finalization meaning Garbage collection time is unpreditable. The application should not rely on GC to clean up expensive resources, which hurts scalability and performance.

Determistic Finalization
To free up used resources, client should proactively release used resource, such like DB connection when it is no longer used, instead of relies on GC.

-The Open()/Close() Pattern
Use object pool to manage objects without really destroy an object. Many .Net framwork classes use this pattern, E.g. file, stream (I/O, memory, network), DB connection, communication port etc.

-IDisposable Pattern
Classes that require finalization should implement the IDisposable interface in order to allow client to provide a determistic finalisatoin - short-circuit GC finalization and avoid the garbaged object (which uses external resource, say) promoted to G1.
There are two ways that classes that implement IDisposable have their objects being cleaned up.

1) Client code explicitly calls Dispose.
Your implementation in Dispose has the _chance_ to explicitly clean-up unmanaged resource as well as managed resource (by GC). By now your unmanaged resource is already freed up, there is no point to put the object in finalize queue to delays its release (G1), you should supress this by calling GC.SuppressFinalize(this);

2) Destructor Finalize method calls Dispose. This is a fallback plan if client fails to clean up.
In this case the Dispose method can only clean up unmanaged resource. Because during finalization, GC may have already removed the object for which Dispose is called (in following code example, the timer object). In this case, Dispose calls to clean this object, it will fail on null referencing.

Sample code from MCSD training material:

// Implementing IDisposable implies that the
// instances of this class will use unmanaged resources

public class Parent:IDisposable
{
// An unmanaged resource
private IntPtr ptr;

// A Managed resource
private System.Timers.Timer timer;

// Variable to track call to Dispose method
private bool disposed;

public Parent()
{
// Implement constructor
}

public void Dispose()
{
// Call the overloaded Dispose method
// with true as argument, indicating that
// Dispose is called by the user of the object
Dispose(true);
// Suppress the Finalize method so that it does not call Dispose again
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool called_by_user )
{
if (!this.disposed)
{
// if the user of the object called the Dispose method
// Clean managed as well as unmanaged data
// Otherwise clean only unmanaged data
if (called_by_user)
{
// Clean managed data
timer.Dispose();
}
ptr = IntPtr.Zero;
disposed = true;
}
//base.Dispose();
}
// C# destructor which is used to execute the finalization code
~Parent()
{
Dispose(false);
}

}

public class Child:Parent
{.
.
.

protected override void Dispose(bool called_by_user )
{
// Cleanup code for the child object
.
.
.

// Call Dispose method of the Parent class
base.Dispose(called_by_user);

}
}

03 November 2005

I am as good as Chris Sells in Math

You Passed 8th Grade Math

Congratulations, you got 9/10 correct!


Chris's result here

27 October 2005

Anti Intellisense with Test Driven Development

"I don’t think IntelliSense[note 1] is helping us become better programmers. The real objective is for us to become faster programmers, which also means that it’s cheapening our labor. " -- Charles Petzold A Talk Delivered at the NYC .NET Developer’s Group, October 20, 2005

I have to agree with Charles that Intellisense changes the way we code, it turns design and implementation solution to be kindof conversation with development environment and hence slave to it.

Luckily, here comes rescue: Test Driven Development. With it you can actually start your implementation with the requirement, demand and the burning need to fix the broken test case. You actually start with a question 'what do I want to achieve by doing this?' You then start with using a method, a member variable before had it defined. And with Re-Sharper, you can easily turn the undeclared member into a skeleton you can implement later.

Because they are undeclared when first used, Intellisense won't pop up.

Hurray!

Object Invocation Earliy binding vs Late binding


1) Use early binding – so the types are known at compile time.

2) Use Remoting Activator – under the hood, it is Reflection


//caller
Object instance = CreateInstanceUseReflection(myType, newArgs);
… …
private object CreateInstanceUseReflection(Type type, object[] args)
{
object instance = type.InvokeMember("",
BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.Instance,
null,
null,
args);
return instance;
}

3) Use Reflection directly

//caller
Object instance = CreateInstanceUseRemoting(myType, newArgs);
… …
private object CreateInstanceUseRemoting(Type type, object[] args)
{
return Activator.CreateInstance(type, args);
}


The test

I stress it will a search on 10,000 records against a 2 million records DB. And here is the result: (in milliseconds; timing on average of three runs each.)

use Remoting Activator 33855.69480
use Reflection 33422.52055
use Early Binding 31251.81840

Interesting to know.

25 October 2005

Lesson learnt: Implement IXmlSerializable to override Wsdl definition (Part II)

It is fine to override IXmlSerializable if you only intend to use XmlSerializer. If you want to generate web service wsdl definition as well, there is a problem. Our http://MyNamespace.Jingye.com/SomeWebService.asmx?wsdl gives you something like this:


<s:element minOccurs="0" maxOccurs="unbounded" name="Foo">
<s:complexType>
<s:sequence>
<s:any namespace="http://mynamespace.jingye.com /Foo" />
</s:sequence>
</s:complexType>
</s:element>


<s:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace=" http://mynamespace.jingye.com" id="Foo">
<xs:element name="Foo">
<xs:complexType>
<xs:sequence>
<xs:element name="MemberVar" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</s:schema>

Although look like we have the schema definition for Foo and would be able to use it to validate a Foo object and provide type info for de-serialisation. It is not. Any short-circuits our wsdl tool to combine external and internal schemas into one service description. If the message object is of a different XML namespace than the service, duplicate schema information is generated in the subsequent WSDL file (one for the correct XML namespace, and one for the service). If your message object is of the same XML namespace as the service, then the WSDL generation fails because the document can't contain the same namespace twice. In either of these cases, you do not get the behaviour that you are looking for. Article WSE 2.0: Give Your Web Services Consumers the Exact XML They Need to Succeed explains this.

It seemed that .NET Framework 2.0, this problem is solved by allowing the object to return an element of a schema instead of an entire schema document, which makes it possible to merge schemas during the WSDL generation step. Read this "New Features for Web Service Developers in Beta 1 of the .NET Framework 2.0".

24 October 2005

Lesson learnt: Implement IXmlSerializable to override Wsdl definition (Part I)

This is part one of implementing IXmlSerializable to customised object serialization.

Overriding WSDl definition is easy. You just need to implement IXmlSerializable for your type definition. IXmlSerializable contains three methods that is used by wsdl serialisation.
GetSechma: Implement this method to supply our custom schema definition.
WriteXml: Implement this method to write xml fragment
ReadXml: Tells your object how to de-serialise xml doc into an object of this type.

Here is a sample:


public class Foo: IXmlSerializable
{
private string _memberVar;

/// default constructor used by XmlSerializer
public Foo(){ }


XmlSchema IXmlSerializable.GetSchema()
{
Stream s = null;
try
{
s = base.GetSchema(typeof (Foo), "Foo.xsd");
s.Position = 0;
s.Flush();
XmlSchema schema = XmlSchema.Read(s, null);
schema.Compile(null);
return schema;
}finally
{
s.Close();
}
}

// ... if use default behaviour for serialization
// [XmlElement("MemberVar")]
// public String MemberVar
// {
// get {return _memberVar;}
// set { //serializer requires this }
// }

/// we do not implement this method because we only intend to use it for serialisation (from object to xml data)
void IXmlSerializable.ReadXml(XmlReader reader)
{
throw new NotImplementedException();
}

void IXmlSerializable.WriteXml(XmlWriter writer)
{
//
// dwiohfjwhfjekfhjsdkhf
//


// Do somthing to load _membervar?
writer.WriteElementString("MemberVa", _memberVar);

//WARNING: Don't close the writer here! Let serilizer do it for you!!!
//writer.Close();
}
}
}


Foo.xsd is supplied as an embedded resource:

<xs:schema id="Foo" targetNamespace="http://mynamespace.jingye.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="Foo">
<xs:complexType>
<xs:sequence>
<xs:element name="MemberVar" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

This would serialize a Foo object to:

<Foo>MemberVar>Some Data</MemberVar>l</Foo>

And the parent element (Say ‘Bar’) of Type Foo could have WriteXml like this

public class Bar : IXmlSerializable
{
public Foo[] Item;
...
public void WriteXml(XmlWriter writer)
{
foreach (Foo r in Item)
{
writer.WriteStartElement("Foo");
((IXmlSerializable)r).WriteXml(writer);
writer.WriteEndElement();
}
}

This would serialize a Bar object to:

<Bar>
<Foo><MemberVar>Some Data 1</MemberVar></Foo>
<Foo><MemberVar>Some Data 2</MemberVar></Foo>
</Bar>


So when does serialisation happen, when WriteXml and ReadXml gets called?

WriteXml is called when XmlSerializer is to marshal an object, you can verify this by knock off a quick (nunit) test case like this:

[Test]public void Test()
{

XmlDocument expectedXml = new XmlDocument();
//Build your expected result
///...

Foo aFoo = new Foo(...); //if your constructor takes param to initialize memberVar

XmlSerializer xs = new XmlSerializer(typeof (Foo));

xtwriter.Formatting = Formatting.None;


// WriteXml() is called here
xs.Serialize(xtwriter, aFoo);

XmlDocument doc = new XmlDocument();
xtwriter.Flush();
ms.Position = 0;
ms.Flush();
doc.Load(ms);

Assert.AreEqual(expectedXml.OuterXml, doc.OuterXml);
}

GetSchema is called when wsdl is invoked to generate the type definition. This could be using http://MyNamespace.Jingye.com/SomeWebService.asmx?wsdl or by using MS wsdl tool at command line to generate client proxy class.

However, if you do either of that, and if you look carefully, you will see a problem. I will talk about it in the second part.

06 October 2005

Passing double/single quote in Nant task

Tasks like in Nant doesn't like nested quotes. If you want to pass a commandline argurement that contains double quotes (for space in directory names need double quotes around it), you will have small problem.
For example, you cann't do this:


<exec workingdir="c:workingdir\bin" basedir="c:\executableDir" program="sar.exe" commandline="/s:ContentDB.config /d:..\..\ContentDB.config /f:"my file contains space" /i" />

Have tried a few ways to escape the double quote, not very sucessful. Suddenly I find we can do this:
1) add a property at top for double/single quote
2) use it!

<property name="quote" value='"' />
...
<exec workingdir="c:workingdir\bin" basedir="c:\executableDir" program="sar.exe" commandline="/s:ContentDB.config /d:..\..\ContentDB.config /f:${quote}my file contains space${quote} /i" />

30 September 2005

Control the host name/port ID when VS.Net or DevEnv load a web app

Stable build and integration build cannot host by a single machine. It is all due to when IIS and VS.NET creates a web app it always creates it in default location - local host.

The background:
W2k3, IIS6, .NET, SQL Server

Prove it 1. using vs.net

Using vs.net to open a web app which is configured to run at port 8080, notice it complains cannot find the web app at localhost(80) and tries to create one. I am sure you are familiar with the ‘smart way’ IIS and VS.net tries to recreate XXXXX_1 web app for you. Same thing here.

Prove it: 2 using DevEnv.exe
Here comes the long story:
On the CI build server, two web apps (part of the solution; both set to vdir in IIS) is hosted by the default web app (localhost:80). This config works fine with CC.net and Nant. Web apps can be built and tested properly.

Now on the same build server, I would like to have an independent nightly build process using CC.net.

From file system, the entire nightly build solution is a sibling to the CI build solution.
I create a new (IIS) Application pool – just to have a clean cut from the default application pool;
I create a NightlyBuild web App in this App pool, and I config the two web apps to map to their directories in nightly build solution. So they are symmetrical to the CI build settings. And I assign port 8081 to NightlyBuild web app.

Launch the Nant build process. Everything seemed worked fine. The build report actually says it. But when looking into the web apps. Source codes are fresh from source safe (check timestamp). However, there are no bin folders there. It looked like they are not built at all.

Turn to the CI build solution, there are bin folders. Then look up binaries. I was expecting the timestamp being older – when they are lastly built. Hi Presto, it is the timestamp of NightlyBuild’s.

See what I mean?

When start VS.net IDE or use DevEnv. It will always try to register the web app in http://localhost(:80) without any exception.

I wonder if there is a way I can intervene into this by Nant. So I can control the web app to be mapped to, say localhost:8080?

22 September 2005

Loading Xml Serialization Object Graph

A memo on using XmlSerializer.Serialize and loading the object graph:
1) use default XmlTextWriter
2) provide StringWriter to control the xml output format
3) use MemoryStream to control encoding
4) by lazy, use Console.Out to check the output on screen

There are quite a few ways to stream out the XmlSerializer marshalling result depends on the need.

How XmlSerializer.Serialize works?


1. XmlSerializer xs = new XmlSerializer(typeof(Foo));
2. xs.Serialize(..., aFoo);

On excuting the second line, our serializer then tries to serialize aFoo to Foo type. If Foo implements IXmlSerializable, it will ingore all public properties and just execute WriterXml that Foo implemented.
By default, serializer passes an object of XmlTextWriter to WriterXml. In your WriterXml implementation, you should never close the Writer. This should be left to the caller (XmlSerializer in our case).

How to serialize an object and load the marshalled result (object graph)?
The most common way is use the default XmlTextWriter
However, the xml document gererated is nicely formatted - i.e. indented. If a custom control over the format is needed, you need to passed your XmlTextWriter object to Serializer

1. XmlSerializer xs = new XmlSerializer(typeof(Foo));
2. using (StringWriter sw = new StringWriter())
3. {
4. XmlTextWriter xtwriter =new XmlTextWriter(sw);
5. xtwriter.Formatting = Formatting.None;
6. xs.Serialize(xtwriter, item.Reviews);
7. Console.WriteLine(sw.ToString());
8. }

As seen, we have a control over on the xml doc layout by specified Formatting style.
Not everthing can be controlled if use StringWriter though. It is Unicode base, so unless you override (or hijack?) StringWriter, you will always see utf-16 encoding

To specify which Encoding style to use, you need to use MemoryStream instead

1. XmlSerializer xs = new XmlSerializer(typeof(Foo));
2. using (MemoryStream ms = new MemoryStream())
3. {
4. XmlTextWriter xtwriter =new XmlTextWriter(ms, Encoding.UTF8);
5. xtwriter.Formatting = Formatting.None;
6. xs.Serialize(xtwriter, aFoo);
7. xtwriter.Flush();
8. ms.Position = 0;
9. XmlDocument doc = new XmlDocument();
10. doc.Load(new MemoryStream(ms.GetBuffer()) );
11. }

And simplified version without encoding or style:

1. XmlSerializer xs = new XmlSerializer(typeof(Foo));
2. using (MemoryStream ms = new MemoryStream())
3. {
4. xs.Serialize(ms, item.Reviews);
5. XmlDocument doc = new XmlDocument();
6. doc.Load(new MemoryStream(ms.GetBuffer()) );
7. Console.WriteLine(doc.OuterXml);
8. }

And finally, just be lazy - use Console.Out gives you a quick peek on what is produced:

1. XmlSerializer xs = new XmlSerializer(typeof(Foo));
2. xs.Serialize(Console.Out, aFoo);

16 September 2005

Automated SQL Server database objects scripting and deployment in continuous integration environment


The initiatives

In the normal development lifecycle, database objects (tables, views, stored procedures and etc) evolve as implementation progressed. The despaired changes in each developer’s environment need to aggregate into the central CI build server then cascade to each developer’s environment. Normally this is done manually as there is at lack of source control mechanism for database objects.
What we would like to have is an automated db objects scripting and release processes that we can plug into CC.Net or NAnt build process.

The Requirements:
Iteration one:
1) Automated (SQL Server) database scripting (DBGen.sql)
2) Version control DBGen.sql. – Only update source/roll out changes when new changes are made.
3) Assuming only one development machine is making changes to DB – so there is no need to consider builder server to development machines synchronisation.

Iteration two: SynchronisationSynchronous build server with development machines in a controlled way, i.e. only when a local development tasks has completed (build and tested) and the developer do a ‘Get Latest’ to sync the source code. The contrary to this is whenever changes to build server db is made and tests has been successful, using SQL backup/ replication/publish/subscription (?) mechanism to roll out the changes immediately.

Story break down (iteration one)
1. Post Build Event.
DevEnv (Vs.net) uses post build event to trigger user database schema objects (tables, views, store procedures etc) scripting.
1) Post-build event command line that kicks off the db object scripting. This can be done in two ways:
a. Scpriting (WScript/CScript) with ActiveX object SQLDMO. This option is more fine grain control on what objects to script.
b. Command line executable using SQL Server upgrade facility. Scripts entire database in one go. (Scripting Database Objects has detail introduction.)
2) Check file size (not sum size) on the generated DBGen.sql, auto check out the source control version if there are difference, - but do not check it in yet.

(Developer checks in all changes)
2. Release decision.
Decide whether there is a need to release new db schema by checking the source safe version and time stamp. This process needs to be built into Nant build process.

3. Run DBGen.sql
(If release required) DBGen.sql is executed by Nant using WMI and blah blah blah…
DBGen.sql is run after successful build, before unit tests.

Scripting Database Objects

DBAzine.com: Scripting Database Objects have some very good advice on command line Sql Server database objects (tables, index, store procedures etc) scripting. This is extremely useful on automated build process using NAnt. Basically you can run this command to generate db objects except database itself.
The script is created by executed this command:


C:\Program Files\Microsoft SQL Server\MSSQL\Upgrade>scptxfr.exe /s /d /I /f DBExtract.sql /q /r /H /Y

Then concatenate it with the 'standard' database drop and creation script produced with Enterprise Manager - All Tasks - Generate SQL Script (with only script database option)

09 September 2005

XML extensibility, xsi:type, XmlSerializer and configuration (or how to leverage XmlSerializer + OO extensibility)

Daniel Cazzulino got this smashing discussion on XML extensibility, xsi:type, XmlSerializer and configuration (or how to leverage XmlSerializer + OO extensibility): "XmlSerializer ser = new XmlSerializer( typeof( People ), new Type[] { typeof( Employee ) } );"

To sum up, this is a pattern on leverage OO inheritance and XML extensibility.

We want to produce a schema like this (sample data)


<?xml version="1.0" encoding="utf-16" ?>
<Products xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://whatever.com/example/DataTypes.xsd">
<Vehicle xsi:type="Car" Price="20000" Fuel="Petrol" Chassis="Saloon">
<VIN>070121SD1T69079YFW</VIN>
<Brand>AUDI</Brand>
<Comfort>Climate Control, CD</Comfort>
<Safe>ABS, Twin Airbage</safe>
</Vehicle>
<Vehicle xsi:type="Tractor" Price="22000" Fuel="Diesel">
<VIN>01211334SD9079YFW</VIN>
<Brand>Famer's Friend</Brand>
<FieldTools>Wrench</FieldTools>
</Vehicle>
</Products>

the scehma:


namespace ClassLibrary1
{
using System.Xml;
using System.Xml.Serialization;

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://whatever.com/example/DataTypes.xsd")]
[XmlRootAttribute(Namespace="http://whatever.com/example/DataTypes.xsd", IsNullable=false)]
public class Products
{
[XmlElement("Vehicle", typeof(Vehicle))]
public Vehicle[] Item;
}

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://whatever.com/example/DataTypes.xsd")]
public class Vehicle
{
/// <summary>retail price</summary>
[XmlAttributeAttribute("Price")]
public virtual double Price
{
get { return _price; }
set { _price = value; }
}double _price;

/// <summary>Fuel Type</summary>
[XmlAttributeAttribute("Fuel")]
public virtual string Fuel
{
get { return _fuel; }
set { _fuel = value; }
}string _fuel;

/// <summary>Vihicle Identifer Number</summary>
[XmlElementAttribute("Vin")]
public virtual string Vin
{
get { return _vin;}
set { _vin = value;}
}string _vin;

/// <summary>Vihicle brand name</summary>
[XmlElementAttribute("Brand")]
public virtual string Brand
{
get { return _brand;}
set { _brand = value;}
}string _brand;

// make the Record xml schema extensible
[XmlAnyElement()]
public XmlElement[] AllElements;

[XmlAnyAttribute()]
public XmlAttribute[] AllAttributes;
}

[XmlTypeAttribute(Namespace="http://whatever.com/example/DataTypes.xsd" )]
[XmlRoot("Vehicle")]
public class Car : Vehicle
{
[XmlAttributeAttribute("Chassis")]
public string Chassis;
/// <summary>Vihicle brand name</summary>

[XmlElementAttribute("Comfort")]
public virtual string ComfortPack
{
get { return _comfortPack;}
set { _comfortPack = value;}
}string _comfortPack;

/// <summary>Vihicle brand name</summary>

[XmlElementAttribute("Safe")]
public virtual string SaftyDevice
{
get { return _saftyDevice; }
set { _saftyDevice = value; }
}string _saftyDevice;
}

public class Tractor : Vehicle
{
/// <summary>field tools included</summary>
[XmlElement("FieldTools")]
public virtual string FieldTools
{
get { return _fieldTools; }
set { _fieldTools = value; }
}string _fieldTools;
}
}

Now to serialize the following object, I created a Nunit Test case to run it, very handy.


using System;
using NUnit.Framework;

namespace ClassLibrary1 {
using System.IO;
using System.Xml;
using System.Xml.Serialization;

[TestFixture]
public class TestClass1 {

[SetUp]
public void SetUp() {}

[TearDown]
public void TearDown() {}

[Test] public void Test()
{
Car car1 = new Car();
car1.Chassis = "Saloon";
car1.ComfortPack = "Climate Control, CD";
car1.SaftyDevice = "ABS, Twin Airbage";
car1.Brand = "AUDI";
car1.Fuel = "Petrol";
car1.Price = 20000.00;
car1.Vin = "070121SD1T69079YFW";


Tractor tractor1 = new Tractor();
tractor1.Brand = "Famer's Friend";
tractor1.FieldTools = "Wrench";
tractor1.Fuel = "Diesel";
tractor1.Price = 22000;
tractor1.Vin = "01211334SD9079YFW";

Products p = new Products();
p.Item = new Vehicle[] {car1, tractor1} ;

//The most critical step, add the extended type to schema, so serializer knows what to to do
XmlSerializer serializer = new XmlSerializer( typeof( Products ), new Type[] {typeof(Car), typeof(Tractor)} );
StringWriter writer = new StringWriter();

serializer.Serialize(writer, p);
XmlDocument actual = new XmlDocument();
actual.LoadXml(writer.ToString());
Console.WriteLine(actual.OuterXml);
}
}
}

This should produce the sample data as shown above.

Question: what are AllElements, AllAttributes in Vichicle class for?

08 September 2005

XML Schema best practice guideline



Found this XML Schema best practice guideline by David Stephenson.
Here is the summary.
1. General Meta requirements for XML schemas:
Understandable
Semantically complete
Constraining (i.e. validate)
Non-redundant
Reusable
Extensible
Non-modifying

Attributes vs. elements
There is no real consensus on when to use attributes or elements. Here are best practices to help you choose when writing your XML schema.
a. Use attributes for metadata about the parent element (foo is the parent element in the example above).

b. Use attributes for data that is semantically tied to the enclosing element.

c. Use elements for data that have a meaning separate from the enclosing element.

d. Use attributes when the value will be frequently present in order to improve the human readable form of an XML instance document or reduce its size.

e. If you don't know which to use, then use the element form (which is more extensible).


Always define elements globally. Elements within model groups (choice, sequence) should always use the ref= form and never the type= form.

06 September 2005

Putting binary object into .net resource file


Before I delete (and forget) how to import binary object such like image into .Net resouce file here is the script to do it with Nunit.


[Test]
[Ignore("Use this test to create resource file only")]
public void WriteResouceFile()
{
ResXResourceWriter rsxw = new ResXResourceWriter("TestData.resx");
rsxw.AddResource("BirdsBritannica_JacketImage",Image.FromFile("BirdsBritannica.jpg"));
rsxw.AddResource("BirdsBritannica_Title","Birds Britannica");
rsxw.AddResource("BirdsBritannica_Review","Another magnificent achievement and a unique work ... ");
rsxw.AddResource("BirdsBritannica_ISBN","0701169079");

rsxw.AddResource("Coast_JacketImage",Image.FromFile("Coast.jpg"));
rsxw.AddResource("Coast_Title","Coast");
rsxw.AddResource("Coast_Review","Accompanying the BBC series, Coast is not only a superbly illustrated ...");
rsxw.AddResource("Coast_ISBN","0563522798");

rsxw.AddResource("TheDaVinciCode_JacketImage",Image.FromFile("TheDaVinciCode.jpg"));
rsxw.AddResource("TheDaVinciCode_Title","The Da Vinci Code");
rsxw.AddResource("TheDaVinciCode_Review",@"With The Da Vinci Code, Dan Brown masterfully concocts an intelligent...");
rsxw.AddResource("TheDaVinciCode_ISBN","0552149519");
rsxw.Close();
}

01 September 2005

Windows Keyboard Shortcuts

Not really have much fun with the touch pad (or 'nipple') of the laptop I am using.
The best hotkey stokes of all are:
Run Dialog WIN + R
Minimize All WIN + M
Undo Minimize All SHIFT - WIN + M
Explorer WIN + E

27 August 2005

a recent visit to Paris and Rome










Feel free to click and see the full size of it. Hope it wouldn't make you go colour blind :-)

26 July 2005

CSS Styling Borders


Got this from the book Eric Meyer on CSS. Very light and hands-on work book. I found this CSS section that creates left hand navigation bar border is very interesting. Sometime seemed a haunting job to me.
The first version with HTML:


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Chapter 5 Project</title>
<style type="text/css">
body {background-color: rgb(100%,98%,96%); color: black;}
td {border-width: 0; padding: 0;}
td#banner {border-bottom: 2px solid rgb(60%,50%,40%);}
td#banner h1 {color: rgb(40%,30%,20%);
margin: 0; padding: 0.25em 0 0.125em 0;
font: bold 150% sans-serif; letter-spacing: 0.5em;}
td#main {background-color: transparent; color: black;
padding: 1em; font: 95% Times, serif;}
td#main h2 {font: bold 125% sans-serif;
margin: 0.5em 1em; padding: 0;
border-bottom: 1px solid rgb(80%,75%,70%);}
td#main p {margin: 1em 2.5em;}

/* menu style: the hovering link block has right border overlay the menu border line*/
td#sidelinks {vertical-align: top;}
td#sidelinks a, td#sidelinks h4 {margin:0 3px 0 0; font: bold 100% Arial, Verdana, sans-serif;border-right: 1px solid rgb(60%, 50%, 40%); background: transparent;}
td#sidelinks a {display:block; text-decoration: none; padding: 1px 10px 1px 5px; color: rgb(30%, 30%, 60%);}
td#sidelinks a:visited {color: rgb(55%, 55%, 60%);}
td#sidelinks a:hover, td#sidelinks a:focus{ background-color: rgb(100%, 70%, 70%); color: rgb(50%, 0%, 0%); border-right: 7px solid rgb(80% 30% 20%); padding-right: 7px; margin-right: 0px;}
td#sidelinks h4 {background-color: transparent; color: rgb(30%, 20%. 10%; margin: 0 3px 0 0; padding 1em 0 0; font: bold 100% Arial, Verdana, sans-serif; border-bottom: 2px solid rgb(50%, 40%, 30%); border-right: 1px solid rgb(60%, 50%, 40%); }
td#sidelinks a#comment{ background-color: rgb(100%, 92% 90%); color: black; border: 1px solid rgb(60%, 50%, 40%); border-right-width: 4px; padding-right:7px; margin-right: 3px;}

td#footer {background-color: transparent; color: rgb(70%,60%,50%);
border-top: 1px solid rgb(60%,50%,40%);
text-align: right; font-size: 85%;
padding-top: 0.33em; font-style: italic;}
</style>
</head>
<body>
<table cellspacing="0">
<tr>
<td colspan="2" id="banner"><h1>Styling With EM</h1>
</td>
</tr>
<tr>
<td id="sidelinks">
<h4>Standards</h4>
<a href="html.html" id="html">HTML</a> <a href="xhtml.html" id="xhtml">XHTML</a>

<a href="css.html" id="css">CSS</a> <a href="dom.html" id="dom">DOM</a>
<h4>Extras</h4>
<a href="tools.html" id="tools">Tools</a> <a href="review.html" id="reviews">Reviews</a>

<a href="comment.html" id="comment">Commentary</a> <a href="weblog.html" id="weblog">
Weblog</a>
<h4>Basic</h4>
<a href="contact.html" id="contact">Contact</a> <a href="index.html" id="home">Home</a>

</td>
<td id="main">
<h2>On Being a Web Mechanic</h2>
<p>Once upon a time-- okay,…</p>
<p>Once upon a time-- okay,…</p>
<p>Once upon a time-- okay,…</p>
</td>
</tr>
<tr>
<td colspan="2" id="footer">Copyright 2001 Eric A. Meyer. All Rights Reserved.</td>
</tr>
</table>
</body>
</html>

Some variations - double line border:

/* menu style: the hovering link block has right border overlay the menu border line*/
td#sidelinks {vertical-align: top;}
td#sidelinks a, td#sidelinks h4 {margin:0 3px 0 0; font: bold 100% Arial, Verdana, sans-serif;
/* border-right: 1px solid rgb(60%, 50%, 40%); */border-right: rgb(60%, 50%, 40%) 4px double; background: transparent;}
td#sidelinks a {display:block; text-decoration: none; padding: 1px 10px 1px 5px; color: rgb(30%, 30%, 60%);}
td#sidelinks a:visited {color: rgb(55%, 55%, 60%);}
td#sidelinks a:hover, td#sidelinks a:focus{ background-color: rgb(100%, 70%, 70%);color: rgb(50%, 0%, 0%);border-right: 4px solid rgb(80% 30% 20%);padding-right: 7px;margin-right: 3px;}
td#sidelinks h4 {background-color: transparent; color: rgb(30%, 20%, 10%;
margin: 0 3px 0 0; padding 1em 0 0;
font: bold 100% Arial, Verdana, sans-serif;
border-bottom: 2px solid rgb(50%, 40%, 30%);
/* border-right: 1px solid rgb(60%, 50%, 40%); */
border-right: rgb(60%, 50%, 40%) 4px double;
}
td#sidelinks a#comment{ background-color: rgb(100%, 92% 90%); color: black;border: 1px solid rgb(60%, 50%, 40%); border-right-width: 4px; padding-right:7px; margin-right: 3px;}

icon image border

/* menu style: the hovering link block has right border overlay the menu border line*/
td#sidelinks {vertical-align: top;}
td#sidelinks a, td#sidelinks h4 {margin:0 3px 0 0; font: bold 100% Arial, Verdana, sans-serif;
/* border-right: 1px solid rgb(60%, 50%, 40%); */border-right: rgb(60%, 50%, 40%) 4px double; background: transparent;}
td#sidelinks a {display:block; text-decoration: none; padding: 1px 10px 1px 5px; color: rgb(30%, 30%, 60%);}
td#sidelinks a:visited {color: rgb(55%, 55%, 60%);}
td#sidelinks a:hover, td#sidelinks a:focus{ background-color: rgb(100%, 70%, 70%);color: rgb(50%, 0%, 0%);border-right: 4px solid rgb(80% 30% 20%);padding-right: 7px;margin-right: 3px;}
td#sidelinks h4 {background-color: transparent; color: rgb(30%, 20%, 10%;
margin: 0 3px 0 0; padding 1em 0 0;
font: bold 100% Arial, Verdana, sans-serif;
border-bottom: 2px solid rgb(50%, 40%, 30%);
/* border-right: 1px solid rgb(60%, 50%, 40%); */
border-right: rgb(60%, 50%, 40%) 4px double;
}
td#sidelinks a#comment{ background-color: rgb(100%, 92% 90%); color: black;border: 1px solid rgb(60%, 50%, 40%); border-right-width: 4px; padding-right:7px; margin-right: 3px;}


/* menu style: the hovering link block has right border overlay the menu border line*/
td#sidelinks {vertical-align: top;}
td#sidelinks a, td#sidelinks h4 {margin:0 3px 0 0;font: bold 100% Arial, Verdana, sans-serif;border-right: 1px solid rgb(60%, 50%, 40%);background: transparent;}
td#sidelinks a {display:block;text-decoration: none; padding: 1px 10px 1px 5px; color: rgb(30%, 30%, 60%);}
td#sidelinks a:visited {color: rgb(55%, 55%, 60%);}
td#sidelinks a:hover, td#sidelinks a:focus{ background-color: rgb(100%, 70%, 70%);color: rgb(50%, 0%, 0%);border-right: 7px solid rgb(80% 30% 20%);background-image: url(arrow.gif); background-repeat: no-repeat;background-position: 94% 50%; padding-right: 7px;margin-right: 0px;}
td#sidelinks h4 {background-color: transparent; color: rgb(30%, 20%. 10%; margin: 0 3px 0 0; padding 1em 0 0; font: bold 100% Arial, Verdana, sans-serif;border-bottom: 2px solid rgb(50%, 40%, 30%); border-right: 1px solid rgb(60%, 50%, 40%);}
td#sidelinks a#comment{ background-color: rgb(100%, 92% 90%); color: black;background-image: url(arrow2.gif);ackground-repeat: no-repeat;background-position: 96% 50%;border: 1px solid rgb(60%, 50%, 40%); border-right-width: 4px; padding-right:7px; margin-right: 3px;}

25 July 2005

Up Sticks

I am leaving this famous online Bank in August to join a software house specialised in Learning and Library software. Has been with this Bank for around three years, I think it is good to take in some fresh air and move on.

My new employer does Test Driven development, on .NET and Java platforms. There are loads of challenges in the eXtreme Programming software engineering, new technologies -.Net, XML parser, Web Services, Image search etc to play with. I think it is good to broaden my profile.

Happen to read Steve Jobs’ recent speech at Stanford, which couldn’t describe what I think better.

‘I'm pretty sure none of this would have happened if I hadn't been fired from Apple. It was awful tasting medicine, but I guess the patient needed it. Sometimes life hits you in the head with a brick. Don't lose faith. I'm convinced that the only thing that kept me going was that I loved what I did. You've got to find what you love. And that is as true for your work as it is for your lovers. Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do.’

I will be then driving 36 miles south down notorious M6 J10-J4 section instead of going east 35 miles down the picturesque A518 and A50. I probably will give up driving altogether with pushbike and commuter train, which saves around 2 hours a day.

18 July 2005

Delicious Library and more

Traditional media are tangible: books, CD, VHS tape. We consume them, collect them, shelf them in our personal libraries. Over years the collection grows and we are satisfied because we ‘own’ something that you can touch and feel it.

Digital media while offering convenient method of publication and spreading with minimum logistic cost. They are intangible. Consumer doesn’t feel they ‘own’ it. Delicious Library offers a revolution way to materialize the electron to give user a feel of ownership.

This brings me to think how we could use internet as ‘WEB’ – meaning all information are linked and not isolated. In aspect of digital music, book collections, there is a clear advantage over the tradition tangible collections. You can easily share or show off your collection worldwide.

We are not talking about Napster, but the book list or music play list. Or even better your book review critics.

I am talking about a WWW portable collection of your participation. A hybrid of webblog, Amazon, iTune, Delicious library and Opac or even more. Say, you write reviews on the books you read recently. You have a collection of the critics online that says everything about you – (‘I think therefore I am’?) Your collection is also fed to community forums (BBS, Amazon etc) so it is cross referenced.

Would this model gives a feeling of ownership of intangible?

Delicious Library Review

15 July 2005

Get Things Done

This bullet point article giev some very useful advice on time & project management.

In my experience, in eXtreme programming one of the pain and time-consuming task is project planning: ask business partners to define the requirements and prioritised them with full house of the team sitting around – project managers, developers, testers and etc. Some people loss their interest in this lengthy meetings because on

07 June 2005

SOA: what people said about it

Grady points out SOA is just one partof establishing an enterprise architecture. The solid software engineering is still required.
He also argues that SOA is for 'large grained/low frequency interactions'. This is controversial. As Jim Alateras points out in his blog, B2C sites like ebay, amazon are SOA-ed. SOA brings architectural sound WEB API, it also enables a core infrastructure stack to support multiple channels, such like smart client.

06 June 2005

What Great .NET Developers Ought To Know (Part Four ASP.NET (UI) Developers)

Part Four ASP.NET (UI) Developers (III)
Describe ways to present an arbitrary endpoint (URL) and route requests to that endpoint to ASP.NET.

Explain how cookies work. Give an example of Cookie abuse.
Cookie is a text file has a name, contains a collection of values, and an expiration time. Cookie is planted to client browser by a web server. It contains value that can be used by the server to identify a client later.
Cookie abuse means a web contain provider using cookie to collect personal/demographic information without prior consent from the user and/or use these data in a way without user consent. Here is an example: Doubleclick sued over alleged cookie abuse

Explain the importance of HttpRequest.ValidateInput()?
ValidateInput check the three collections (QueryString, Form, and Cookies) for markup tags to prevent potentially dangerous data like cross site scripting.
If the validation feature is enabled by page directive or configuration, this method is called during the Page's ProcessRequest processing phase. ValidateInput can be called by your code if the validation feature is not enabled. Request validation works by checking all input data against a hard-coded list of potentially dangerous data.
QueryString, Form, and Cookies input data is checked during request validation.

<%@ Page validation="true" /“false” %>

What kind of data is passed via HTTP Headers?
HTTP header metadata (information) about the document (HTML). Standard ones like:

HTTP/1.1 200 OK
Date: Wed, 13 Aug 1997 02:35:50 GMT
Server: Apache/1.2.0
Last-Modified: Fri, 04 Jul 1997 22:18:24 GMT
ETag: "28f7d-810-33bd76b0"
Content-Length: 2064
Accept-Ranges: bytes
Connection: close
Content-Type: text/html

One can also define additional metadata like:
<META HTTP-EQUIV="Author" CONTENT="John Doe">
When the browser gets the document, it pretends that there is a header looks like this:
Author: John Doe

Pointers Quick reference to HTTP headers

Juxtapose the HTTP verbs GET and POST. What is HEAD?
HTTP-GET request gets information from a web server. It passes arguements (querystring) as a part of hostname via URL. Total length is limited a few hundred bytes. HTTP-POST request allows a client to send data to the server. The POST method passes all of its parameter data in an input stream, removing the limit of the size of the data. Unlike the GET method, POST is not expected to be safe nor idempotent
The HTTP HEAD method is very similar to the HTTP GET method. The request looks exactly the same as the GET request (except the word HEAD is used instead of GET), but the server only returns the header information.
HEAD is often used to check the following:
The last-modified date of a document on the server for caching purposes
The size of a document before downloading (so the browser can present progress information)
The server type, allowing the client to customize requests for that server
The type of the requested document, so the client can be sure it supports it
Note that HEAD, like GET, is expected to be safe and idempotent.
A practical use of HEAD is to scan massive URLs for validity or search stream files (MP3 search engine) in which we need retrieve only part of a file which contains the descriptor fields (metadata).

Name and describe at least a half dozen HTTP Status Codes and what they express to the requesting client.
1XX: informational:
100: Continue; 101: Switch protocols
2XX: Sucessful:
200: OK; 201 Created; 202 Accepted
3XX: Redirection
300 Multiple Choices; 301 Moved Permanently; 302: Redirect request found;
4XX: Client Error
400: Bad request; 401:Unauthorized; 403: Forbidden; 404: Resource Not Found;
5XX: Server Error
500: Internal Server Error; 501 Not Implemented; 502 Bad Gateway; 503 Service Unavailable.
Pointer
Hypertext Transfer Protocol -- HTTP/1.1 RFC 2616 Fielding, et al. Status Code Definitions

How does If-Unmodified-Since work? How can it be programmatically implemented with ASP.NET?
The If-Unmodified-Since request-header field is used with a method to make it conditional. If the requested resource has not been modified since the time specified in this field, the server SHOULD perform the requested operation as if the If-Unmodified-Since header were not present.

Tracking and Resuming Large File Downloads gives a nice clean example on a file download HttpHandler implementation. In the ProcessRequest method it checks HTTP headers to decide the download status.
...
ElseIf Not CheckIfUnmodifiedSince(objRequest, _
objFile) Then
' The entity was modified since the requested
' date...
objResponse.StatusCode = 412 ' Precondition failed

Private Function CheckIfUnmodifiedSince(ByVal objRequest As HttpRequest, ByVal objFile As Download.FileInformation) As Boolean
Dim sDate As String
Dim dDate As Date
Dim bReturn As Boolean


' Checks the If-Unmodified or Unless-Modified-Since header, if
' one of them was sent with the request.
'
' Returns True, if the file was not modified since the
' indicated date (RFC 1123 format), or
' if no header was sent,
' returns False, if the file was modified since the indicated date
' Retrieve If-Unmodified-Since Header value from Request (Empty if none is indicated)
sDate = RetrieveHeader(objRequest, "If-Unmodified-Since"
, String.Empty)

If sDate.Equals(String.Empty) Then
' If-Unmodified-Since was not sent, check Unless-Modified-Since...
sDate = RetrieveHeader(objRequest, HTTP_HEADER_UNLESS_MODIFIED_SINCE, String.Empty)
End If
If sDate.Equals(String.Empty) Then
' No date was indicated,
' so just give this as True
bReturn = True

End If
Return bReturn
End Function

Pointers Quick reference to HTTP headers Tracking and Resuming Large File Downloads

What Great .NET Developers Ought To Know (Part Four ASP.NET (UI) Developers)

Part Four ASP.NET (UI) Developers (II)

Give an example of how using an HttpHandler could simplify an existing design that serves Check Images from an .aspx page.
Scot Hanselman himself got something to say on A Boilerplate HttpHandler.
Just like .aspx page is an HttpHandler that handles Http Request:

// System.Web.UI.Page
public class Page : TemplateControl, IHttpHandler

<httphandlers>
...
<add type="System.Web.UI.PageHandlerFactory" path="*.aspx" verb="*">
...
</httphandlers>

You can implement your own HttpHandler to return other MEMI type (image, download file, pdf etc) to browser. The register your HttpHandler in the web.config (or you can register it in machine.config or IIS Application Configuration properties page if implements IHttpHandlerFactory)
//my HttpHandler
public class MyHandler : IHttpHandler{…}
<configuration>
<system.web>
<httphandlers>
<add type="Namespace.MyHandler.New, MyHandlerAssemblyName" path="MyHandler.New" verb=" GET, PUT, POST ">
</httphandlers>

MyHandler implementation (primarily in ProcessRequest)should cater following steps:
1 Setting context.Response.StatusCode
2 Setting context.Response.ContentType = "somespecific/mimetype"; (e.g. Image/jpg)
3 Setting context.Response

What kinds of events can an HttpModule subscribe to? What influence can they have on an implementation? What can be done without recompiling the ASP.NET Application?
An HttpModule implement IHttpModule:
public interface IHttpModule{
void Dispose();
void Init(HttpApplication context)
}

HttpModules are hooked into the Http request pipeline via entry like this in web.config:
<httpmodules>
<configuration><system.web></a><httpmodules>
<add type="classname,assemblyname" name="modulename"><remove name="modulename">
<clear>
</httpmodules>

The ASP.NET runtime calls the module's Init and Dispose methods. Init is called when the module attaches itself to the HttpApplication object and Dispose is called when the module is detached from HttpApplication. The Init and Dispose methods represent the module's opportunity to hook into a variety of events exposed by HttpApplication.

As can be seen from the above config settings, without recompile the asp.net app, you can remove one or more HttpModules.

An HttpMoudle can subscribe to following events:
AcquireRequestState When ASP.NET acquires the current state (for example, session state) associated with the current request.
AuthenticateRequest When a security module has established the identity of the user
AuthorizeRequest When a security module has verified user authorization
BeginRequest When the first event in the HTTP pipeline chain of execution responds to a request
Disposed When ASP.NET completes the chain of execution when responding to a request
EndRequest When the last event in the HTTP pipeline chain of execution responds to a request
Error When an unhandled exception is thrown
PostRequestHandlerExecute When the ASP.NET handler (page, XML Web Service) finishes execution
PreRequestHandlerExecute Just before ASP.NET begins executing a handler such as a page or XML Web Service
PreSendRequestContent Just before ASP.NET sends content to the client
PreSendRequestHeaders Just before ASP.NET sends HTTP headers to the client
ReleaseRequestState After ASP.NET finishes executing all request handlers; also causes state modules to save the current state data
ResolveRequestCache When ASP.NET completes an authorization event to let the caching modules serve requests from the cache, bypassing execution of the handler (the page or XML Web Service, for example)
UpdateRequestCache When ASP.NET finishes executing a handler in order to let caching modules store responses that will be used to serve subsequent requests from the cache

A sample HttpModule class:

// This module, named HttpModules.CS will be compiled
// into an assembly named HttpModules.dll
// config entry:
// <add type=" HttpModuleExamples.CustomHttpModule, HttpModules.dll" name=" CustomHttpModule">

using System;
using System.Web;

namespace HttpModuleExamples {
public class CustomHttpModule : IHttpModule {
// IHttpModule members
public void Init(HttpApplication httpApp) {
httpApp.BeginRequest +=
new EventHandler(this.OnBeginRequest);

httpApp.EndRequest +=
new EventHandler(this.OnEndRequest);
}

public void Dispose() {
// Usually, nothing has to happen here...
}

// event handlers
public void OnBeginRequest(object o, EventArgs ea) {
HttpApplication httpApp = (HttpApplication) o;
HttpContext ctx = HttpContext.Current;
ctx.Response.Write("Beginning Request
");
}

public void OnEndRequest(object o, EventArgs ea) {
HttpApplication httpApp = (HttpApplication) o;
HttpContext ctx = HttpContext.Current;

ctx.Response.Write("Ending Request
");}}}


Pointer: HTTP Modules

Explain <@OutputCache%> and the usage of VaryByParam, VaryByHeader, VaryByCustom?
<@OutputCache%> Declaratively controls the output caching policies of an ASP.NET page or a user control contained in a page. Cached page/control is placed in memory.
VaryByParam: Different versions of the page are stored based on the query string values.

If there are one thousand different ids are queried in 30 sec, there will be one thousand pages cached. On the other hand queries page.aspx?id=1, page.aspx?id=1&Num=1, and page.aspx?id=1&Num=2 will receive the same cached page.
VaryByHeader: Different versions of the page are stored based on the specified HTTP header values.

Four requests arrive for the page with the following Accept-Language headers: 1) de-lu; 2) en-us; 3) fr; 4) en-us, three cached pages is created and the second en-us request reads from cache.
VaryByCustom: Different versions of the page are stored based on browser type and major version. Additionally, you can extend output caching by defining custom strings.

Then override the HttpApplication.GetVaryByCustomString method in the Global.asax file. This string is built by you and used as a key to store and retrieve a cached version of your page. The key can be anything and build from anything. You can create a composite key from other pieces of information available to you like cookies, user-languages, browser capabilities, whatever.

public override string GetVaryByCustomString(HttpContext context, string arg){
if(arg.ToLower() == "mycustomstring"){
HttpCookie cookie = context.Request.Cookies["ID"];
if(cookie != null)
return cookie.Value;
}
return base.GetVaryByCustomString (context, custom);
}

Pointer: Advanced ASP.NET Caching and AddValidationCallBack Improving ASP.NET Performance

How would one implement ASP.NET HTML output caching, caching outgoing versions of pages generated via all values of q= except where q=5 (as in http://localhost/page.aspx?q=5)?
Use AddValidationCallBack It provides a mechanism to programmatically check the validity of a item in the cache before the item is returned from the cache. Before the response is served from the Web server cache, all registered handlers are queried to ensure resource validity. If any handler sets a flag indicating that the resource is invalid, the entry is marked invalid and evicted from the cache. The request is then handled as if it were a cache miss.
Pointer: Advanced ASP.NET Caching and AddValidationCallBack MSDN AddValidationCallBack

When can I safely set the EnableViewState property to False?
Pointers: Understanding ASP.NET View State
View state is only need when you need to remember state across postbacks.
For a Web page that has a read-only DataGrid, you'd definitely want to set the DataGrid's EnableViewState property to False. You can even create sortable and pageable DataGrids with the view state disabled.

The DataGrid stores its contents in the view state so the page developer doesn't need to rebind the database data to the DataGrid on each and every page load, but only on the first one. The benefit is that the database doesn't need to be accessed as often. If, however, you set a DataGrid's EnableViewState property to False, you'll need to rebind the database data to the DataGrid on both the first page load and every subsequent postback.

Using cookieless sessions is that the session state is lost if an absolute URL is invoked(Underpinnings of the Session State Implementation in ASP.NET)
The context:
Suppose that you request a page at the following URL:
http://www.contoso.com/sample.aspx

What is really displayed in the browser's address bar is slightly different and now includes the session ID, as shown here:
http://www.contoso.com/(5ylg0455mrvws1uz5mmaau45)/sample.aspx

When instantiated, the session-state HTTP module checks the value of the cookieless attribute. If true, the request is redirected (HTTP 302) to a modified virtual URL that includes the session ID just before the page name. When processed again, the request embeds the session ID. If the request starts a new session, the HTTP module generates a new session ID and then redirects the request. If the request is a postback, the session ID is already there because postbacks use relative URLs.

The problem:
The drawback of using cookieless sessions is that the session state is lost if an absolute URL is invoked. When cookies are used, you can clear the address bar, go to another application, and then return to the previous one and retrieve the same session values. If you do this when session cookies are disabled, the session data is lost. For example, the following code breaks the session:
<a href="/code/page.aspx" runat="server">Click</a>

The solution:
If you need to use absolute URLs, resort to a little trick and manually add the session ID to the URL. You use the ApplyAppPathModifier method on the HttpResponse class.
<a runat="server"
href=<% =Response.ApplyAppPathModifier("/code/page.aspx")%> >Click</a>

The ApplyAppPathModifier method takes a string representing a URL and returns an absolute URL, which embeds session information. For example, this trick is especially useful in situations in which you need to redirect from a HTTP page to an HTTPS page.

What Great .NET Developers Ought To Know (Part Four ASP.NET (UI) Developers)

This is getting longer and longer. Some questions are rather like a small insight reseach than a simple Q&A. I will re-org some of them later to be a stand alone post.

Part Four ASP.NET (UI) Developers (I)

Describe how a browser-based Form POST becomes a Server-Side event like Button1_OnClick.
In Web server controls, certain events, typically click events, cause the form to be posted back to the server. An event is a message, "Button1_Click". The binding between the event message and a specific method — that is, an event handler — is done using an event delegate. In the following code snippet, an (object of) event delegate EventHandler is instantiated, it binds the event handler - btnNext_Click method to the event btnNext.Click (message).

// the binding
private void InitializeComponent(){
//
this.Load += new System.EventHandler(this.Page_Load);
this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
}
// an event handler
private void btnNext_Click(object sender, System.EventArgs e){}

What is a PostBack?
Postback means a web page (contains web request) is submitted to itself on the web server. ASP.NET page object model uses PostBack on event handling.

What is ViewState? How is it encoded? Is it encrypted? Who uses ViewState?
VIEWSTATE is a set or string of encoded data stored in a hidden form field to to preserve data between round trips to and from the server. ViewState automatically preserves property values of the page, and the values of all controls on the page, between round trips. Data stored in VIEWSTATE is base-64 encoded.
Pointers: Understanding ASP.NET View State

What is the <machinekey>element and what two ASP.NET technologies is it used for?
Machine Authentication Check(MAC): Machine authentication checks are designed to ensure that the data received by a computer is the same data that it transmitted out. This can be done by using <machinekey>.
<machinekey>configures key to use for encryption and decryption of forms authentication cookie data and view state data, and for verification of out-of-process session state identification. This section can be declared at the machine (machine.config), site, and application levels (web.config), but not at the subdirectory level.
<configuration><system.web></a><machinekey validation="SHA1\\MD53\\DES" decryptionkey="AutoGeneratevalue[,IsolateApps]" validationkey="AutoGeneratevalue[,IsolateApps]">

What three Session State providers are available in ASP.NET 1.1? What are the pros and cons of each?
<sessionState mode="InProc StateServer SQLServer />

InProc Session values are kept as live objects in the memory of the ASP.NET worker process (aspnet_wp.exe or w3wp.exe in Microsoft® Windows Server™ 2003). This is the default option.
Pros: Fastest. Cons: Not scalable (web garden/farm); performance hit as memory required, state lives short, less robust.

Pros on out-of-proc: fully scalable; state lives longer; protected against IIS and ASP.Net failure; Cons: Slower than InProc due to serialization and de-serialization to/from storage medium. StateServer add at least 15%, SQLServer add at least 25% performance drop compares to InProc).

StateServer Session values are serialized and stored in the memory of a separate process (aspnet_state.exe). The process can also run on another machine.
Pros: fastest Out-of-Proc solution; easy administration. Cons: not as fast as InProc; not as robust as SQLServer.

SQLServer Session values are serialized and stored in a Microsoft® SQL Server™ table. The instance of SQL Server can run either locally or remotely.
Pros: more robust than StateServer.
Cons: Slowest.
Pointers: Underpinnings of the Session State Implementation in ASP.NET

What is Web Gardening? How would using it affect a design?
The ASP.NET process model helps enable scalability on multiprocessor computers by distributing work to several processes, one per CPU, each with processor affinity set to its CPU. This technique is called Web Gardening. This technique is particularly useful for applications that rely extensively on external resources, for example: slow database server or calls COM objects that have external dependencies.

Web gardening is not suitable for heavy stateful applications. Web gardening enables multiple worker processes (one per CPU participating in the web garden) to run at the same time. All processes will have their own copy of application state, in-process session state, ASP.NET cache, static data etc, and all that is needed to run applications. The more stateful applications are, the more they risk to pay in terms of real performance of web gardening.

Beyond the questions:
The Web garden model is configurable through the <processmodel>section of the machine.config file.

Notice that the <processmodel> section is the only configuration section that cannot be placed in an application-specific web.config file. This means that the Web garden mode applies to all applications running on the machine.

However, by using the &l;tlocation> node in the machine.config source, you can adapt machine-wide settings on a per-application basis.
<processmodel webgarden="[truefalse]" cpumask="[bit mask]">

Pointer: The ASP.NET HTTP Runtime
Given one ASP.NET application, how many application objects does it have on a single proc box? A dual? A dual with Web Gardening enabled? How would this affect a design?
The term ‘Application object’ is vague here. If we are talking about ASP.NET worker process (aspnet_wp.exe or w3wp.exe) it is one per CPU. On a Dual CPU box without enabling web gardening, it is still one worker process. If web gardening is enabled for all CPUs, one worker process is created per CPU.

If we are talking about HttpApplication object here, it has nothing to do with number of the CPUs in a box. ASP.NET worker process creates a number of AppDomains, isolated by the virtual directory or web application. Each AppDomain has an HttpApplication object pool. An individual HttpApplication object is created to handle each simultaneous HTTP request. However number of the objects in the HttpApplication pool is not configurable. Neither number of AppDomains (sure too many active web apps on a box will slow it down).

Pointer Building Secure ASP.NET Applications: Authentication, Authorization, and Secure Communication

Are threads reused in ASP.NET between reqeusts? Does every HttpRequest get its own thread? Should you use Thread Local storage with ASP.NET?
Aha, just partly answered this question above. Each HTTP request is served by an HttpApplication object, which is pooled. Threads are reused.
Thread Local storage(TLS) provides a mechanism to store data that is unique to a thread and whose value is determined at run time. This type of storage can be very helpful when dealing with an existing multithreaded application whose interfaces or original design are too inflexible for passing these values another way.
Using TLS in a thread pooling environment can be very tricky. One should not use TLS on ASP.NET application. You never know on what thread you might be called (inside an ISAPI DLL), so using TLS for anything other than tracking resources that are explicitly bound to a particular thread is a bad idea.

Is the [ThreadStatic] attribute useful in ASP.NET? Are there side effects? Good or bad?
The conext: how to share static fields on per thread scope in opposite to per AppDomain scope?
The question itself doesn't make sense. The value that needs to be shared should be tied to a specific request, not a specific thread. To do this you can use the System.Web.HttpContext.Current.Items collection.
[ThreadStatic] is the managed equivalent of TLS in ASP.NET. It should be used when you can control the threadpool and the lifecycle of a thread (but you can’t achieve this easily, can you?) ThreadStatic gives you thread local storage, not HttpContext local storage!
It needs to be very careful on using [ThreadStatic] . Static fields marked with ThreadStaticAttribute are not shared between threads. Each executing thread has a separate instance of the field, and independently sets and gets values for that field. If the field is accessed on a different thread, it will contain a different value. And this is deemed to happen due to thread agile in ASP.NET. ASP.NET not only uses a thread pool, but may switch threads during request processing. It makes thread local storage unreliable.
Pointers Storing things on Threads, A tale of two techniques: The [ThreadStatic] Attribute and System.Web.HttpContext.Current.Items, ThreadStatic FUD

03 June 2005

What Great .NET Developers Ought To Know (Part Three C# Component Developers)

Continue on Scott Hanselman's - What Great .NET Developers Ought To Know
Part Three C# Component Developers


Juxtapose the use of override with new. What is shadowing?
Using override keyword to a method in the derived class meaning the method provides a new implementation to the overridden method (same signature) in the base class. The base method must be virtual, abstract, or override.
By default a method is not modified by ‘virtual’. So if a method in derived class wants to override the base implementation, it needs to be modified by ‘new’ keyword.
This is called shadowing, not overriding. The method in the derived HIDES the one in the base class.

Explain the use of virtual, sealed, override, and abstract.
virtual keyword is used to modify a method or property declaration. It allows the downcasting from base class to derived class. From derived class’s point, the method overrides it specialised the implementation. To enforce the overriding, base method modify with abstract.
abstract keyword is used to modify a class, method or property declaration. You cannot instantiate an abstract class or make calls to an abstract method directly.
An abstract virtual method meaning that the method provides maybe a partial implementation, may be no implementation at all. And a derived class of it must implement it.
A sealed class cannot be derived. This modifier is most likely used on a static class. An abstract class cannot be sealed.
A sealed override method in a derived class prevent it being further overriden.

Explain the importance and use of each component of this string: Foo.Bar, Version=2.0.205.0, Culture=neutral, PublicKeyToken=593777ae2d274679d
This string specifies shows a fully specified reference for a strongly named assembly.

Explain the differences between public, protected, private and internal.
Visibility differences on accessing a method, class:
private methods are only visible to a member in the same class;
protected methods are only visible to members in the derived class plus private scope;
internal methods/classes are visible to members in the same assembly;
public methods/class are visible to all.

What benefit do you get from using a Primary Interop Assembly (PIA)?
You get compatibility between applications that shares types defined in a PIA.
PIA contains the official, unique type identity description as defined by the publisher of those types. The PIA may contain certain customizations that make the types easier to use from managed code. The PIA is always signed by the publisher of the original COM type.
By what mechanism does NUnit know what methods to test?
Reflection.

What is the difference between: catch(Exception e){throw e;} and catch(Exception e){throw;}
catch(Exception e){throw e;} suppress the original exception stack and begin a new exception flow. Original stack trace information maybe lost
catch(Exception e){throw;} re-throw original exception.

What is the difference between typeof(foo) and myFoo.GetType()?
Operatior typeof(foo) is used to obtain the System.Type object for a type at compile time.
myFoo.GetType() is used to obtain the exact run time type of an object. This method uses reflection.

Explain what’s happening in the first constructor and how is this construct useful?


public class c{
public c(string a) : this() {;};
public c() {;}
}

When constructor public c(string a) is called to construct an object, it first calls the default constructor then the initialisation procedures in public c(string a).

What is this? Can this be used within a static method?
The this keyword refers to the current instance of the class. It cannot be use within a static method, because static method doesn’t live in an instance object type.

01 June 2005

(Communication Skills)The Secret of Inspiration

Wednesday June 1, 01:41 PM
The Secret of Inspiration
By Carmine Gallo

During an interview for my new book, I was speaking to Dilbert creator Scott Adams about the failure of most managers to inspire their employees. Adams is very funny and extremely insightful. In his opinion, most business professionals fail to articulate Advertisement

a compelling vision because they don't have anything to say.

Simple enough and probably true in many cases. But if you're like most entrepreneurs, you do have something to say -- and a vision that will inspire your employees, colleagues, and customers. You might just need a little help getting it across.

Here's the deal: Most employees are uninspired by their work and by the people for whom they work. How do I know? Well, it doesn't take a rocket scientist. Just look at the face of the person next to you on the train during rush-hour commute. Pay attention to the bank teller's demeanor. Note the lack of enthusiasm from the department store sales clerk.

COMMON TECHNIQUES. A recent Conference Board survey finds that only half of U.S. workers are happy with their jobs, and of those who are happy, only 14% are "very satisfied." When people are uninspired at work, it shows -- customer-service surveys have revealed a marked decline in satisfaction in recent years.

My goal in writing 10 Simple Secrets of the World's Greatest Communicators was to identify business leaders who truly inspire those in their personal and professional lives, and to share their techniques with my readers. After interviewing more than two dozen contemporary CEOs, executives, and experts, I found a common technique among all those considered among the most inspiring. Are you ready for it? The secret to inspiring your listeners is to paint a picture of a world made better by your service, product, company, or cause.

Think about it. Cisco (CSCO) CEO John Chambers doesn't sell routers and switches when he communicates to employees, colleagues, or customers -- he sells a vision of an Internet that changes the way we "live, work, play, and learn." Starbucks (SBUX) founder Howard Schultz doesn't sell coffee beans -- he sells the concept of a community, a "third place" between work and home. Suze Orman doesn't sell irrevocable trusts -- she sells the vision of a life free of burdensome debt.

In much the same way, California Governor Arnold Schwarzenegger won the hearts and minds of voters not by outlining specific policies but by selling his vision of a state that's a better place to raise a family and do business.

"MAKING A DIFFERENCE." Intuit (INTU) founder Scott Cook told me, "It's important to communicate a bold vision for many reasons, but primarily for internal reasons, for the people in your company. Your people want to know that their work is adding up to a great cause. They want more than a paycheck. They want to know that they are making a difference in the world."

Are you communicating a bold, captivating vision? Do the people who work for you feel as though their work is adding up to more than a paycheck? Once they do, the results could be extraordinary. Remember the famous story of how Steve Jobs convinced former Pepsi (PEP) President John Scully to take the helm at Apple (AAPL)? On a balcony overlooking New York's Central Park, Jobs turned to Scully and asked, "Do you want to sell sugared water all your life, or do you want to change the world?"

It worked. In much the same way, articulating a big mission will help you win over employees, customers, and colleagues -- especially in a small business, where you can have direct and frequent contact with them.

WALK THROUGH FIRE? Last year, one of those traveling motivational conferences came rolling through my town in Silicon Valley. Its roster was made up of athletes, authors, and celebrities. I couldn't help but feel sorry for those business professionals who may have been fired up to do their best work by the end of the conference only to be deflated by their uninspiring supervisors the next day at the office.

CNBC money guru Jim Cramer once commented that people would walk through fire for Cisco's Chambers. Would they do the same for you? If not, why? Is it possible that you're failing to communicate the big mission behind your service, product, or company? In his autobiography, My American Journey, former Secretary of State Colin Powell writes that a great leader, "[makes] individuals feel important and part of something larger than themselves."

Sound familiar? The simple secret to inspiring those around you is to communicate a vision, a roadmap, of where you're heading and why it's important to your listeners. This technique applies to your conversations with employees, colleagues, customers, or investors.

START WITH YOURSELF. I hope I've already got you thinking about the mission you wish to convey. In my last column, I invited readers to share their "30-second pitch" with me (see BW Online, 5/4/05, "Mastering the 30-Second Pitch"). Many of you responded. Well, this time I'm asking that you share your "big mission." Let me know how you've inspired those who work for you or how you plan to do so based on the above information. I would be happy to send you feedback.

But just one more thing before you head out to articulate your big mission. Suze Orman once told me that people can only inspire when they're inspired themselves. Good point. If you're not inspired by your own vision, then it might be time to reevaluate the road you're heading down. After all, if you're not out to change the world, plenty of others are.

What Great .NET Developers Ought To Know (Part Two Mid-Level .NET Developer)

Continue on Scott Hanselman's - What Great .NET Developers Ought To Know
Part Two Mid-Level .NET Developer


  • Describe the difference between Interface-oriented, Object-oriented and Aspect-oriented programming
    Object-oriented programming (OOP): a programming methodology built around data abstraction (i.e. type, class and object) and message communication between objects. The basic concepts of OOP are encapsulation, inheritance, and polymorphism.

    Interface-oriented programming (IOP) is an extension of OOP in which all program interdependencies are expressed via abstract interfaces. Abstract interfaces and implementations are strictly separated. As a result, inheritance in IOP is interface-oriented, supports both specialization and adaptation inheritance, and is decoupled from the implementation binding mechanism. (from Generative Programming, Interface-Oriented Programming and Source Transformation Systems)

    Aspect-oriented programming (AOP) Eclipse Foundation defines AOP as:
    ‘A type or style of programming that explicitly takes into account crosscutting concerns, just as object-oriented programming explicitly takes into account classes and objects.’

    The essence of AOP model is to identify concerns such as logging, exception handling, etc. that cross-cut several layers in a system, and modularise them into orthogonal units called aspects and inject these aspects at selected execution points in a system dynamically at runtime.
    Pointers: Aspect Orienting .NET Components


  • Describe what an Interface is and how it’s different from a Class
    An interface defines a contract without implementation. A class can implement one or more interface. A class can only inherit one parent class. A interface can inherit many parent interfaces. There is no ‘implementation’ concept in interface. See this code snippet.

    namespace ClassLibrary1{
    public interface IFace1{
    void Foo1();
    void Bar1();
    }
    public interface IFace3{
    void Foo3();
    }
    public interface IFace2 : IFace1, IFace3{
    void Foo2();
    }
    public class Class1 : IFace2{
    public Class1(){}

    // IFace2 Members
    public void Foo2(){}

    // IFace3 Members
    public void Foo3(){}

    // IFace1 Members
    public void Foo1(){}
    public void Bar1(){}
    }
    }


  • What is Reflection?
    When building an assembly or a module, the .NET compiler creates metadata of the assembly at the same time. Metadata contains a type definition table, a field definition table, a module definition table etc. The process to query these metadata tables at runtime is called Reflection. The FCL’s System.Reflection namespace contains types a developer can use to write code to reflect metadata and obtain information about a type/class in the assembly, such like fields, methods, properties and events etc.


  • What is the difference between XML Web Services using ASMX and .NET Remoting using SOAP?
    Xml Web Services uses standard protocols such like HTTP, XML and SOAP in messaging. It addresses interoperability between disparate, heterogeneous applications.
    The .NET Remoting system enables messaging between remote object across AppDomain, process or machine boundaries using communication channels like TCP/IP or HTTP. It doesn’t address interoperability between heterogeneous applications, meaning client and service object must expose/understand the communication interface – object reference.
    Message sent over communication channel is called remote object. Remote object is encoded/decoded using native .NET serialization formatters, such like binary or XML encoding like SOAP. Binary encoding is more efficient. XML encoding is preferred choice when interoperability is required.

    Some brief Q&As about Remoting.


  • Are the type system represented by XmlSchema and the CLS isomorphic?
    Isomorphic: In mathematics, an isomorphism (in Greek isos = equal and morphe = shape) is a kind of interesting mapping between objects. (Reference) Two isomorphic sets (such as species) have a one-to-one correspondence between them. For each member of one set, there is a corresponding member of the other set. For example, there is a one-to-one correspondence between the set of lower case letters and the set of upper case letters, with a corresponding to A, etc. (Reference)
    .NET classes can be serialised into a XML Schema by using types in System.Xml.Serialization, such like XmlSerializer. (Some code example can be found here)
    CLS, Common Language Specification


  • Conceptually, what is the difference between early-binding and late-binding?
    Early-binding: static binding, validated by compiler. Late-binding: dynamic, type cast, validate by runtime.

    public class Point{
    public Point(){
    }
    public int X = 0;
    public int Y = 0;
    }
    public class Line : Point{
    public Line(){
    }
    }
    public class Widget{
    public Widget(){}
    }
    public class Foo{
    Line line1 = null;
    Widget widget = null;
    Object object1 = null;

    public void Demo() {
    //... do something with line1

    //early binding, complier will complain
    Point p1 = (Point)line1;
    Console.Write("Location (X,Y) ({0}, {1})", p1.X, p1.Y);

    //compiler complians on the cast
    //Point p2 = (Point)widget;

    //late binding on a generic type
    Point p3 = (Point)object1;

    //following line only throws exception at runtime
    Console.Write("Location (X,Y) ({0}, {1})", p3.X, p3.Y);
    }
    }


  • Is using Assembly.Load a static reference or dynamic reference? What is an Asssembly Qualified Name? Is it a filename? How is it different? Is this valid? Assembly.Load("foo.dll"); When would using Assembly.LoadFrom or Assembly.LoadFile be appropriate?
  • Using Assembly.Load(...) is a Dynamic Reference to load a type. The parameter can be an assembly qualified name (AQN, Type: string) like: ‘TopNamespace.SubNameSpace.ContainingClass+NestedClass,TheAssemblyName’.
    Assembly.Load also accepts a simple assembly name, which must be an object of AssemblyName, for example:

    AssemblyName myAssemblyName = new AssemblyName();
    myAssemblyName.Name = "MyAssembly";
    myAssemblyName.Version = new Version("1.0.0.2001");

  • ‘foo.dll’ is not a FQN, so Assembly.Load("foo.dll"); is invalid.
  • An AQN contains an assembly’s identity information, such like:
    • A simple file name (unencrypted name);

    • A version number;

    • A cryptographic key pair;

    • And a supported culture.

    It allows versioning and singing as opposed to a simple filename.
  • Assembly.LoadForm: Loads an assembly given its file name or path. Assembly.LoadFile: Loads the contents of an assembly file on the specified path. Use the LoadFile method to load and examine assemblies that have the same identity, but are located in different paths. Do not use LoadFile to load assemblies that you want to execute.

    Assembly SampleAssembly = Assembly.LoadFrom("c:\\Sample.Assembly.dll");
    Assembly SampleAssembly2 = Assembly.LoadFrom("c:\\Sample.Assembly2.dll");

    LoadFile does not load files into the LoadFrom context, and does not resolve dependencies using the load path, as the LoadFrom method does. LoadFile is useful in this limited scenario because LoadFrom cannot be used to load assemblies that have the same identities but different paths; it will load only the first such assembly.


  • How is a strongly-named assembly different from one that isn’t strongly-named?
    Strongly-named assemblies are signed using a private\public key pair which helps with code verification.
    signed assemblies could be placed in thee GAC.


  • Can DateTimes be null?
    No. It is a Structure an object of it is place in the stack.


  • What is the JIT? What is NGEN? What are limitations and benefits of each?
    Reference Compiling MSIL to Native Code
    Just-In-Time compilation. (In .Net context) complier produces MSIL (the portable executable); at the first time the assembly is called, it is compiled into CPU specific native code by the .NET JIT compiler. The benefit: portable executable – ‘write once, run everywhere’; JIT compiles the code and stores result native code in memory at initial call and subsequent calls is accessing in-memory native code hence it is relatively efficient.

    NGEN Pre-compilation, or pre-JITing, is the creation of native code from MSIL on the client machine, but instead of being generated at run time it is done as a separate isolated step. NGen is the term used generically for pre-JIT technology in the common language runtime (CLR), and specifically for the command-line utility used to create and store the native code. The native code produced by NGen is stored in true Win32® PE files which are called "native images" or "NGen images" which are in turn stored in the "native image cache" or "NGen cache."
    When creating native images, NGen simply loads the JIT compiler and invokes it to create native code for the assembly, which is then persisted into the native image and stored in the native image cache.
    NGEN is recommended as the last step during installation process, hence the name (native) install-time code generation?
    Note: Consideration on using NGen:
    • NGen is important to getting faster startup through better page sharing and working set reduction

    • NGen is primarily interesting for client scenarios which need the faster startup to be responsive

    • NGen is not recommend for Asp.Net because the assemblies NGen produces cannot be shared between App Domains

    • NGen for V1.0 and V1.1 was primarily designed for the CLR, and while it can be used for shared libraries and client apps:
      Always measure to make sure it is a win for your application
      Make sure your application is well behaved in the face of brittleness and servicing


  • How does the generational garbage collector in the .NET CLR manage object lifetime? What is non-deterministic finalization?
    CLR GC (Garbage Collection/Garbage Collector depends of the context) makes following assumptions:
    The newer an object is, the shorter its lifetime will be.
    The older an object is, the longer its lifetime will be.
    Collecting a portion of the heap is faster than collecting the whole heap.

    Generation memory threshold
    There are three (0, 1, 2) generations defined by CLR. When CLR initializes, it selects a memory threshold for each generation, say 256 Kb for g0, 2 Mb for g1 and 10 Mb for g2. GC self-tunes these thresholds to give an optimal GC frequency-process time.

    Generation Promotion
    All objects are initially put into g0. When threshold reached, GC starts compact the garbage objects. Survival objects are promoted into g1. GC won’t compact g1 until its threshold is reached. Vice versa, objects survive g1 GC are promoted into g2. When GC compacts an older generation, it also compacts younger generation(s).

    A Finalizable type implements Finalize method. GC calls this Finalize method to release the memory taken by the object. At least 2 GC processes are required to clear up finalizable objects. However, GC works on a separate thread and only starts when a generation threshold is full, there is no guarantee when Finalize will be called. This is the Non deterministic finalization.

    Finalizable types should always implement Dispose Pattern to allow GC deterministically disposes it.

  • What is the difference between Finalize() and Dispose()?
    Finalize is non public and only called by GC. Dispose is public and called by the user of the class. Call to Dispose to cleanup an resource triggers the Finalize being called by GC later (if Finalize is implemented).


  • How is the using() pattern useful? What is IDisposable? How does it support deterministic finalization?
    We use using(…){} on Disposable resource such like FileStream. This pattern gives a visual block of the scope that a resource will be available to use and enforce the Dispose by the close brace.

    IDisposable, an Interface defines Dispose() method. It should be implemented by all objects, resources that need Finalization in a deterministic way. Richter suggests a very useful Dispose Pattern in his book.

    using System;
    /// implemeting the IDisposable interface for Dispose Pattern
    public sealed class OSHandle : IDisposable{

    /// holds the Win32 handle of the unmanaged resource
    private IntPtr _handle;

    public OSHandle(IntPtr handle){
    this._handle = handle;
    }

    ///
    /// when garbage collected, this Finalize method is called to close the
    /// unmanaged resource's handle
    ///

    ~OSHandle(){
    Dispose(false);
    }

    ///
    /// this public method can be called to deterministically close the unmanaged resource's handle
    ///

    public void Dispose(){
    //because the object is explicitly cleaned up, stop the garbage collector from calling the
    //finalize method when this is running
    GC.SuppressFinalize(this);
    Dispose(true);
    }

    ///
    /// this public method can be called to deterministically close the unmanaged resource's handle
    ///

    public void Close(){
    Dispose();
    }

    ///
    /// this method is called by Finalize, Dispose and Close to do the actual cleanup
    /// OSHandle is made 'sealed' and this method is 'private'to protect it from be called -
    /// the derived class need to implement their own cleanup then call this one.
    /// If OSHandle cannot be 'sealed' this method need to be virtual protected
    ///

    ///

  • private void Dispose(bool disposing){
    //Synchronize threads calling Dispose/Close simutaneously
    if (disposing){
    //the object is being explicitly disposed of/closed, not finalized. It is therefore
    //safe for code here to access fields that reference other objects because the Finalize
    //method of these ther objects hasn't yet been called

    //For our sample OSHandle class, we have nothing to do here
    }

    // The object is being disposed of/closed or finalized
    if (IsValid){
    // close the unmanaged resource
    CloseHandle(this._handle);

    // set the handle to some sentinel value. This prcaution prevents the possiblity of
    // calling CloseHandle twice
    this._handle = InvalidHandle;
    }
    }

    //Make this property return an invalid value for whatever unmanaged resource you are using
    public IntPtr InvalidHandle {get {return IntPtr.Zero; }}

    //Public method to return the wrapped handle
    public IntPtr ToHandle () {return this._handle ; }

    //public implicit cast operator returns the wrapped handle
    public static implicit operator IntPtr(OSHandle osHandle){
    return osHandle.ToHandle();
    }

    public bool IsValid {get {return (this._handle != InvalidHandle);}}
    public bool IsInvalid {get {return !IsValid;}}

    //private method called to free the unmanaged resource
    [System.Runtime.InteropServices.DllImport("Kernel32")]
    private extern static bool CloseHandle(IntPtr handle);
    }

    Pointer: Chapter 19 of Jeffery Richter’s Book Applied Microsoft .NET Framework Programming


  • What does this useful command line do? tasklist /m "mscor*"
    TASKLIST [/S system [/U username [/P [password]]]] [/M [module] | /SVC | /V] [/FI filter] [/FO format] [/NH]

    Description: This tool displays a list of currently running processes on either a local or remote machine.


  • What is the difference between in-proc and out-of-proc? What technology enables out-of-proc communication in .NET?
    Procedure call in out-of-proc requires object sterilization/marshalling. .Net Remoting enables out-of-proc communication in .NET.


  • When you’re running a component within ASP.NET, what process is it running within on Windows XP? Windows 2000? Windows 2003?
    In Windows XP and Windows 2000 (IIS5), ASP.NET is running in ASPnet_wp.exe worker process. In Windows 2003 (IIS6), ASP.NET is running in W3WP.exe worker process.
    P.S. Worker Process is the Application(s) pool that web application is assigned to.

  •