I tried to make use of the dependency injection. After many tries i still don't get it to work. So hopefull you guys can help me out here:
let me share some code:
(Startup.cs - IApplicationEventHandler
public class StartUp : IApplicationEventHandler
{
public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
}
public void OnApplicationStarted(UmbracoApplication httpApplication, Umbraco.Core.ApplicationContext applicationContext)
{
var builder = new ContainerBuilder();
//register all controllers found in this assembly
builder.RegisterControllers(typeof(NewsController).Assembly);
builder.RegisterApiControllers(typeof(UmbracoApplication).Assembly);
builder.RegisterApiControllers(typeof(NewsController).Assembly);
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterType<SearcherService>().As<ISearcherService>();
//add custom class to the container as Transient instance
builder.Register(c => new SearcherService()).As<ISearcherService>().SingleInstance();
var container = builder.Build();
var resolver = new AutofacWebApiDependencyResolver(container);
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
}
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
}
public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
}
}
Surface Controller:
namespace *.Feature.News.Controllers
{
public class NewsController : Umbraco.Web.Mvc.SurfaceController
{
public NewsController()
{
}
public NewsController(ISearcherService searcherService)
{
this.SearcherService = searcherService;
}
public ISearcherService SearcherService { get; }
public ActionResult NewsOverview(RenderModel model)
{
var newsOverviewModel = new Models.NewsOverview(model.Content);
var newsItems = this.SearcherService.SearchByNodeType("NewsSearcher", "content", "newsDetail");
newsOverviewModel.NewsItems = newsItems.Cast<NewsDetail>().ToList();
return this.PartialView("~/Views/NewsItems.cshtml", newsOverviewModel);
}
}
}
SearchService.cs
public class SearcherService : ISearcherService
{
code
}
Interface not set when using Dependency Injection(AutoFac).
Hi there,
Using this tutorial: https://our.umbraco.org/documentation/reference/using-ioc
I tried to make use of the dependency injection. After many tries i still don't get it to work. So hopefull you guys can help me out here:
let me share some code:
(Startup.cs - IApplicationEventHandler
Surface Controller:
SearchService.cs
ISearchService.cs
So the problem here is that the IsearchService is just not set.
is working on a reply...