Created
May 27, 2016 21:41
-
-
Save glcheetham/ee2dd9c336bd931d101b6f5919ec7f54 to your computer and use it in GitHub Desktop.
Umbraco IOC Implementation that works properly
This file contains hidden or 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 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