Using Autofac DynamicProxy2 Interceptors with MVC3–Part 2

Continuing from the previous post, the interceptor is now ready to be registered and consumed.

In the Application_Start() method of my Global.asax.cs, I create an instance of the IoC container and register all the types.

   1:  protected void Application_Start()
   2:  {
   3:      // register all your types into this container
   4:      // you are basically saying that you want this container
   5:      // to take care of instantiating an object
   6:      ContainerBuilder containerBuilder = new ContainerBuilder();
   7:      containerBuilder.RegisterControllers(typeof(MvcApplication).Assembly);
   8:      containerBuilder.RegisterModule(new AutofacWebTypesModule());
   9:      containerBuilder.RegisterSource(new ViewRegistrationSource());
  10:   
  11:      // register the interceptor
  12:      containerBuilder.RegisterType<SomeRandomServiceInterceptor>()
  13:          .SingleInstance();
  14:   
  15:      // this is where you're telling intercept the service proxy using the 
  16:      // interceptor; also a single instance is needed throughout the application;
  17:      containerBuilder.RegisterType<SomeRandomServiceProxy>()
  18:          .As<ISomeRandomServiceProxy>()
  19:          .EnableInterfaceInterceptors()
  20:          .InterceptedBy(typeof(SomeRandomServiceInterceptor))
  21:          .SingleInstance();
  22:   
  23:      IContainer container = containerBuilder.Build();
  24:      DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
  25:   
  26:      AreaRegistration.RegisterAllAreas();
  27:   
  28:      RegisterGlobalFilters(GlobalFilters.Filters);
  29:      RegisterRoutes(RouteTable.Routes);
  30:  }

Line 12 is where I register the interceptor in the container and the statement at line 17 is where I’m  telling – ‘dude, if someone requests the type SomeRandomServiceProxy, make sure you intercept it’s method calls using an instance of  SomeRandomServiceInterceptor’.

All I have to do now is to inject a type of ISomeRandomProxy into my controller – I have chosen to do it through constructor injection (as against setter injection).

   1:  private readonly ISomeRandomServiceProxy _someRandomServiceProxy;
   2:   
   3:  public HomeController(ISomeRandomServiceProxy someRandomServiceProxy)
   4:  {
   5:      _someRandomServiceProxy = someRandomServiceProxy;
   6:  }

That’s it for all the set up work. Now it’s time to run and see the output. If I step through the constructor, I’ll see something’s different about the ISomeRandomServiceProxy instance that is being injected. It’s of type Castle.Proxies.ISomeRandomServiceProxyProxy, but it has a _target field that has a reference to the SomeRandomServiceProxy type. It looks as if the DynamicProxy ‘hacks’ the proxy into something of its own. This ‘hacking’ is what creates the wrapper class (the interceptor) around the proxy class to run our custom business logic.

image

See the below three screen captures to see the flow of the control from the action method, to the interceptor and then to the actual proxy.

image

image

image

When I hover over the instance of the IInvocation type, we can see what’s going on underneath – specifically, the Method , the InvocationTarget and the Proxy properties are of my interest.

image -

After the statement gets executed, I also see the return value of the method that was invoked in the ReturnValue property. If you remember from the previous blog the implementation of the RunsJustFine() returns 1.

I ran all the three action methods and went into my event view to see the entries.

image1

image2

image3

image4

So there they are all your log entries.

This article shows how you could use Autofac DynamicProxy to intercept method calls. The sample code used can be found here.

2 Comments

  • Why is your proxy and interceptor lifetime single instance?
    Don't you want it to be instance per http request??

  • Hi,While I trace SOAP message in Eclipse IDE for looalhcst its working fine but i used another IP address it didnt show any trace(request,response).Give a solution for that.eg:Local Monitoring port:8888 HostName:Localhost port:9999 Type:TCP/IPWhen using this and running my application in same pc(means looalhcst) its getting output correct and showing trace(request,response).But, When i am using the following details TCP/IP monitorLocal Monitoring port:8888 HostName:123.456.7.8 port:9999 Type:TCP/IPI am getting correct output but i didnt get trace (request,response).Here i deploy my service endpoint in another pc having IP:123.456.7.8 .What is problem here?Thanks,sudheer

Comments have been disabled for this content.