I'm trying to setup Autofac for Umbraco and I'm failing hard. It looks like Autofac is ignoring the configuration.
My Repository is always ending up as null. What am I missing?
Here's the Startup config
namespace MyApp.Website.EventHandlers
{
public class ApplicationStartedEventHandler : IApplicationEventHandler
{
public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
}
public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
}
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
RegisterAutoMapper.PopulateAutoMapper();
var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterApiControllers(typeof(UmbracoApplication).Assembly);
builder.RegisterControllers(typeof(InstructorProfileStep2Controller).Assembly);
builder.RegisterControllers(typeof(RegisterController).Assembly);
builder.RegisterControllers(typeof(Umbraco.Forms.Web.Trees.DataSourceTreeController).Assembly);
builder.RegisterType<InstructorRepository>().As<IInstructorRepository>().InstancePerLifetimeScope().PropertiesAutowired();
var container = builder.Build();
var resolver = new AutofacWebApiDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = resolver;
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
}
}
Now the
builder.RegisterControllers(typeof(InstructorProfileStep2Controller).Assembly);
builder.RegisterControllers(typeof(RegisterController).Assembly);
should register my controllers - one is RenderMvcController another one is SurfaceController.
The implementation of Repository is as follows:
namespace MyApp.DataAccess
{
public class InstructorRepository : IInstructorRepository
{
public InstructorProfileDto GetInstructorProfile(int id)
{
return null;
}
}
}
And inside Controller - Repository here is always null:
public class InstructorProfileStep2Controller: RenderMvcController
{
public IInstructorRepository InstructorRepository { get; set; }
public override ActionResult Index(RenderModel model)
{
InstructorProfileStep2Model viewModel = new InstructorProfileStep2Model(model.Content);
InstructorRepository.GetInstructorProfile(1123);
return base.Index(viewModel);
}
}
That works. Although it's not the perfect solution for us. We would prefer to go with Repository being a autoinjected property. Having in mind the above solution works, do you have any ideas why the solution provided by me isnt't working?
Autofac not working
Hi
I'm trying to setup Autofac for Umbraco and I'm failing hard. It looks like Autofac is ignoring the configuration.
My Repository is always ending up as null. What am I missing?
Here's the Startup config
Now the builder.RegisterControllers(typeof(InstructorProfileStep2Controller).Assembly); builder.RegisterControllers(typeof(RegisterController).Assembly); should register my controllers - one is RenderMvcController another one is SurfaceController.
The implementation of Repository is as follows:
And inside Controller - Repository here is always null:
And project structure
Thanks
I think better way is to use constructor initialization like
For apply property auto-wired on controller you should enable it
builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
Hi Yakov
That works. Although it's not the perfect solution for us. We would prefer to go with Repository being a autoinjected property. Having in mind the above solution works, do you have any ideas why the solution provided by me isnt't working?
Did you try this one ?
Thanks!
That worked. The only thing I had to either remove my
or apply PropertiesAutowired() on them.
Maciej
I think you can register all controllers in one row
or define per each controller
is working on a reply...