3

How can I use an interceptor in IInterceptorSelector.SelectInterceptors method which has a constructor parameter. I want to let Autofac resolve my interceptor with it's parameters something like in this Castle framework:

InterceptorReference.ForType<CallLogger>()

I make a research about it, but found nothing.

Here is a sample code that taken from examples:

class Program
{
    static void Main(string[] args)
    {
        var builder = new ContainerBuilder();
        var proxyGenerationOptions = new ProxyGenerationOptions();

        //I want to use this
        //proxyGenerationOptions.Selector = new InterceptorSelector();

        builder.RegisterType<SomeType>()
            .As<ISomeInterface>()
            .EnableInterfaceInterceptors(proxyGenerationOptions)

            .InterceptedBy(typeof(CallLogger));//and remove explicit statement

        builder.Register<TextWriter>(x => Console.Out);

        builder.RegisterType<CallLogger>().AsSelf();

        var container = builder.Build();

        var willBeIntercepted = container.Resolve<ISomeInterface>();
        willBeIntercepted.Work();
    }
}

public class InterceptorSelector : IInterceptorSelector
{
    public IInterceptor[] SelectInterceptors(Type type, MethodInfo method, IInterceptor[] interceptors)
    {
        //don't know how to solve dependency here, because it's registration time
        return new IInterceptor[] { /* new CallLogger(dependency) or InterceptorReference.ForType<CallLogger>()  */};
    }
}

public class CallLogger : IInterceptor
{
    TextWriter _output;

    public CallLogger(TextWriter output)
    {
        _output = output;
    }

    public void Intercept(IInvocation invocation)
    {
        invocation.Proceed();

        _output.WriteLine("Done: result was {0}.", invocation.ReturnValue);
    }
}

public interface ISomeInterface { void Work(); }

public class SomeType : ISomeInterface { public void Work() { } }

I also would like to know, if there is any dynamic interceptor assign mechanism in Autofac. In Castle, there are various ways to modify interception pipeline.

Yusuf Uzun
  • 1,491
  • 1
  • 13
  • 32

1 Answers1

2

Right now this isn't possible in Autofac.Extras.DynamicProxy. You can see in the source that the interceptors are retrieved from a metadata property on the registration (put there by IntereceptedBy) and not using the interceptor selector.

We have had one user note that you can wire up your own interceptors doing something like this:

builder.RegisterType<Implementation>().AsSelf();
builder.Register(c =>
{
  ProxyGenerator proxyGen = new ProxyGenerator(true);
  return proxyGen.CreateInterfaceProxyWithTargetInterface<IInterfaceOfImplementation>(
    c.Resolve<Implementation>(),
    c.Resolve<ExceptionCatcherInterceptor>());
}).As<IInterfaceOfImplementation>();

This is a bit more manual, but it might get you where you're going.

I've added an enhancement issue to look into this.

Travis Illig
  • 23,195
  • 2
  • 62
  • 85
  • I appreciate for your help. I will try this. If I understand correctly, the main idea here putting the logic of selector into the Register part. So I can select which interceptors to add inside register. – Yusuf Uzun Dec 17 '18 at 06:25