Monday, September 29, 2008
.Net Remoting
Wednesday, September 24, 2008
Pakistan Ranks 62nd in Global IT Competitiveness Index
Tuesday, September 23, 2008
As time passes, the data source may change in response to other actions. However, if your code uses caching, you may remain unaware of the changes and continue using out-of-date information from the cache. To help mitigate this problem, ASP.NET supports cache dependencies. Cache dependencies allow you to make a cached item dependent on another resource so that when that resource changes the cached item is removed automatically. ASP.NET includes three types of dependencies:
- Dependencies on other cache items.
- Dependencies on files or folders.
- Dependencies on a database query.
Before you can use SQL Server cache invalidation, you need to enable notifications for the database. This task is performed with the aspnet_regsql.exe command-line utility, which is located in the c:\[WinDir]\Microsoft.NET\Framework\[Version] directory. To enable notifications, you need to use the -ed command-line switch. You also need to identify the server (use -E for a trusted connection and -S to choose a server other than the current computer) and the database (use -d). Here's an example that enables notifications for the Northwind database on the current server:
The AspNet_SqlCacheTablesForChangeNotification contains a single record for every table you're monitoring. When you make a change in the table (such as inserting ,deleting or updating a record), the changeId column is incremented by 1 -see AspNet_SqlCacheUpdateChangeIdStoredProcedure procedure-. ASP.NET queries this table repeatedly and keeps track of the most recent changeId values for every table. When this value changes in a subsequent read, ASP.NET knows that the table has changed.
The caching still works in the same way. That means if any product record is touched, even if the product resides in another category (and therefore isn't one of the cached records), the notification is still sent and the cached item is considered invalid. Also keep in mind it doesn't make sense to cache tables that change frequently.
To enable ASP.NET polling , you need to use the
Creating the Cache Dependency:
Now that we've seen how to set up a database to support SQL Server notifications, the only remaining detail is the code, which is quite straightforward. We can use our cache dependency with programmatic data caching, a data source control, and output caching.
For programmatic data caching, we need to create a new SqlCacheDependency and supply that to the Cache.Insert() method. In the SqlCacheDependency constructor, you supply two strings. The first is the name of the database you defined in the element in the section of the web.config file e.g: Northwind. The second is the name of the linked table e.g: Products.Example:private static void CacheProductsList(List
SqlCacheDependency sqlDependency = new SqlCacheDependency("Northwind", "Products");HttpContext.Current.Cache.Insert("ProductsList", products, sqlDependency, DateTime.Now.AddDays(1), Cache.NoSlidingExpiration);
}
private static List
return HttpContext.Current.Cache["ProductsList"] as List
}
NWProductItem is business class, and here we are trying to cache a list of NWProductItem instead of DataSet or DataTable.The following method is used by an ObjectDataSource Control to retrieve List of Products
public static List
//Try to Get Products List from the CacheList
//Products List not in the cache, so we need to query the Database by using a Data LayerNWProductsDB db = new NWProductsDB(_connectionString);DbDataReader reader = null;products = new List
if (catId > 0){
//Return Product List from the Data Layerreader = db.GetProductsList(catId);
}else{
//Return Product List from the Data Layerreader = db.GetProductsList();
}//Create List of Products -List if NWProductItem-products = BuildProductsList(reader);reader.Close();//Add entry to products list in the CacheCacheProductsList(products);
}products.Sort(new NWProductItemComparer(sortBy));if (sortBy.Contains("DESC")) products.Reverse();return products;
}
To perform the same trick with output caching, you simply need to set the SqlDependency property with the database dependency name and the table name, separated by a colon: <%@ OutputCache Duration="600" SqlDependency="Northwind:Products" VaryByParam="none" %>
<%@ OutputCache Duration="600" SqlDependency="Northwind:Products" VaryByParam="none" %>The same technique works with the SqlDataSource and ObjectDataSource controls:
Monday, September 22, 2008
ASP.NET Impersonation
Note Impersonation can significantly affect performance and scaling. It is generally more expensive to impersonate a client on a call than to make the call directly.
Using impersonation, ASP.NET applications can optionally execute the processing thread using the identity of the client on whose behalf they are operating. You usually use impersonation for resource access control. Delegation is a more powerful form of impersonation and makes it possible for the server process to access remote resources while acting as the client.
- Impersonation is disabled. This is the default setting. For backward compatibility with ASP, you must enable impersonation and change the ASP.NET process identity to use the Local System account. In this instance, the ASP.NET thread runs using the process token of the application worker process regardless of which combination of IIS and ASP.NET authentication is used. By default, the process identity of the application worker process is the ASPNET account.
- Impersonation enabled. In this instance, ASP.NET impersonates the token passed to it by IIS, which is either an authenticated user or the anonymous Internet user account (IUSR_machinename).
- Impersonation enabled for a specific identity. In this instance, ASP.NET impersonates the token generated using an identity specified in the Web.config file.
userName="domain\user"
password="password" />
If the application resides on a UNC share, ASP.NET always impersonates the IIS UNC token to access that share unless a configured account is used. If you provide an explicitly configured account, ASP.NET uses that account in preference to the IIS UNC token.
You should exercise care when using impersonation because it makes it possible for an application to potentially process code using permissions not anticipated by the application designer. For example, if your application impersonates an authenticated intranet user, that application possesses administrative privileges when impersonating a user with those privileges. Likewise, if the impersonated user possesses more restrictive permissions than anticipated, the user may not be able to use the application.
Delegates in C#.Net
A delegate type maintains three important pices of information :
1. The name of the method on which it make calls.
2. Any argument (if any) of this method.
3. The return value (if any) of this method.
when you want to create a delegate in C# you make use of delegate keyword.The name of your delegate can be whatever you desire. However, you must define the delegate to match the signature of the method it will point to. fo example the following delegate can point to any method taking two integers and returning an integer.public delegate int DelegateName(int x, int y);A Delegate Usage Example
namespace MyFirstDelegate
{
//This delegate can point to any method,
//taking two integers and returning an
//integer.
public delegate int MyDelegate(int x, int y);
//This class contains methods that MyDelegate will point to.
public class MyClass
{
public static int Add(int x, int y)
{
return x + y;
}
public static int Multiply(int x, int y)
{
return x * y;
}
}
class Program
{
static void Main(string[] args)
{
//Create an Instance of MyDelegate
//that points to MyClass.Add().
MyDelegate del1 = new MyDelegate(MyClass.Add);
//Invoke Add() method using the delegate.
int addResult = del1(5, 5);
Console.WriteLine("5 + 5 = {0}\n", addResult);
//Create an Instance of MyDelegate
//that points to MyClass.Multiply().
MyDelegate del2 = new MyDelegate(MyClass.Multiply);
//Invoke Multiply() method using the delegate.
int multiplyResult = del2(5, 5);
Console.WriteLine("5 X 5 = {0}", multiplyResult);
Console.ReadLine();
}
}}