Source

main / Source / WebService / Web.Admin / Global.asax.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Activation;
using System.Web.Mvc;
using System.Web.Routing;
using DataUp.Web.Admin.DataLayer;
using DataUp.Web.Common;
using Microsoft.Practices.Unity;

namespace DataUp.Web.Admin
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        private static UnityContainer container;

        /// <summary>
        /// Gets the parent unity container.
        /// </summary>
        public static UnityContainer ParentUnityContainer
        {
            get
            {
                return container;
            }
        }

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional },  // Parameter defaults
                new { controller = "^(?!RepositoryService).*" }
            );

            // This is the service route and it needs to be ignored from default routes
            routes.Add(new ServiceRoute("RepositoryService", new WebServiceHostFactory(), typeof(RepositoryService)));
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            RegisterUnityContainer();
        }

        /// <summary>
        /// Creates an instance of UnityContainer and registers the instances which needs to be injected
        /// to Controllers/Views/Services, etc.
        /// </summary>
        private static void RegisterUnityContainer()
        {

            container = new UnityContainer();

            container.RegisterType<DCXLAdminEntities>(
                   new PerRequestLifetimeManager(), new InjectionConstructor(ConfigReader<string>.GetSetting("DCXLAdminEntities")));

            RegisterServices(container);

            DependencyResolver.SetResolver(new UnityDependencyResolver(container));
        }

        /// <summary>
        /// Registers the required services which is required for all the controllers.
        /// </summary>
        /// <param name="container">Instance of unity container</param>
        private static void RegisterServices(UnityContainer container)
        {
            if (container != null)
            {
                container.RegisterType<IAdminUserRepository, AdminUserRepository>(new PerRequestLifetimeManager());
                container.RegisterType<IRepositoryDetailsDB, RepositoryDetailsDB>(new PerRequestLifetimeManager());
            }
        }

        /// <summary>
        /// End request handler
        /// </summary>
        /// <param name="sender">sender object</param>
        /// <param name="e">event arguments</param>
        private void EndRequestHandler(object sender, EventArgs e)
        {
            // This is a workaround since subscribing to HttpContext.Current.ApplicationInstance.EndRequest 
            // from HttpContext.Current.ApplicationInstance.BeginRequest does not work. 
            IEnumerable<PerRequestLifetimeManager> perRequestManagers =
                container.Registrations
                    .Select(r => r.LifetimeManager)
                    .OfType<PerRequestLifetimeManager>()
                    .ToArray();

            foreach (var manager in perRequestManagers)
            {
                manager.Dispose();
            }
        }
    }
}