Two ways to set method interceptors in Autofac
public class TestInterceptor : IInterceptor { public void Intercept(IInvocation invocation) { string c = string .Format( " Calling method {0} with parameters {1}... " , invocation. Method. Name, string .Join( " , " , invocation. Arguments. Select(a => (a ?? "" ). ToString()). ToArray())); invocation. Proceed(); string b = string .Format( " Done: result was {0}. " , invocation. ReturnValue); } }
[Intercept( typeof (TestInterceptor))] public interface IPersonRepository { IEnumerable <Person> GetAll(); Person Get( int id); Person Add(Person item); bool Update(Person item); bool Delete( int id); }
builder. RegisterType<PersonRepository>(). EnableInterfaceInterceptors(). As<IPersonRepository> (); builder. RegisterType <TestInterceptor>();
builder .RegisterType <PersonRepository> () .EnableInterfaceInterceptors() .InterceptedBy( typeof (TestInterceptor)) .As <IPersonRepository>();