Last active
May 27, 2016 21:35
-
-
Save glcheetham/87c9385ef8f1ab2b0a54dc71b7cd35f1 to your computer and use it in GitHub Desktop.
UmbracoApplication with components registered in the DI container
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Autofac; | |
using Autofac.Integration.Mvc; | |
using Autofac.Integration.WebApi; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Optimization; | |
using MyApp.Services; | |
using MyApp.Services.Interfaces; | |
using Umbraco.Core; | |
using Umbraco.Core.Persistence.UnitOfWork; | |
using Umbraco.Core.Services; | |
namespace UkReview | |
{ | |
public class UmbracoApplication : IApplicationEventHandler | |
{ | |
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) | |
{ | |
BundleConfig.RegisterBundles(BundleTable.Bundles); | |
var builder = new ContainerBuilder(); | |
// Register our controllers from this assembly with Autofac | |
builder.RegisterControllers(typeof(UmbracoApplication).Assembly); | |
// Register the types we need to resolve with Autofac | |
builder.RegisterInstance(applicationContext.Services.ContentService).As<IContentService>(); | |
builder.RegisterType<MyContentService>().As<IMyContentService>(); | |
// Set up MVC to use Autofac as a dependency resolver | |
var container = builder.Build(); | |
System.Web.Mvc.DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); | |
} | |
public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) | |
{ | |
} | |
public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment