0

I have a .NET3.0 program I am moving to .NET6. We use Castle.DynamicProxy to intercept calls to certain methods and then log the parameter those methods was called with. In .NET3 to add this to the class MyController we use

IWindsorContainer container;
_container = container;
container.Register(Component.For<MyController>().ImplementedBy<MyController>().LifeStyle.Transient
            .Interceptors(InterceptorReference.ForType<LogInterceptor>()).Anywhere);

The syntax is different in .NET6 and I have tried with out luck

builder.Services.AddInterceptedSingleton<IMyController, MyController, LogInterceptor>();

I have also tried

builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Host.ConfigureContainer<ContainerBuilder>(builder =>
{
    builder.RegisterType<MyController>().As<IMyController>().EnableClassInterceptors();
});

The basis of LogInterceptor.cs

    public class LogInterceptor: IInterceptor
    {

        public void Intercept(IInvocation invocation)
        {
        }

        LogAttribute logAttribute = GetLogAttribute(invocation);

        if logAttribute == null)
        {
            invocation.Proceed();
            return;
        }

        switch (logAttribute.logType)
        {
         .... //Logic for the switch
    }
  • Your .NET Core 3 example is Castle Windsor and your .NET 6 example is Autofac, two completely different containers that both use Castle DynamicProxy for interceptors. – Jonathon Rossi Jan 18 '23 at 13:08

0 Answers0