Intercepting on IDependency

Topics: Customizing Orchard
Oct 8, 2013 at 8:08 PM
I'm trying to have a method in ISomeServices intercepted. I know that Orchard uses Castle.DynamicProxy2 and Autofac, so I'm reusing the same component.

Ideally, I should have an Interceptor Class as followed:
//Using Castle.Core.Interceptor
public class SomeServiceInterceptor : IInterceptor
{        
    public ILogger MyLogger { get; set; }
    public void Intercept(
        Castle.Core.Interceptor.IInvocation invocation)
    {
        MyLogger.Information(
            "Start of Method:"+invocation.Method.Name);
            
        //Proceed Calling the Actual Service Methods
        invocation.Proceed();

        MyLogger.Information(
            "End of Method:" + invocation.Method.Name);            
    }        
}
My Interface has a simple Method as followed:
public interface ISomeService : IDependency
{
    void SomeMethod();
}
Assuming my Controller has ISomeService Injected (Orchard already does this):
public CatalogueController(ISomeService someService)
{
    //Invoking this would always channel the 
    //call to the interceptor class
    someService.SomeMethod();
}
Goal of this ? So that I can put in additional functionality like logging or whatever functionality I desire. Can someone here help me or provide guidance as to how I can do the wiring?
Coordinator
Oct 9, 2013 at 2:19 AM
I'll let others more well-versed in Castle than I am chime in, but I would say that implementing an aspect weaving system over Orchard is a huge undertaking, that it sounds likely to destroy performance beyond all hope of repair, and that it's massively overkill. There's a reason why aspect-oriented programming is pretty much unheard of nowadays, after having been very fashionable in the late 90's. It seems like something more modest and targeted more narrowly would be more successful. You stated the goal of this is to put in whatever functionality you desire. Can you be a little more specific than that? What functionality and where?
This being said, you might want to take a look at the mini-profiler module's code.
Oct 21, 2013 at 7:59 PM
Has anyone use the InterceptedBy method that's part of the Orchard.Environment.AutofacUtil.DynamicProxy2 namespace ?
 public static void InterceptedBy<TService>(this IComponentRegistration cr) {
            var dynamicProxyContext = DynamicProxyContext.From(cr);
            if (dynamicProxyContext == null)
                throw new ApplicationException(string.Format("Component {0} was not registered with EnableDynamicProxy", cr.Activator.LimitType));

            dynamicProxyContext.AddInterceptorService(cr, new TypedService(typeof(TService)));
        }
I try using it, and nothing get's intercepted.
Dec 23, 2013 at 9:14 PM
pls see this project from a smart guy. for sample.
https://github.com/nicholaspei/OrchardNoCMS/

he also did that without cms