In a previous Lab about Custom Action Filters you have been working with filters customization and injection. In this exercise you will learn how to inject filters with Dependency Injection by using Unity Application Block containers. To do that, you will add to the Music Store Solution a custom action filter that will trace site activity.
Task 1: Including the tracking filter in the solution
In this task you will include in the Music Store a custom action filter for event tracing. As filters were treated in a previous Lab “Custom Action Filters”, you will include the filter class from the Assets folder and then create a Filter Provider for Unity:
- Open the begin solution at /Source/Ex03 – Injecting Filters/Begin/MvcMusicStore.sln.
- Create the folder /Filters at project root.
- Add the custom action filter TraceActionFilter.cs to the project in the folder /Filters that you can find it at/Sources/Assets/TraceActionFilter.cs
C# - TraceActionFilter.cs
Copy Code
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcMusicStore.Filters { public class TraceActionFilter : IActionFilter { public void OnActionExecuted(ActionExecutedContext filterContext) { filterContext.HttpContext.Trace.Write("OnActionExecuted"); filterContext.HttpContext.Trace.Write("Action " + filterContext.ActionDescriptor.ActionName); filterContext.HttpContext.Trace.Write("Controller " + filterContext.ActionDescriptor.ControllerDescriptor.ControllerName); } public void OnActionExecuting(ActionExecutingContext filterContext) { filterContext.HttpContext.Trace.Write("OnActionExecuting"); filterContext.HttpContext.Trace.Write("Action " + filterContext.ActionDescriptor.ActionName); filterContext.HttpContext.Trace.Write("Controller " + filterContext.ActionDescriptor.ControllerDescriptor.ControllerName); } } }
Note:Note: This custom action filter performs ASP.NET tracing.
You can check “Global and Dynamic Action Filters” Lab for more reference. - Add the empty class FilterProvider.cs to the project in the folder /Filters.
- Include System.Web.Mvc and Microsoft.Practices.Unity namespaces in FilterProvider.cs
(Code Snippet –Ex03 Injecting Action Filters – FilterProvider namespace – Csharp)
C#
Copy Code
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Microsoft.Practices.Unity; namespace MvcMusicStore.Filters { public class FilterProvider { } } - Make the class inherit from IFilterProvider Interface.
C#
Copy Code
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Microsoft.Practices.Unity; namespace MvcMusicStore.Filters { public class FilterProvider : IFilterProvider { } } - Add a IUnityContainer property in FilterProvider class, and then create a class constructor to set the container:
(Code Snippet –Ex03 Injecting Action Filters – IUnityContainer – Csharp)
C#
Copy Code
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Microsoft.Practices.Unity; namespace MvcMusicStore.Filters { public class FilterProvider : IFilterProvider { IUnityContainer container; public FilterProvider(IUnityContainer container) { this.container = container; } } }
Note:The filter provider class constructor is not creating a new object inside. The container is passed as a parameter, and the dependency is solved by Unity. - Implement in FilterProvider class the method GetFilters from IFilterProvider interface:
(Code Snippet –Ex03 Injecting Action Filters – GetFilters – Csharp)
C#
Copy Code
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Microsoft.Practices.Unity; namespace MvcMusicStore.Filters { public class FilterProvider : IFilterProvider { IUnityContainer container; public FilterProvider(IUnityContainer container) { this.container = container; } public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor) { foreach (IActionFilter actionFilter in container.ResolveAll<IActionFilter>()) yield return new Filter(actionFilter, FilterScope.First, null); } } }
Task 2: Registering and enabling the filter
- In this task you will enable site tracking and then you will register the filter in Global.asax.cs Application_Start method to start tracing:
- Open Web.Config at project root and enable trace tracking at System.Web group:
XML
Copy Code
<system.web> <trace enabled="true"/> <compilation debug="true" targetFramework="4.0">
- Open Global.asax.cs at project root and add a reference to the Filters namespace:
C#
Copy Code
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; using Microsoft.Practices.Unity; using MvcMusicStore.Services; using MvcMusicStore.Factories; using MvcMusicStore.Controllers; using MvcMusicStore.Filters; namespace MvcMusicStore
- Select Application_Start method and register the filter in the Unity Container. You will have to register the filter provider and the action filter as well:
(Code Snippet – Ex03 Injecting Action Filters – Global asax unity registration - CSharp)
C#
Copy Code
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); var container = new UnityContainer(); container.RegisterType<IStoreService, StoreService>(); container.RegisterType<IController, StoreController>("Store"); var factory = new UnityControllerFactory(container); ControllerBuilder.Current.SetControllerFactory(factory); IDependencyResolver resolver = DependencyResolver.Current; container.RegisterInstance<IMessageService>(new MessageService { Message = "You are welcome to our Web Camps Training Kit!", ImageUrl = "/Content/Images/logo-webcamps.png" }); container.RegisterType<IViewPageActivator, CustomViewPageActivator>(new InjectionConstructor(container)); container.RegisterInstance<IFilterProvider>("FilterProvider", new FilterProvider(container)); container.RegisterInstance<IActionFilter>("LogActionFilter", new TraceActionFilter()); IDependencyResolver newResolver = new UnityDependencyResolver(container, resolver); DependencyResolver.SetResolver(newResolver); }
Task 3: Running the application
In this task you will run the application and test that the custom action filter is tracing the activity:
- Press F5 to run the application
- Browse to /Store and choose ‘Rock’ genre. You can browse to more genres if you want to.

Figure 1
Music Store - Browse to /Trace.axd to see Application trace, and then choose Store ‘View Details’ link at the right column:

Figure 2
Application Trace Log
Figure 3
Application Trace – Request Details