posted on Friday, September 17, 2004 10:27 AM by chris_p_bowen

Jeff Prosise on Power ASP.NET 2.0 Programming

"Power ASP.NET 2.0 Programming"
Jeff Prosise
Boston .NET User Group
September 15, 2004

Jeff Prosise

Jeff Prosise of Wintellect was on hand (courtesy of INETA) to deliver the night's main session.  He wanted to “give us a break" from what he called the usual ASP.NET 2.0 talks, so he focused on topics that have not been given much limelight to date in the Whidbey world.   (Thanks to Chris Pels for use of the photo!)


ASP.NET Client Callback Manager
 
This feature in ASP.NET allows pages to perform lightweight callbacks to the server.  The approach avoids having to fully process and refresh the page, therefore there is no page flashing and, more importantly, much less load on the server/network.  It does this by using Microsoft's XMLHTTP ActiveX control under the covers to make a call to the server, which returns a simple string which can be parsed on the client with JavaScript.

To do this, you create a JavaScript client-side function which will be called when the server has completed the callback.  The approach Jeff showed was embedding this script as a string in the codebehind and registering this on the page with RegisterClientScriptBlock().  You wire up the page to make the callback to the server by calling GetCallbackEventReference() and adding this to a specific control using .Attributes.Add("onclick",...)

The page which receives the callback must implement ICallbackEventHandler.  This simple interface requires implementation of the RaiseCallbackEvent() method.  This receives the callback from the browser, accepting a string which the client can use to communicate what it needs.  The method returns a string that can be parsed by the previously registered client-side JavaScript function.

This approach is very useful to update client information when it is changed on the server.  Jeff showed a simple example page which displayed details of three stocks.  The page was configured to callback to the server every few seconds.  Remember, this action requires very little overhead, unlike using the Meta-Refresh approach commonly used today.  The page was driven from a simple XML file.  When he made a change to the XML file, updating a stock price, almost immediately after he saved the file, the rendered page displayed the updated price.

Unfortunately, because the callback manager functionality relies on XMLHTTP, this (generally) limits clients to Internet Explorer only.

For more information, there is an article on the Callback Manager here:
http://www.ondotnet.com/lpt/a/5035


Configuration API

This new API in ASP.NET 2.0 allows easy management of configuration settings.  It merges all settings up the configuration "chain" (e.g. machine.config, web.config, etc.)

A powerful feature of this API is support for encryption.  You can encrypt and decrypt almost all sections of your configuration files, including any user-defined sections.  There are some exceptions, such as the section, which needs to be accessed outside of ASP.NET by some IIS ISAPI code and therefore cannot be encrypted.

Encryption is performed through the ProtectSection()/UnProtectSection() methods, which can be configured to use either RSA or 3DES.  They key is stored in the operating system (via DPAPI) in the Local Security Authority (LSA).  While this avoids the catch-22 of putting it inside a configuration file, it requires running separate installation functions on each server to get the key into the LSA.  This means you cannot simply rely on XCopy deployment for initial server launch.

ASP.NET 2.0 supports a new configuration section called .  As the name suggests, it allows for simple and, via encryption through the Configuration API, secure storage of database connection strings.  It is important to note that this is not under the section.  Having them separate adds another layer of safety.

There is currently no visual tool in Visual Studio for these features, you must rely on programming against the API.  Jeff hoped out loud that the VS team might have time to include one in the Beta 2 timeframe.

Here is a QuickStart sample on these features.


Health Monitoring Subsystem

With the new Health Monitoring subsystem, you can configure ASP.NET to send notifications of errors as they occur.  These notifications can contain exception details, stack trace, browser information, request headers and more.

You specify settings under the new  section of web.config and machine.config.  You can define application events and friendly names with <eventMappings>, create notification targets with , and use to map an event to one or more of those providers (e.g. email, Windows event log, a file, database, WMI, etc.)  For example, you can easily have all unhandled exceptions sent to a debugging email address.

You can also configure filters which indicate maximum frequencies for notifications.  In other words, only send one email for this error every 10 minutes.  This is quite useful to prevent flooding of email boxes or database servers should an error result from a commonly-accessed page.

In addition, the subsystem can be configured to send out periodic "heartbeats" of status details, which allows external monitoring to ensure that a target application is running as expected.

You can easily define custom events to plug into the subsystem.  Your class must derive from WebBaseEvent (or WebBaseErrorEvent) and override FormatCustomEventDetails() to return a string representing as much or as little detail as you'd like.  The basic properties are message, source and eventID, but it is easy to add your own.

It's clear the ASP.NET 2.0 team took notice of supplemental logging and exception management frameworks such as the Exception Management Application Block and the Enterprise Instrumentation Framework.  At Monster, we've long relied on the Exception Block for just these purposes (sending email, logging to database, etc.), so it's great to see native support for these actions will be coming.  I wonder how Microsoft will guide developers in choosing between these features and those of the Microsoft's/Avanade's Enterprise Library's Exception Block

Ben Lovell has a post on Health Montioring with an example project:
http://bitarray.co.uk/ben/articles/245.aspx

UPDATE:  Fellow Boston .NET UG member Girish Bharadwaj was inspired by Jeff's talk and wrote a very good post on the health monitoring subsystem.


"$" Expressions and Expression Builders

Jeff demonstrated a new syntax usable from ASP.NET pages to load values from resource files, configuration files and even custom sources.  For example:

" Runat="server">

This accesses the section of web.config and returns the value found at the "SomeKey" node.  You could use syntax like to return the database connection string as configured in the (new) section of web.config.  Similar functionality can be used from the codebehind as well.

You can create your own custom expression builders by deriving from System.Web.Compilation.ExpressionBuilder and overriding the CodeBLOCKED EXPRESSION method.  You register them in the section of web.config.  You can then reference your custom builder with the same syntax as above (e.g. ).
 
Fredrik Normen has a great post on this:
http://normen.mine.nu/viewpost.aspx?PostID=161

Here is Jeff's own post on the subject:
http://wintellect.com/WEBLOGS/wintellect/archive/2004/07/22/295.aspx


Multiple Logical Forms and Validation Groups

Supporting the concept of multiple logical forms has always been a challenge in ASP.NET.  You might have two submit buttons, each logically associated with different sets of fields, but clicking either would invoke all of the validators on the page, not generally what was intended.

With 2.0, all controls support the concept of "validation groups" via a ValidationGroup property.  You assign the same name to all of the controls, including validators, in your logical form.  When you submit your page, only the validators associated with the current group are executed.

On the server side, you can still use Page.IsValid() as this reflects only the state of the validators group selected with the last postback.  In addition, you can check a specific group using Page.IsValid("TheGroupName").

A simple example can be found at Johan Normen's blog:
http://www.nsquared2.net/johan/viewpost.aspx?PostID=51
 

Default Actions
 
Another source of difficulty prior to 2.0 has been assigning default buttons and actions to fields which receive an "enter" keypress.  A large number of custom workarounds exist, but with 2.0, a simple native solution exists.

You can now add a Panel control to your form and set the DefaultButton property to point to the control you wish to receive a click when enter is pressed in one of the Panel's controls.  The Form supports the DefaultButton property as well.

If you're looking for a solution for your current applications, look at the Default Button workspace on GotDotNet.
 
 
Image Generation Service

Jeff discussed features in ASP.NET surrounding generation of images and how retrieving images from a data store and displaying on a page is very straightforward.
 
Unfortunately, this functionality is to be gutted with the Beta 2 release.  Jeff showed a simple workaround where the IMG tag's src property points to a custom HttpHandler (e.g. "http://example.com/imageGenerator.ashx?width=100&height=50&Text=Hello")

An example of creating this HttpHandler can be found here:
http://weblogs.asp.net/ShankuN/archive/2004/08/16/215487.aspx


Conclusion

It was a great night.  Jeff is a fantastic speaker and I'm sure everyone learned at least one Nugget of Usefulness.  Many thanks to INETA for footing the bill and to Jeff for making the time for us.

-Chris

Comments