Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Unity 2.0 Interception via Method Decoration in verbose code

, 24 Jul 2012
Rate this:
Please Sign up or sign in to vote.
Unity 2.0 Interception via Method Decoration in verbose code
I found a lot of examples on how to implement Unity interception with configuration files, but no concise examples of how to configure it with verbose code. Hopefully someone else will find this useful too.
 
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
using System;

namespace UnityExample
{
    class Program
    {
        //Private Variable
        private static IUnityContainer unityContainer;

        static void Main(string[] args)
        {
            unityContainer = new UnityContainer();

            //This allows Interception for the whole Unity container.
            unityContainer.AddNewExtension<interception>();

            //This sets up an InterfaceInterceptor for IAuthenticationService
            //IAuthenticationService is a class I wrote (see below), simply substitute  
            //whatever class you're interested in intercepting here
            unityContainer.Configure<interception>()
               .SetInterceptorFor<iauthenticationservice>(new InterfaceInterceptor());
        }
    }

    //define what it is you want to do with your custom interceptor
    public class LogExceptionsHandler : ICallHandler
    {
        public int Order { get; set; }
        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {   //Put any logic here you would like to invoke BEFORE the method is invoked.
            Console.WriteLine("Before method Invocation happened");

            //This invokes the next item in the Injection pipeline, or eventually
            //calls your method
            var methodReturn = getNext().Invoke(input, getNext);

            // AFTER the target method execution check to see if it excepted 
            if (methodReturn.Exception != null)
            {
                Console.WriteLine("Add some real logging here");
            }

            //if you would like to re-throw, do it here, otherwise just return 
            //your methodReturn
            else
            {
                Console.WriteLine
                   ("To Rethrow, or not to rethrow, that is the question.");
            }
            return methodReturn;
        }
    }

    //create an attribute so you can apply it to your methods
    public class LogExceptionsAttribute : HandlerAttribute
    {
        public override ICallHandler CreateHandler(Microsoft.Practices.Unity.IUnityContainer container)
        {
            return new LogExceptionsHandler();
        }
    }

    //lastly, apply the decoration to the method you want to intercept
    //Note: This is the same Interface defined above in the .SetInterceptorFor
    public interface IAuthenticationService
    {
        [LogExceptions]
        bool isAuthenticated(string userName, string password);
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

KevonHoughton
Software Developer LandmarkDividend
United States United States
I've been programming since I was in grade school. Started in Apple Basic in the 80's. I finally got my CS degree in 2004, after spending a few years in the military, and have been focusing on .Net and C# ever since. I would not consider myself an expert on anything, but I feel like I have a pretty good grasp on most things. I am always looking for better ways to do things and new things to learn.

Comments and Discussions

 
QuestionWhere is code exmaple that I can download PinmemberPingPong KingKong31-Mar-13 5:28 
QuestionUnity interception example doesn't compile Pinmemberkimberlite24-Jul-12 4:32 
GeneralRe: Unity interception example doesn't compile PinmemberKevonHoughton24-Jul-12 5:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 24 Jul 2012
Article Copyright 2012 by KevonHoughton
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid