Monday, July 23, 2007

Web.Config: Read ConnectionStrings from Web.Config

Read Connection String from Web.Config:

System.Configuration.Configuration rootWebConfig =
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(“/WebsiteRoot”);
System.Configuration.ConnectionStringSettings connString;

if(0< rootWebConfig.ConnectionStrings.ConnectionStrings.Count)
{
connString = rootWebConfig.ConnectionStrings.ConnectionStrings[“NorthwindConnectionString”];

if(null != connString)
Console.WriteLine(“Northwind conn string = \” {0}\”, connString.ConnectionString);
else
Console.WriteLine(“Conn string not found..”);

}

.net Framework : JIT Just in time compilation

JIT Compilation: Just in time:

•JIT compiler connverts MSIL to a native code

•Native code is CPU specific that runs on the same computer architecture as the JIT compiler

•JIT compilation takes into account the fact that some code may never get called during the execution

•Rather then converting entire MSIL into native code, it converts the MSIL as needed during the execution and stores the native code so that it is accessible for subsequent calls

Reflection: Basics, Code Listing

Reflection:
• Reflection provides objects that encapsulate assemblies, modules and types
• Reflection can be used to dynamically create an object of the class
• Bind an object to a class, or get type from an object
• Also invoke the object’s methods and access its field and properties

Uses:
• Use Assembly to load and define assembly
• Load modules that are listed in the assemly manifest
• See inside a module to find out assembly that contains the module and classes inside that module
• Classes in System.Reflection.Emit allow to build types at runtime

Viewing Type Information:
• System.Type class is central to reflection. CLR runtime creates the Type for a loaded type when reflection requests it
• Use Assembly.GetType or GetTypes to obtain Type objects from assemblies that have not been loaded

Code Listing: Reflection:

using System;
using System.Reflection;

class ListMembers
{
public static void Main(Stringp[] args)
{
Type t = typeof(System.String);
Console.WriteLine(“Listing public constructors of the {0} type ”, t);

ConstructorInfo[] ci = t.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
Console.WriteLine(“Constructors”);
PrintMembers(ci);
}

public static void PrintMembers(MemberInfo[] ms)
{
foreach(MemberInfo m in ms)
{
Console.WriteLine(“{0}{1}”, “ “, m);
}
Console.WriteLine();

}

}

Steps in getting data: Theory

Steps in Getting Data from the Database:
1. Create the data connection object
2. Create the Data adapter object
3.Create the dataset object
4.Invoke methods on adapter object to fill or update the data set
5.Use data binding to display the data from the data set

Sunday, July 22, 2007

Threading: Basics, Code Listing

Threading:
• A thread is the basic unit to which the OS allocates processor time
• Namespace: System.Threading
• A .net program has minimum of two threads, main and garbage collector

Code Listing: Threading

using System;
using System.Threading;

public class Worker
{
public void DoWork()
{
while(! _shouldStop) { Console.WriteLine(“Worker thread working...”); }
Console.WriteLine(“Worker thread terminating...”);
}

public void RequestStop()
{
_shouldStop = true;
}

private volatile bool _shouldStop;
}

public class WorkerThreadExample
{
static void Main()
{
Worker workerobject = new Worker();
Thread workerThread = new Thread(workerObject.DoWork);

workerThread.Start();
Console.WriteLine(“Main thread:starting worker thread...”);

while(workerThread.IsAlive);

Thread.Sleep(1);
workerObject.RequestStop();

workerThread.Join();
Console.WriteLine(“Main Thread: Worker thread terminated...”);
}
}

Object Pooling

ObjectPooling:
• Avoid the overhead of creating objects from scratch
• When an object is activated, it is pulled from the pool, on deactivation, it is placed back
• Pooling can be configured by applying the ObjectPoolingAttribute to a class
• ObjectPoolingAttribute derives from System.EnterpriseServices.ServicedComponent class
• Object pooling allows controlling the number of connections used
• Object pooling enforces minimums and maximums, precaution needed when defining maximum

Code Listing: ObjectPooling

[ObjectPooling(Enabled=true; MinPoolSize=2; MaxPoolSize=5; CreationTimeout=20000)]
public class TestObjectPooling : ServicedComponent
{

public void Perform() {}
protected override void activate() {}
protected override void Deactivate() {}

protected override bool CanBePooled()
{
return true;
}

}

DCOM: Theory

DCOM:
• Allows creation of objects distributed across a network
• DCOM provides a protocol for invoking that object’s methods, and secure access to the object
• DCOM provides a wrapper around COM, and hence it is backward compatible
• DCOM uses Remote Procedure Call
• Protocol gets registered just prior to use rather then at initialization
• To convey alive status of client, periodic pinging is used
• In DCOM, multiple QueryInterfaces are all clustered into one call

DCOM Call Security:
• Authentication: Prevent false clients from impersonating the true client
• Authorization: Client’s actions are appropriate
• Data integrity: Ensure data was not tampered during transit
• Data privacy: Ensure only authorized users can access it