0
votes

Good afternoon everyone,

I am trying to use the example "Aspect Oriented Programming Using C# and PostSharp" by Reza Ahmadi http://www.codeproject.com/Articles/337564/Aspect-Oriented-Programming-Using-Csharp-and-PostS and dnrTV http://dnrtv.com/dnrtvplayer/player.aspx?ShowNum=0190 for the exception handling. Everything works great if the "OnExceptionAspect" is in the same project/assembly, however the event does not work if it I move the class to it own dll.

[assembly: ExceptionAspect (AttributePriority = 1)]
[assembly: ExceptionAspect(AttributePriority = 2, AttributeExclude = true, AttributeTargetTypes = "HpsErp.Common.AspectObject.*")]
    namespace AspectObject
         [Serializable]
            public class ExceptionAspect : OnExceptionAspect
            {
                public override void OnException(MethodExecutionArgs args)
                {
                    Trace.TraceError("{0} in {1}.{2}",
                        args.Exception.GetType().Name,
                        args.Method.DeclaringType.FullName,
                        args.Method.Name);

                    if (args.Instance != null)
                    {
                       Trace.TraceInformation("this={0}", args.Instance); 
                    }

                    foreach (ParameterInfo parameter in args.Method.GetParameters())
                    {
                        Trace.TraceInformation("{0}={1}", parameter.Name,
                            args.Arguments[parameter.Position] ?? "null");
                    }
                }

I also created a class in the external dll for "Timing" and it works great if I add a custom attribute to the class.

namespace AspectObject
 [Serializable]
    [MulticastAttributeUsage(MulticastTargets.Method)]
    public class TimingAspect : OnMethodBoundaryAspect
    {
        [NonSerialized]
        Stopwatch _StopWatch;

        public override void OnEntry(MethodExecutionArgs args)
        {
            _StopWatch = Stopwatch.StartNew();

            base.OnEntry(args);
        }

        public override void OnExit(MethodExecutionArgs args)
        {
            Console.WriteLine(string.Format("[{0}] took {1}ms to execute",
                new StackTrace().GetFrame(1).GetMethod().Name,
                _StopWatch.ElapsedMilliseconds));

            base.OnExit(args);
        }


Using AspectObject;

namespace MyApp
{
    public class Car
    {
        [TimingAspect]
        private void Drive()
        {
            //...
        }
    }
}

In the end, I am hoping to have this is multi dlls so that I can reuse it ie: wcf.

Thanks for any and all help...

James

1

1 Answers

1
votes

You can access your aspects if they are stored in a separate DLL.

I always create a DLL class project called Aspects. In the projects I want AOP, I add a reference to that dll class. Then decorate your methods/class/assembly like you normally do.

https://github.com/sharpcrafters/PostSharp-Toolkits <-- good examples http://researchaholic.com/tag/postsharp/ <-- some more examples, just uploaded an example