Advertisement
gunnim

Untitled

Jul 24th, 2017
91
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using Hangfire.Storage;
  2. using Microsoft.Practices.Unity;
  3. using Microsoft.Practices.Unity.Mvc;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Http;
  7. using System.Web.Mvc;
  8. using Umbraco.Core;
  9. using Umbraco.Extensions;
  10. using Umbraco.Extensions.App_Start;
  11. using Umbraco.Web;
  12. using Umbraco.Web.HealthCheck;
  13.  
  14. class UnityEvents : IApplicationEventHandler
  15. {
  16.     public void OnApplicationStarted(
  17.         UmbracoApplicationBase httpApplication,
  18.         ApplicationContext applicationContext
  19.     )
  20.     {
  21.         var container = UnityConfig.GetConfiguredContainer();
  22.  
  23.         // If you intend to use DI with filter attributes
  24.         FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>().First());
  25.         FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container));
  26.         // Web API
  27.         GlobalConfiguration.Configuration.DependencyResolver = new Microsoft.Practices.Unity.WebApi.UnityDependencyResolver(container);
  28.         // MVC
  29.         DependencyResolver.SetResolver(new Microsoft.Practices.Unity.Mvc.UnityDependencyResolver(container));
  30.  
  31.         container.RegisterTypes(
  32.             AllClasses.FromAssemblies(typeof(UnityConfig).Assembly),
  33.             WithMappings.FromMatchingInterface,
  34.             WithName.Default
  35.         );
  36.  
  37.         container.RegisterInstance(ApplicationContext.Current);
  38.         container.RegisterInstance(new Configuration());
  39.  
  40.         container.RegisterType<HttpContextBase>(new PerRequestLifetimeManager(), new InjectionFactory(c => new HttpContextWrapper(HttpContext.Current)));
  41.         container.RegisterType<UmbracoContext>(new PerRequestLifetimeManager(), new InjectionFactory(c => UmbracoContext.Current));
  42.         container.RegisterType<UmbracoHelper>(new PerRequestLifetimeManager(), new InjectionConstructor(typeof(UmbracoContext)));
  43.         container.RegisterType<HealthCheckController>(new InjectionConstructor());
  44.  
  45.         container.RegisterType<IStorageConnection>(new InjectionFactory(c => Hangfire.JobStorage.Current.GetConnection()));
  46.     }
  47.  
  48.     public void OnApplicationInitialized(UmbracoApplicationBase httpApplication, ApplicationContext applicationContext)
  49.     {
  50.     }
  51.  
  52.     public void OnApplicationStarting(UmbracoApplicationBase httpApplication, ApplicationContext applicationContext)
  53.     {
  54.     }
  55. }
Advertisement
RAW Paste Data Copied
Advertisement