7/24/2008
LifeCycle Solutions - Home ( the software development blog )
 

<July 2008>
SunMonTueWedThuFriSat
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

Subscribe to this feed:

RSS 2.0 | Atom 1.0 |CDF





Add to Technorati Favorites

Thursday, April 19, 2007

ASP.NET Health Monitoring is one of the more under-hyped features of ASP.NET.  It is a rich, flexible logging framework that can be used to log application events in a database, flat file, email, or custom source.  However, JavaScript errors, since they occur on the client-side, do not get the benefits of this framework.   Fortunately this can be fixed by linking the client to the server with a little Ajax.  James Newton-King has posted a great solution to do just this in ASP.NET. 

In this solution, a small script to capture errors from window.error is registered on the client.  These errors are wrapped in XML and sent to the server via XmlHttp.  On the server, an HttpHandler unwraps the XML and logs a Web Event for the exception- including browser information.  Viola!  Client-side errors in your application's event log.  This is extremely useful for debugging problem scripts and alerting you to problems in your application you may otherwise miss.

Posted by Daniel Root

Saturday, April 14, 2007

The Patterns & Practices team at Microsoft recently released the 3.0 version of the ever-useful Enterprise Library.  Among the changes in the current iteration of the library is the addition of a new application block - the Policy Injection Application Block ("PIAB").

The purpose of the PIAB is to provide developers a way to define "policies" that can be applied selectively throughout an application.  Imagine that you're building a human resources application and one of the requirements is that the application provide an audit trail for all changes made to an employee's personnel record.  The PIAB allows you to define this requirement as a "policy" and configure its behavior external to your application.

Here's how it works:

  • Using the Enterprise Library Configuration tool, you define a policy (in this case we'll name it "Audit Policy")
  • Each policy can have any number of associated "matching rules"; these rules define how/when the policy is applied.  The PIAB provides the following rules out-of-the-box:
    • Assembly Matching Rule
    • Custom Attribute Matching Rule
    • Member Name Matching Rule
    • Method Signature Matching Rule
    • Namespace Matching Rule
    • Parameter Type Matching Rule
    • Property Matching Rule
    • Return Type Matching Rule
    • Tag Attribute Matching Rule
    • Type Matching Rule
  • Each policy can also have any number of "handlers"; handlers determine what happens when a policy is executed.  The PIAB provides the following handlers, which utilize other Enterprise Library application blocks, out-of-the-box:
    • Authorization Handler
    • Caching Handler
    • Exception Handling Handler
    • Logging Handler
    • Performance Counters Handler
    • Validation Handler
  • In our HR application example, we'll elect to use a "Tag Attribute Matching Rule" that allows us to mark methods in our business tier classes as requiring audit functionality; in addition, we'll choose to apply the Enterprise Library's Logging Application Block as our policy's handler.

     The resulting XML in our application's configuration file looks like this (the Logging Application Block section has been omitted for brevity):

<policyInjection>
<policies>
<add name="AuditPolicy">
<matchingRules>
<add match="Audit" ignoreCase="false" 
  type=
  "Microsoft.Practices.EnterpriseLibrary.PolicyInjection.MatchingRules.TagAttributeMatchingRule,
  Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=3.0.0.0,
  Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
  name="Tag Attribute Matching Rule" />
</matchingRules>
<handlers>
<add logBehavior="After" beforeMessage="" afterMessage="" eventId="0"
  includeParameterValues="true" includeCallStack="false" includeCallTime="true"
  priority="-1" severity="Information"
  type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.LogCallHandler,
  Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers,
  Version=3.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
  name="Logging Handler">
<categories>
<add name="General" />
</categories>
</add>
</handlers>
</add>
</policies>
</policyInjection>


  • In our application, we're going to mark each method that requires an audit trail with a "Tag" attribute...notice that "Audit" corresponds to our matching rule defined above:
    /// <summary> /// Note: class must inherit MarshalByRefObject /// for use with the PIAB /// </summary> class Employee : MarshalByRefObject { [Tag("Audit")] public void Save() { //write employee to data store } [Tag("Audit")] public void Delete() { //remove employee from data store } }
  • The "policy injection magic" performed by the PIAB will occur as a result of the way we create an instance of our Employee class: 

    Employee ourEmployee = PolicyInjection.Create<Employee>(); ourEmployee.Save();

    The Create() factory method actually builds and returns a "proxy class" -- in this example, the proxy class intercepts our call to the Save() method, evaluates the matching criteria and determines that the handler should be executed because of the "Audit" tag match.  The result in this case is that an entry is written to the Windows event log as defined by our Logging Application Block configuration.

The "policy injection" pattern is worth a look if you have common functionality that needs to be applied throughout your application, and you want to do so in a declarative, transparent way.

Posted by Brian Parks

Tuesday, April 03, 2007


Like most users, I'm finding the switch from Office 2003 to Office 2007 to be relatively painless, EXCEPT, I've been consistently experiencing a delay when switching between instances of Word.  It finally annoyed me enough to research the issue, and I located the solution here...the culprit turns out to be the Acrobat PDFMaker COM Add-In.

Note that this post is not SD-related, directly, except that waiting on Word to switch windows cuts into one's development time.  =)
Posted by Brian Parks

Monday, April 02, 2007

In the slides from Scott Guthrie's 'ASP.NET Tips and Tricks' TechEd Presentation, I ran across this gem.  You can turn off debugging server-wide by adding the following to <system.web> section in the server's machine.config file (typically located in C:\windows\Microsoft.NET\Framework\v<version>\Config):

       <deployment retail="true"/>

This is a great backup in case developers forget to turn it off in the application using <compilation debug="false"/>.  Scott also mentions a few of the negative results of forgetting this little setting:

  • Slower performance due to debug code
  • App uses much more memory
  • Client-side scripts are not cached
Posted by Daniel Root

© 2006 LifeCycle Solutions, LLC | All Rights Reserved