Showing posts with label Dot Net. Show all posts
Showing posts with label Dot Net. Show all posts

Wednesday, March 24, 2010

Getting into the .NET runtime

The IISAPIRuntime interface acts as the interface point between the unmanaged code coming from the ISAPI extension (directly in IIS 6 and indirectly via the Named Pipe handler in IIS 5). If you take a look at this class you’ll find a ProcessRequest method with a signature like this:

[return: MarshalAs(UnmanagedType.I4)]
int ProcessRequest([In] IntPtr ecb, [In, MarshalAs(UnmanagedType.I4)]
int useProcessModel);

The ecb parameter is the ISAPI Extension Control Block (ECB) which is passed as an unmanaged resource to ProcessRequest. The method then takes the ECB and uses it as the base input and output interface used with the Request and Response objects. An ISAPI ECB contains all low level request information including server variables, an input stream for form variables as well as an output stream that is used to write data back to the client. The single ecb reference basically provides access to all of the functionality an ISAPI request has access to and ProcessRequest is the entry and exit point where this resource initially makes contact with managed code.

The ISAPI extension runs requests asynchronously. In this mode the ISAPI extension immediately returns on the calling worker process or IIS thread, but keeps the ECB for the current request alive. The ECB then includes a mechanism for letting ISAPI know when the request is complete (via ecb.ServerSupportFunction) which then releases the ECB. This asynchronous processing releases the ISAPI worker thread immediately, and offloads processing to a separate thread that is managed by ASP.NET.

ASP.NET receives this ecb reference and uses it internally to retrieve information about the current request such as server variables, POST data as well as returning output back to the server. The ecb stays alive until the request finishes or times out in IIS and ASP.NET continues to communicate with it until the request is done. Output is written into the ISAPI output stream (ecb.WriteClient()) and when the request is done, the ISAPI extension is notified of request completion to let it know that the ECB can be freed. This implementation is very efficient as the .NET classes essentially act as a fairly thin wrapper around the high performance, unmanaged ISAPI ECB.

ref:west-wind

Saturday, February 13, 2010

From Browser to ASP.NET

Let’s start at the beginning of the lifetime of a typical ASP.NET Web Request. A request starts on the browser where the user types in a URL, clicks on a hyperlink or submits an HTML form (a POST request). Or a client application might make call against an ASP.NET based Web Service, which is also serviced by ASP.NET. On the server side the Web Server – Internet Information Server 5 or 6 – picks up the request.

At the lowest level ASP.NET interfaces with IIS through an ISAPI extension. With ASP.NET this request usually is routed to a page with an .aspx extension, but how the process works depends entirely on the implementation of the HTTP Handler that is set up to handle the specified extension. In IIS .aspx is mapped through an ‘Application Extension’ (aka. as a script map) that is mapped to the ASP.NET ISAPI dll - aspnet_isapi.dll. Every request that fires ASP.NET must go through an extension that is registered and points at aspnet_isapi.dll.

Depending on the extension ASP.NET routes the request to an appropriate handler that is responsible for picking up requests. For example, the .asmx extension for Web Services routes requests not to a page on disk but a specially attributed class that identifies it as a Web Service implementation. Many other handlers are installed with ASP.NET and you can also define your own. All of these HttpHandlers are mapped to point at the ASP.NET ISAPI extension in IIS, and configured in web.config to get routed to a specific HTTP Handler implementation. Each handler, is a .NET class that handles a specific extension which can range from simple Hello World behavior with a couple of lines of code, to very complex handlers like the ASP.NET Page or Web Service implementations.

For now, just understand that an extension is the basic mapping mechanism that ASP.NET uses to receive a request from ISAPI and then route it to a specific handler that processes the request.

Ref:west-wind

Thursday, February 11, 2010

The ISAPI Connection

ISAPI is a low level unmanged Win32 API. The interfaces defined by the ISAPI spec are very simplistic and optimized for performance. They are very low level – dealing with raw pointers and function pointer tables for callbacks - but they provide he lowest and most performance oriented interface that developers and tool vendors can use to hook into IIS. Because ISAPI is very low level it’s not well suited for building application level code, and ISAPI tends to be used primarily as a bridge interface to provide Application Server type functionality to higher level tools. For example, ASP and ASP.NET both are layered on top of ISAPI as is Cold Fusion, most Perl, PHP and JSP implementations running on IIS as well as many third party solutions such as my own Web Connection framework for Visual FoxPro. ISAPI is an excellent tool to provide the high performance plumbing interface to higher level applications, which can then abstract the information that ISAPI provides. In ASP and ASP.NET, the engines abstract the information provided by the ISAPI interface in the form of objects like Request and Response that read their content out of the ISAPI request information. Think of ISAPI as the plumbing.

For ASP.NET the ISAPI dll is very lean and acts merely as a routing mechanism to pipe the inbound request into the ASP.NET runtime. All the heavy lifting and processing, and even the request thread management happens inside of the ASP.NET engine and your code.

As a protocol ISAPI supports both ISAPI extensions and ISAPI Filters. Extensions are a request handling interface and provide the logic to handle input and output with the Web Server – it’s essentially a transaction interface. ASP and ASP.NET are implemented as ISAPI extensions. ISAPI filters are hook interfaces that allow the ability to look at EVERY request that comes into IIS and to modify the content or change the behavior of functionalities like Authentication. Incidentally ASP.NET maps ISAPI-like functionality via two concepts: Http Handlers (extensions) and Http Modules (filters).

ref:west-wind

Tuesday, November 10, 2009

Create and Format an Excel in Asp.net

A simple code snippet to create excel and write some date to cell from C#.
Step 1: Add reference to " microsoft excel 12.0 object library " in the project.
Step 2: Include the namespace " using Excel = Microsoft.Office.Interop.Excel; "
Step 3: In the click event of export button:
string strCurrentDir = Server.MapPath(".") + "\\";
Excel.Application excel = new Excel.ApplicationClass();
excel.Visible = false;
Excel._Workbook workbook = excel.Workbooks.Add(Missing.Value);
Excel.Sheets sheets = workbook.Worksheets;
Excel._Worksheet exlSheet = (Excel.Worksheet)sheets.get_Item("Sheet1");
summarySheet.Name = "Report Name";
Excel.Range headerRng = (Excel.Range)exlSheet .get_Range("A1", "B1");
headerRng.MergeCells = true; headerRng.Value2 = "Report Header";
headerRng.Font.Bold = true;
headerRng.Font.Name = "Arial";
headerRng.Font.Size = 18;
headerRng.WrapText = true;
headerRng.HorizontalAlignment = Excel.Constants.xlCenter;
headerRng.Interior.Color = System.Drawing.Color.Gray.ToArgb();
headerRng.Borders.Weight = 3;
headerRng.Borders.LineStyle = Excel.Constants.xlSolid;
headerRng.Cells.RowHeight = 30;
headerRng.EntireColumn.AutoFit();

workbook.SaveAs(strCurrentDir + "ExportReport.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, null, null, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, false, false, null, null, null);

Sunday, September 27, 2009

How handle exception in VB.NET ?

VB.NET has an inbuilt class that deals with errors. The Class is called Exception. When an exception error is found, an Exception object is created. The coding structure VB.NET uses to deal with such Exceptions is called the Try … Catch structure.

Syntax:TryCatch ex As ExceptionEnd TryThe Try word means "Try to execute this code". The Catch word means "Catch any errors here". The ex is a variable, and the type of variable it is an Exception object.

Example:Tryrt1.LoadFile ("C:\test10.txt", RichTextBoxStreamType.PlainText)Catch ex As Exception MsgBox (ex. Message) End Try

When you run your program, VB.net will try to execute any code in the Try part. If everything goes well, then it skips the Catch part. However, if an error occurs, VB.NET jumps straight to Catch and exception message in message box is executed.

Wednesday, February 11, 2009

Advantage of ReadOnly over Constant variables in dotnet

Constant variables are compile time constants whereas ReadOnly variables are runtime constants. Compile-time constants are slightly faster, but far less flexible, than runtime constants. Reserve the compile-time constants for when performance is critical and the value of the constant will never change over time.

EXAMPLE:
// Compile time constant:public const int _Millennium = 2000;
// Runtime constant:public static readonly int _ThisYear = 2004;

Also, you can use readonly values for instance constants, storing different values for each instance of a class type. Compile-time constants are, by definition, static constants.
Computers Add to Technorati Favorites Programming Blogs - BlogCatalog Blog Directory