1

So I talked briefly here about how I have a global filter that runs GlobalFilters.Filters.Add(new MyFilter) and obtains a subdomain manager which will conduct some database look up based on subdomain information.

My goal was to have the filtercontext.result set to a new ResultRedirect object if the validations I am conducting fails. However, if it is successful, this Subdomain manager object will persist in the Structuremap Container for the remainder of the request.

Originally I had believed that the controller object was not instantiated when the global filters ran so I had expected to populate a subdomain object property on the controller based on the results of this subdomain manager.

However, I have two delemma's:

  1. I believe the controller has been instantiated when the global filter runes, so even if the global controller that runs the subdomain manager and validates the request is validated, I can't populate the subdomain object on the on my controller from the subdomain manager object that is in the container

  2. Secondly, and more largely, I can't seem to find a valid way to inject dependencies into my Global filter (SomeValidationAttribute : ActionAttribute) using structuremap.

Any suggestions

Community
  • 1
  • 1
pghtech
  • 3,642
  • 11
  • 48
  • 73

2 Answers2

0

1) Stick with a simple dependecy in the constructur of the controller and maybe inject it in the BeginExecute. But I do think you will have to describe what you really is trying to archive, and not let us guess along with your solution.

2) Register your GlobalFilterProvider

public class StructureMapGlobalFilterProvider : IFilterProvider
{
    public StructureMapGlobalFilterProvider(IContainer container)
    {
        _container = container;
    }

    private IContainer _container;

    public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor 

actionDescriptor)
    {
        return new List<Filter>();
    }
}

Source: http://thecodinghumanist.com/blog/archives/2011/2/3/structuremap-global-action-filters-and-dependency-injection-in-asp-net-mvc-3

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Jacee
  • 186
  • 2
  • 7
0

GetFilters of FilterAttributeFilterProvider returns zero item for global filters. In this special case, define your global filter using the StructureMap's container:

GlobalFilters.Filters.Add(container.GetInstance<LogAttribute>());

And then you need to inject StructureMap's IContainer to your filter as well:

public class LogAttribute : ActionFilterAttribute
{
    private readonly IContainer _container; 
    public LogAttribute(IContainer container)
    {
        _container = container;
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        _container.GetInstance<ILogActionService>().Log("......data......");
        base.OnActionExecuted(filterContext);
    }
}

Without calling _container.GetInstance, all of the dependencies of a global filter, will be instantiated only one time, at the beginning of the program.

VahidN
  • 18,457
  • 8
  • 73
  • 117