Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7169269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:57:12+00:00 2026-05-28T14:57:12+00:00

I have a project with several layers – among them the web front end

  • 0

I have a project with several layers – among them the web front end (ASP.NET MVC3) and the service back end (mainly business logic). The project is a few months old, so everything is working as expected. Now I am trying to add a logging aspect to some of the MVC3 controller methods using custom [Log] attributes.

I am using Castle Windsor for dependency injection. To get a logging aspect I leverage Castle DynamicProxy through SNAP. Controllers are being resolved using WindsorControllerFactory from Krzysztof Koźmic’s helpful tutorial – but I modified it to look for the default interface for the controller (see below).

In my service layer:

[Log(LoggingLevel.Info)]
public void Save(MyBusinessDto dto)
{
    // business logic and other checks

    this.repository.Save(mbo);
}

In my web front end’s IWindsorInstaller for controllers:

private static BasedOnDescriptor FindControllers()
{
    return AllTypes
            .FromThisAssembly()
            .BasedOn<IController>()
            .WithService.DefaultInterface();
}

In my (slightly customized) WindsorControllerFactory that looks for the default interface for the controller:

protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
    if (controllerType == null)
    {
        throw new HttpException(404, string.Format(Error404, requestContext.HttpContext.Request.Path));
    }

    string controllerName = controllerType.Name;
    string defaultInterfaceName = 'I' + controllerName;
    Type defaultInterface = controllerType.GetInterface(defaultInterfaceName);

    object controller = this.kernel.Resolve(defaultInterface);

    return (IController)controller;
}

In my controllers:

public class MyBusinessController : MyBusinessControllerBase, IMyBusinessController
{
    [Log(LoggingLevel.Debug)]
    public ActionResult CreateOrUpdate(MyBusinessFormModel fm)
    {
        // Convert form model to data transfer object,
        // perform validation and other checks

        this.service.Save(dto);

        return View(fm);
    }
}

This all works fine in the service project, but in the controllers the methods are not being intercepted.

  • I have confirmed that the WindsorControllerFactory returns proxied controllers.
  • I have confirmed that the controllers have the interceptor registered.
  • I have confirmed that the MasterProxy in SNAP intercepts the controller – but it only intercepts IController.Execute(RequestContext requestContext).

How can I intercept all controller methods that have my [Log] attribute?

Update 1: I have considered using DynamicProxy directly instead of SNAP, but this is secondary to getting it to work for controllers as well.

Update 2+4: It seems that SNAP is missing from github back on github.

Update 3: This is what I see in the Visual Studio debugger when breaking in the WindsorControllerFactory (see above). The inspected controller variable is what is returned to MVC, and it is indeed proxied.

  • controller {Castle.Proxies.IMyBusinessControllerProxy}
    • __interceptors {Castle.DynamicProxy.IInterceptor[1]}
      • [0] {Snap.MasterProxy}
    • __target {My.Business.Web.Controllers.MyBusinessController}
      • service {Castle.Proxies.IMyBusinessServiceProxy}
      • (other contructor injections)
    • MyInjectedProperty {My.Business.Useful.MyOtherType}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:57:13+00:00Added an answer on May 28, 2026 at 2:57 pm

    In IController GetControllerInstance(...), don’t serve interface proxies, serve class proxies with virtual methods.

    The user-implemented methods in the controller returned from IController GetControllerInstance(...) will not be accessed through the proxied IMyBusinessController interface, but cast from IController to to the actual class of the controller; for example MyBusinessController. Use a class proxy instead, to make MVC3’s cast return the proxy. Also, mark methods as virtual, otherwise the intercepting proxy won’t be able to intercept the method calls and check for custom attributes.

    In the controllers, add virtual to your methods with attributes:

    public class MyBusinessController : MyBusinessControllerBase, IMyBusinessController
    {
        [Log(LoggingLevel.Debug)]
        public virtual ActionResult CreateOrUpdate(MyBusinessFormModel fm)
        {
            // Convert form model to data transfer object,
            // perform validation and other checks
    
            this.service.Save(dto);
    
            return View(fm);
        }
    }
    

    Why is only Execute(...) intercepted? The IController interface only contains Execute(...). Execute is called on the returned controller interface proxy, thus it can be intercepted. But once MVC3’s internal ControllerBase.Execute(...) gets the call, it performs the cast to the class it expected from the ControllerFactory.

    The problem is similar to this leaking, in that both bypass the interface proxy. I guess it could be solved in a number of ways; perhaps by creating a custom type converter, creating a class proxy from the interface proxy’s target in the factory, a clever Windsor configurations etcetera.

    Krzysztof Koźmic’s IController installer and WindsorControllerFactory should work out of the box. Interface proxies may be recommended in the bigger picture (and they work well until using interceptors in the controllers) but in this case there might be a reason not to go that far, to avoid further side effects.

    Thanks to Marius for pointing me in the right direction!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have troubles consuming unity container in ASP.NET MVC3 application. I have several project
I have a project divided into several sub-modules (each of them are jar libraries):
In a .net application involving several projects in one solution, I have a project
I have a .NET 3.5 WinForms project that uses several 3rd party controls and
I have a project with several sources directories : src/A /B /C In each,
I have a project with several CSS files, each with many different settings. Every
We have an ADP project created several years ago using Access 2000. This project
I have an Xcode project with several static library dependancies set up in the
I have a project that has several build configurations (FREE version, male-only, female-only, etc.).
When working on a project with several other people it's common to have several

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.