Skip to content

Instantly share code, notes, and snippets.

@glcheetham
Last active May 27, 2016 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glcheetham/87c9385ef8f1ab2b0a54dc71b7cd35f1 to your computer and use it in GitHub Desktop.
Save glcheetham/87c9385ef8f1ab2b0a54dc71b7cd35f1 to your computer and use it in GitHub Desktop.
UmbracoApplication with components registered in the DI container
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