0

I am working on an implementation of log interceptor using Castle Dynamic Proxy and StructureMap, so in my dependency registry I tell to StructureMap to decorate all instance of TrafficSourceRepository with a LoggingInterceptor.

var proxyGenerator = new ProxyGenerator();
For<ITrafficSourceRepository>(Lifecycles.Singleton)
  .DecorateAllWith(instance => proxyGenerator
  .CreateInterfaceProxyWithTargetInterface(instance,
    new LoggingInterceptor(LogManager.GetLogger("AgencyPlayground"))))
.Use<TrafficSourceRepository>();

enter image description here

All seems to be fine, it works, but the TrafficSourceRepository will be instanciated as a Singleton, and I don't want to, so I change the lifetime of the resolved TrafficSourceRepositories as Transient:

var proxyGenerator = new ProxyGenerator();
    For<ITrafficSourceRepository>(Lifecycles.Transient)
      .DecorateAllWith(instance => proxyGenerator
      .CreateInterfaceProxyWithTargetInterface(instance,
        new LoggingInterceptor(LogManager.GetLogger("AgencyPlayground"))))
    .Use<TrafficSourceRepository>();

and it doesn't work anymore... It is a bug or I am doing something wrong?

ale
  • 10,012
  • 5
  • 40
  • 49
  • By doesn't work you mean LoggingInterceptor is not called? – Evk Mar 06 '17 at 10:38
  • yes, if I change the lifetime to transient, the interceptor doesn't intercept – ale Mar 06 '17 at 10:44
  • Using exact code you posted I cannot reproduce it: with lifecycle transient interceptor is still called. So maybe you should provide minimal working example to reproduce that. – Evk Mar 06 '17 at 10:54
  • @Evk could you provide your implementation please? – ale Mar 06 '17 at 11:27

1 Answers1

2

This is not an answer but I cannot fit it into comment. Here is a minimal example showing that it works fine with Lifecycles.Transient:

class Program {
    public static void Main() {
        var proxyGenerator = new ProxyGenerator();
        var container = new Container(config => {
            config.For<ITrafficSourceRepository>(Lifecycles.Transient)
                .DecorateAllWith(instance => proxyGenerator
                    .CreateInterfaceProxyWithTargetInterface(instance,
                        new LoggingInterceptor()))
                .Use<TrafficSourceRepository>();
        });
        var ts = container.GetInstance<ITrafficSourceRepository>();
        ts.Call();
        Console.ReadKey();
    }        
}

public interface ITrafficSourceRepository {
    void Call();
}

public class TrafficSourceRepository : ITrafficSourceRepository {
    public void Call() {
        Console.WriteLine("Called");
        throw new Exception("Ex");
    }
}

public class LoggingInterceptor : IInterceptor {
    public void Intercept(IInvocation invocation) {
        try {
            invocation.Proceed();
        }
        catch (Exception ex) {
            Console.WriteLine("Intercepted: " + ex.Message);
        }
    }
}

Output:

Called
Intercepted: Ex
Evk
  • 98,527
  • 8
  • 141
  • 191
  • I was using an old version of StructureMap and StructureMap for MVC5, I updated the packages to the latest version and now it works... – ale Mar 06 '17 at 13:57