Musings by Generator

Development, Life and everything else in S.A.

Aspect Orientated Programming with PostSharp.

I have recently discovered Aspect Orientated Programming or AOP, I know it is a bit late but rather late than never. Wikipedia has a great article on it: Aspect-Orientated Programming. The real benefit of AOP is that you remove all code that is not core to your functionality within a method such as Logging and Exception Handling.

It allows you to have crosscutting code in one location and if you make any changes to the signature of the code, you don’t have to go wading through all your code changing method calls etc.

I have been using a great extension called PostSharp, made by sharpCrafters. it comes with a free Trial and Community version, but also has paid for licences as well.

The following example creates an exception attribute that inherits the OnExceptionAspect which is defined in the namespace PostSharp.Aspects. The attribute is called an Aspect, the method OnException is called an advice and it will be called whenever an exception is thrown in the decorated method, in this case CauseException(); So here, I am giving the method CauseException advice on what to do with exceptions.

So go on, download PostSharp and give AOP a try!


using System;
using System.Diagnostics;

using PostSharp.Aspects;

namespace AOPConsole
{
  [Serializable]
  public sealed class ExceptionAttribute : OnExceptionAspect
  {
    public ExceptionAttribute()
    {
    }

    public override void OnException(MethodExecutionArgs args)
    {
      Trace.WriteLine(String.Format("Exception - {0}.{1}: {2} - {3}",args.Method.DeclaringType.Name, args.Method.Name, args.Exception.Message, args.Exception.StackTrace));
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
      System.Diagnostics.Trace.Listeners.Add(new System.Diagnostics.TextWriterTraceListener(Console.Out));

      CauseException();

      Console.ReadKey(true);
    }

    [ExceptionAttribute()]
    private static void CauseException()
    {
      object nullObject = null;

      int intObject = (int)nullObject;
    }
  }
}


 

Help Test Net Framework 4 Beta 2 with Windows Update

I follow Scott Hanselman’s blog and he is a Microsoft fundi, he works for them and he posts regularly about the goings on at Microsoft. One of his posts recently caught my eye and I was glad to see it.

The .Net framework team are allowing people to test the latest Beta release of the .Net 4 client profile via the windows update. The update will be a recommended update when it is officially released about 6-8 weeks after the release of .Net Framework 4.

Below is  comparison of the 4 client profile and 3 client profile.

.NET Framework 3.5 SP1 Client Profile .NET Framework 4 Client Profile
Web Install Local Package & Web Install
Only Windows XP SP2 or SP3 and x86 architecture. All platforms and CPU architectures supported by the full .NET Framework 4 Beta 2 except IA64.
Separate from the framework. Part of the full .NET Framework. The .NET Framework is made up of the Client Profile and Extended components that exist separately in Add or Remove Programs.
Windows Update will upgrade it to the full .NET Framework.

Independent component.

  • Can be serviced separated.

  • Does not need the full framework.

 

if you want to test the Beta, then here is how you go about it:

if you have no dot net 4 bits on your machine, Just make a text file with some meaningful name and make sure the file extension is .reg, and put the following text in it:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4B2WU]"OptIn"=dword:00000001

 

then save and close it, and double click on the file and it will insert the registry key and you will be able to install the client profile from windows update.

You can do it from an administrator command prompt if you want:

reg.exe add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4B2WU" /v OptIn /t REG_DWORD /d 1 /f

 

I have heard that if you have any .net 4 components installed already, that the client profile will not install.

When they release the client profile it will install over the beta install and will upgrade it.

If you experience any problems, just remember it can be uninstalled, but do report any issues you have at The .Net Framework 4 Setup & deployment support forum.

Older Posts »