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

  • SEARCH
  • Home
  • 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 7791737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:58:55+00:00 2026-06-01T21:58:55+00:00

I have an action filter in an ASP.NET MVC 3 app that needs some

  • 0

I have an action filter in an ASP.NET MVC 3 app that needs some dependencies injected into it. I am using Autofac.Mvc3 as the dependency injector.

According to the autofac wiki I just have to register the types that I want to inject, call RegisterFilterProvider, and put a public property on my action filter, and then autofac will fill the property with the right object during filter instantiation.

Here is a part of my action filter:

Public Class LogActionAttribute
    Inherits ActionFilterAttribute

    Property tracer As TraceSource

    Public Overrides Sub OnActionExecuting(filterContext As System.Web.Mvc.ActionExecutingContext)
        ...
        tracer.TraceData(...)
        ...
    End Sub
End Class

Here is a part of my global.asax:

Public Class MvcApplication
    Inherits System.Web.HttpApplication

    Shared Sub RegisterGlobalFilters(ByVal filters As GlobalFilterCollection)
        filters.Add(New MyHandleErrorAttribute)
        filters.Add(New LogActionAttribute)
    End Sub

    Sub Application_Start()
        InitSettingRepoEtc()
        ...
    End Sub

    Protected Shared Sub InitSettingRepoEtc()
        ...
        Dim builder = New ContainerBuilder
        builder.RegisterControllers(Reflection.Assembly.GetExecutingAssembly)
        ...
        builder.Register(Of TraceSource)(
            Function(x) New TraceSource("requests", SourceLevels.All)).InstancePerHttpRequest()
        ...
        builder.RegisterFilterProvider()
        Dim container = builder.Build
        DependencyResolver.SetResolver(New AutofacDependencyResolver(container))
        ...
    End Sub
End Class

I have put breakpoint right after SetResolver and in the immediate window tried:

DependencyResolver.Current.GetService(Of TraceSource)

And I successfully got a TraceSource object from autofac, so the registration seems to be OK.

But during OnActionExecuting my tracer property is empty.

What did I miss?

  • 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-06-01T21:58:56+00:00Added an answer on June 1, 2026 at 9:58 pm

    IIRC the provider doesn’t work with ‘global’ filters.

    Remove this function:

    Shared Sub RegisterGlobalFilters(ByVal filters As GlobalFilterCollection)
        filters.Add(New MyHandleErrorAttribute)
        filters.Add(New LogActionAttribute)
    End Sub
    

    And instead, register the global filters with Autofac directly:

     builder.Register(Of MyHandleErrorAttribute)
         .As(Of IActionFilter)
         .PropertiesAutowired()
         .SingleInstance();
    
     builder.Register(Of LogActionAttribute)
         .As(Of IActionFilter)
         .PropertiesAutowired()
         .SingleInstance();
    

    Autofac will create the filters and include them appropriately. The advantage of this approach is that you can refactor so the filters are not attribute-derived, and then use constructor rather than property injection.

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

Sidebar

Related Questions

In my ASP.NET MVC app, I have a view that's calling Html.Action(MyAction, this.Model) -
I have an asp.net MVC app that i am working on and I wrote
I have an ASP.net MVC controller called Designs that has an action with the
Here's the setup. Say I have some action filter that needs an instance of
I have created a new ActionFilter for an ASP.NET MVC application that I'm creating.
Here's a little background on my solution: ASP.Net MVC app Using Linq-to-SQL with table-per-hierarchy
On ASP.NET MVC 2 I have an ActionFilterAttribute called [Transaction] that starts an NHibernate
An ASP.Net MVC 3 application I've just started writing needs to have authentication for
I have an asp.net mvc app which has membership implemented. So a user has
My ASP.NET MVC intranet app has a data repository that uses current user's Windows

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.