Copied to clipboard

Flag this post as spam?

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


  • stevo 63 posts 106 karma points
    Jul 13, 2015 @ 07:33
    stevo
    0

    dependency Injection doesn't work in TreeController for rendering the second level of nodes

    Hi,

    I'm trying to develop a extension for Umbraco. I inject via dependency injection (Autofac) a service in the tree controller.

    It works fine until I go to the second level. Umbraco will create an instance from RaceTreeController by using the default constructor…

    Error message:

    Received an error from the server
    Failed to retrieve data for child nodes undefined
    
    Type 'Indegrade.Autoface.BackOffice.Controllers.RaceTreeController' does not have a default constructor
    
    EXCEPTION DETAILS:
    
    System.ArgumentException: Type 'Indegrade.Autoface.BackOffice.Controllers.RaceTreeController' does not have a default constructor
    STACKTRACE:
    
    at System.Linq.Expressions.Expression.New(Type type)
       at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)
       at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)
       at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
    

    Autofac configuration

    public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        var assemblies = GetApplicationAssemblies().ToList();
        BootAutofac(assemblies);
    }
    
    private static void BootAutofac(IEnumerable<Assembly> assemblies)
    {
        var builder = new ContainerBuilder();
        builder.RegisterControllers(assemblies.ToArray());
    
        builder.RegisterAssemblyTypes(assemblies.ToArray())
            .Where(t => t.Name.EndsWith("Service"))
            .AsImplementedInterfaces();
    
        builder.RegisterApiControllers(typeof (ApplicationTreeController).Assembly);
        builder.RegisterApiControllers(typeof (RaceTreeController).Assembly);
    
        DependencyResolver.SetResolver(new AutofacDependencyResolver(builder.Build()));
    }
    
    private static IEnumerable<Assembly> GetApplicationAssemblies()
    {
        IEnumerable<Assembly> assemblies = AppDomain.CurrentDomain.GetAssemblies();
        if (HostingEnvironment.InClientBuildManager)
        {
            assemblies = assemblies.Union(BuildManager.GetReferencedAssemblies().Cast<Assembly>()).Distinct();
        }
    
        return assemblies.Where(assembly => assembly.GetName().Name.Contains("Indegrade"));
    }
    

    RaceTreeController

    [PluginController("Mayhem")]
    [Tree("mayhem", "race", "Races", "icon-message", "icon-message", true, 5)]
    public class RaceTreeController : TreeController
    {
        private readonly IRaceService _raceService;
    
        public RaceTreeController(IRaceService raceService)
        {
            _raceService = raceService;
        }
    

    Demo

    I put a little demo together. There is a Umbraco V7 site in the solution the credentials are as follows: Username: admin, Password: admin

    https://bitbucket.org/stevoknievo/umbracoautofacetest

    Our help would be greatly appreciated, Thanks Stefan

  • stevo 63 posts 106 karma points
    Jul 13, 2015 @ 19:22
    stevo
    0

    I tested it with Autofac 3.5.2 (Autofac.MVC4 3.1.0 and Autofac.WebApi 3.1.0) and create another repo. The result is the same. As soon as I try to go to the second level I get the error.

    Is that a bug in Umbraco?

    https://bitbucket.org/stevoknievo/umbracoautofac352

    At the first test I reference to Autofac 3.1.0 (Autofac.MVC4 3.1.0 and Autofac.WebApi 3.1.0)

  • stevo 63 posts 106 karma points
    Jul 13, 2015 @ 20:11
    stevo
    0

    It's the same with Umbraco 7.3 beta

    https://bitbucket.org/stevoknievo/umbraco7.3betaautofac352

    I have no idea. I hope someone can help as I can't work it out.

    Thanks in advance, Stefan

  • stevo 63 posts 106 karma points
    Jul 21, 2015 @ 04:30
    stevo
    0

    instead of the traditional constructor injection, I using the "DependencyResolver"

    public Constructor()
    {
        _fooService = DependencyResolver.Current.GetService<IFooService>(); 
    }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies