Created
November 2, 2017 12:24
-
-
Save MichaelaIvanova/3c01fa642752e46eafd26987dd795c09 to your computer and use it in GitHub Desktop.
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.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