Action Filter Setter Injection with StructureMapFilterProvider
<p><strong>Update 2</strong></p>

<p><strike>
Thanks again for getting even more detailed.  I haven't been able to get your GlobalFilterProvider to work correctly, but it seems that no matter which approach I take here, I won't be able to take advantage of the lifetime scoping that becomes available with a nested container.  Currently, using the StructureMap.MVC5 NuGet package a nested container is created on Application_BeginRequest and disposed of on Application_EndRequest, which allows me to scope objects to UniquePerRequest and have them tore down when the nested container is disposed. It seems like no matter what a filter needs to get added on Application_Start and any dependencies still need to be added using the container, which on Application_Start is only the parent at that time. I don't even think using a <a href="http://blog.ploeh.dk/2014/08/24/decoraptor/" rel="nofollow noreferrer">Decoraptor</a> will help me in this scenario.</p>

<p>Is there any way to achieve this?
</strike></p>

<p>This problem has been solved - see my <a href="https://stackoverflow.com/questions/38483824/passive-attributes-and-nested-containers">other SO question</a>.</p>

<p><strong>Update 1 - Comments on NightOwl888's Answer</strong></p>

<p>I understand what is happening in the "Filling Setter's of an Object" piece you linked to, as that code is what the <code>StructureMapFilterProvider</code> is doing to build up the dependencies of the action filter.  What I was trying to understand is the <code>IContainer</code> reference in the <code>StructureMapFilterProvider</code>.  Is the current <code>DependencyResolver</code> set in MVC what is doing the resolving of that <code>IContainer</code> reference?  I just want to understand where that is coming from.  Also, adding .Singleton() did actually fix the problem, so thanks for pointing that out.  </p>

<p>I'm still trying to understand that <code>GlobalFilterProvider</code> you linked to, and I do understand the benefits of using constructor injection instead.  I'm just trying to wrap my head around it all. </p>

<hr>

<p><strong>Original Post</strong></p>

<p>I am using StructureMap for my DI needs, and have included it in my ASP.NET MVC 5 project via the <code>StructureMap.MVC5</code> NuGet package.  I have the typical logging action filter where I want to inject a dependency (ILogger) into the action filter I have created.  While I have found and understand the <a href="http://blog.ploeh.dk/2014/06/13/passive-attributes/" rel="nofollow noreferrer">passive attributes</a> method for bypassing setter injection, I also became intrigued with the method of using a <a href="http://ericsowell.com/blog/2011/1/27/structuremap-action-filters-and-dependency-injection-in-asp-net-mvc-3" rel="nofollow noreferrer"><code>StructureMapFilterProvider</code></a> like so...</p>

<pre><code>public class StructureMapFilterProvider : FilterAttributeFilterProvider
{
    private readonly IContainer _container;

    public StructureMapFilterProvider(IContainer container)
    {
        _container = container;
    }

    public override IEnumerable&lt;Filter&gt; GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
    {
        var filters = base.GetFilters(controllerContext, actionDescriptor);

        foreach (var filter in filters)
        {
            _container.BuildUp(filter.Instance);
        }

        return filters;
    }
}
</code></pre>

<p>However, the <code>IContainer</code> dependency of the <code>StructureMapFilterProvider</code> seems to be a little magical.  My first question would be how does it even get resolved?  What is doing the resolving if the container itself is what needs to be resolved.  Second, it seems to have a problem with the nested containers the <code>StructureMap.MVC5</code> setup creates and disposes of per request at <code>Application_BeginRequest</code> and <code>Application_EndRequest</code>.  </p>

<p>On <code>Application_Start</code>, the filter provider works fine, presumably because it's not a nested container yet, and the <code>IContainer</code> instance resolved looks like this:</p>

<p><a href="https://i.stack.imgur.com/7N6Uw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7N6Uw.png" alt="enter image description here"></a></p>

<p>Yet if I run the same action again now that the application is started, the filter provider bombs and the IContainer instance now looks like this:</p>

<p><a href="https://i.stack.imgur.com/0rPIq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0rPIq.png" alt="enter image description here"></a></p>

<p>What am I doing wrong?  Is there a fix for this?</p>

<hr>

<p><em>The rest of my code is below if that helps.</em></p>

<p>Logger implementation:</p>

<pre><code>public class Logger : ILogger
{
    public void Log(string message)
    {
        Debug.WriteLine(message);
    }
}

public interface ILogger
{
    void Log(string message);
}
</code></pre>

<p>Logger action filter:</p>

<pre><code>[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
public class LoggerAttribute : ActionFilterAttribute
{
    public ILogger Logger { get; set; }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        Logger.Log("Testing: Executing an action");
    }
}
</code></pre>

<p>Additional DI setup added to DependencyResolution/IoC.cs class provided by the StructureMap.MVC5 package:</p>

<pre><code>public static class IoC
{
    public static IContainer Initialize()
    {
        var container = new Container(c =&gt;
        {
            c.AddRegistry&lt;DefaultRegistry&gt;();
            c.For&lt;IFilterProvider&gt;().Use&lt;StructureMapFilterProvider&gt;();
            c.For&lt;ILogger&gt;().Use&lt;Logger&gt;();
            c.Policies.SetAllProperties(p =&gt;
            {
                p.OfType&lt;ILogger&gt;();
            });
        });

        return container;
    }
}
</code></pre>

<p>Controller:</p>

<pre><code>public class HomeController : Controller
{
    [Logger]
    public ActionResult Index()
    {
        return Content("worked");
    }
}
</code></pre>