Skip to content

Instantly share code, notes, and snippets.

@MichaelaIvanova
Created November 2, 2017 12:24
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 MichaelaIvanova/3c01fa642752e46eafd26987dd795c09 to your computer and use it in GitHub Desktop.
Save MichaelaIvanova/3c01fa642752e46eafd26987dd795c09 to your computer and use it in GitHub Desktop.
using Autofac;
using Autofac.Integration.Mvc;
using Autofac.Integration.WebApi;
using System.Reflection;
using System.Web.Http;
using System.Web.Mvc;
using Tungsten_Network.BusinessLogic.Services.Contracts;
using Umbraco.Core.Services;
using Umbraco.Web;
using System;
using Umbraco.Core;
namespace Tungsten_Network.App_Start
{
public static class AutofacConfig
{
public static void RegisterDependencies(ApplicationContext applicationContext)
{
var builder = new ContainerBuilder();
// Register Umbraco Context, MVC Controllers and API Controllers.
builder.Register(c => UmbracoContext.Current).InstancePerRequest();
builder.Register(x => new UmbracoHelper(UmbracoContext.Current)).InstancePerRequest();
builder.RegisterInstance(new UmbracoHelper(UmbracoContext.Current).ContentQuery).As<ITypedPublishedContentQuery>();
builder.RegisterApiControllers(typeof(UmbracoApplication).Assembly);
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
// private Mock<IContentService> contentService;
//private Mock<ILocalizationService> localizationService;
//private Mock<IDataTypeService> dataTypeService;
//private Mock<IFileService> fileService;
//private Mock<IMediaService> mediaService;
//private Mock<IMemberService> memberService;
//private Mock<IMemberTypeService> memberTypeService;
builder.RegisterInstance(applicationContext.Services.ContentService).As<IContentService>();
builder.RegisterInstance(applicationContext.Services.LocalizationService).As<ILocalizationService>();
builder.RegisterInstance(applicationContext.Services.DataTypeService).As<IDataTypeService>();
builder.RegisterInstance(applicationContext.Services.FileService).As<IFileService>();
builder.RegisterInstance(applicationContext.Services.MediaService).As<IMediaService>();
builder.RegisterInstance(applicationContext.Services.MemberService).As<IMemberService>();
builder.RegisterInstance(applicationContext.Services.MemberTypeService).As<IMemberTypeService> ();
// Register the types we need to resolve with Autofac
//var umbracoServicesAssembly = Assembly.GetAssembly(typeof(IContentService));
//builder.RegisterAssemblyTypes(umbracoServicesAssembly).AsImplementedInterfaces().InstancePerLifetimeScope();
var servicesAssembly = Assembly.GetAssembly(typeof(IResourceLibraryService));
builder.RegisterAssemblyTypes(servicesAssembly).AsImplementedInterfaces();
// Set up MVC to use Autofac as a dependency resolver
var container = builder.Build();
var resolver = new AutofacWebApiDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = resolver;
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment