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 6831349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:43:38+00:00 2026-05-26T22:43:38+00:00

At the moment, I have a custom ControllerFactory into which I inject my Unity

  • 0

At the moment, I have a custom ControllerFactory into which I inject my Unity container:

in global.asax Application_Start():

var container = InitContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));

var factory = new UnityControllerFactory(container);
ControllerBuilder.Current.SetControllerFactory(factory);

In the controller factory I set my controllers to use a custom ActionInvoker like so:

protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
    var controller = base.GetControllerInstance(requestContext, controllerType) as Controller;

    if (controller != null)
        controller.ActionInvoker = new UnityActionInvoker(_container);

    return controller;
}

Finally in my custom ActionInvoker, I attempt to buildup actions being invoked using the ActionInvokers container:

protected override ActionExecutedContext InvokeActionMethodWithFilters(
        ControllerContext controllerContext,
        IList<IActionFilter> filters,
        ActionDescriptor actionDescriptor,
        IDictionary<string, object> parameters)
{
    var builtUpFilters = new List<IActionFilter>();

    foreach (IActionFilter actionFilter in filters)
    {
        builtUpFilters.Add(_container.BuildUp<IActionFilter>(actionFilter));
    }

    return base.InvokeActionMethodWithFilters(controllerContext, builtUpFilters, actionDescriptor, parameters);
}

Here is an example of one of the ActionFilters that is being built up:

public class PopulatRolesAttribute : ActionFilterAttribute, IActionFilter
{
    private const string RolesKey = "roles";

    [Dependency]
    public Func<IMetadataService> Service { get; set; }

    public PopulatRolesAttribute()
    {
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.Controller.ViewData[RolesKey] == null)
        {
            filterContext.Controller.ViewData[RolesKey] = Service().GetRoles();
        }
    }
}

The problem is that the public property on my custom ActionFilterAttribute is never injected with anything, it remains null on execution! I cannot see why my filter is not being correctly builtup by the container. The type being injected is registered properly, like so:

container.RegisterInstance(new ChannelFactory<IMetadataService>(
    new BasicHttpBinding(),
    new EndpointAddress("http://example.com/ABSApplication/MetadataService.svc")));

container.RegisterInstance<Func<IMetadataService>>(
    () => container.Resolve<ChannelFactory<IMetadataService>>().CreateChannel());

And is also being injected elsewhere in the application (Although not via .Buildup). This is pretty much the same process followed by this blog post. What piece of the puzzle am I missing?

  • 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-26T22:43:38+00:00Added an answer on May 26, 2026 at 10:43 pm

    I would do this slightly differently. I would:

    1. install the unity.mvc3 nuget package

    2. call the bootstrapped.initialise() as mentioned in the txt doc the package adds to the project

    3. define your IMetadataService mapping in the initialize to your Concrete type

    4. add IMetadataService to your constructor

    The difference between your implementation and the article you references is you use Func, which Im not sure if that adds a different problem to the mix here. I have to imagine it does as the above method (without Func) works fine for me.

    Edit:
    Brad Wilson’s code worked just fine for me here:
    http://bradwilson.typepad.com/blog/2010/07/service-location-pt4-filters.html

    Applicable parts (please see the link above)

    Global.asax.cs

    
    protected void Application_Start() {
        // ...
    
        var oldProvider = FilterProviders.Providers.Single(
            f => f is FilterAttributeFilterProvider
        );
        FilterProviders.Providers.Remove(oldProvider);
    
        var container = new UnityContainer();
        var provider = new UnityFilterAttributeFilterProvider(container);
        FilterProviders.Providers.Add(provider);
    
        // ...
    }
    
    

    The filter itself:

    
    using System;
    using System.Web.Mvc;
    using Microsoft.Practices.Unity;
    
    public class InjectedFilterAttribute : ActionFilterAttribute {
    
        [Dependency]
        public IMathService MathService { get; set; }
    
        public override void OnResultExecuted(ResultExecutedContext filterContext) {
            filterContext.HttpContext.Response.Write(
                String.Format("

    The filter says 2 + 3 is {0}.

    ", MathService.Add(2, 3)) ); } }

    and UnityFilterAttributeFilterProvider.cs

    
    using System.Collections.Generic;
    using System.Web.Mvc;
    using Microsoft.Practices.Unity;
    
    public class UnityFilterAttributeFilterProvider : FilterAttributeFilterProvider {
        private IUnityContainer _container;
    
        public UnityFilterAttributeFilterProvider(IUnityContainer container) {
            _container = container;
        }
    
        protected override IEnumerable GetControllerAttributes(
                    ControllerContext controllerContext,
                    ActionDescriptor actionDescriptor) {
    
            var attributes = base.GetControllerAttributes(controllerContext,
                                                          actionDescriptor);
            foreach (var attribute in attributes) {
                _container.BuildUp(attribute.GetType(), attribute);
            }
    
            return attributes;
        }
    
        protected override IEnumerable GetActionAttributes(
                    ControllerContext controllerContext,
                    ActionDescriptor actionDescriptor) {
    
            var attributes = base.GetActionAttributes(controllerContext,
                                                      actionDescriptor);
            foreach (var attribute in attributes) {
                _container.BuildUp(attribute.GetType(), attribute);
            }
    
            return attributes;
        }
    }
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

At the moment I have setup a custom ok cancel dialog with a drop
At the moment I have a jQuery do a POST to a Controller which
At the moment I have the following code which works fine. label = new
I have a custom UITableViewCell , which have a button on it, IB linked
I have created a custom column header renderer for my AdvancedDataGrid which has a
I've made a custom control, it's a FlowLayoutPanel, into which I put a bunch
At the moment I have a custom ActiveX plugin that drops down the usual
At the moment I have a widget which, when the user clicks on it,
I have a custom search panel, which is a part of main layout. Most
At the moment I have an object of type A which is being viewed

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.