Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Julien Kulker 75 posts 427 karma points c-trib
    Feb 28, 2018 @ 09:05
    Julien Kulker
    0

    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

      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
    }
    

    ISearchService.cs

    public interface ISearcherService
    {
        IEnumerable<IPublishedContent> SearchByNodeType(string searchProvider, string section, string nodeType);
    }
    

    So the problem here is that the IsearchService is just not set.

Please Sign in or register to post replies

Write your reply to:

Draft