Skip to content

Instantly share code, notes, and snippets.

@glcheetham
Created May 27, 2016 21:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glcheetham/ee2dd9c336bd931d101b6f5919ec7f54 to your computer and use it in GitHub Desktop.
Save glcheetham/ee2dd9c336bd931d101b6f5919ec7f54 to your computer and use it in GitHub Desktop.
Umbraco IOC Implementation that works properly
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 controllers from the Umbraco assemblies with Autofac
builder.RegisterControllers(typeof(Umbraco.Web.UmbracoApplication).Assembly);
builder.RegisterApiControllers(typeof(Umbraco.Web.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