Copied to clipboard

Flag this post as spam?

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


  • Matthew 14 posts 84 karma points
    Oct 02, 2019 @ 01:01
    Matthew
    0

    Umbraco8 dependancy injection with Autofac

    Hi,

    Hoping somebody can point me in the right direction. I am trying to add dependency injection with Autofac to my new Umbraco8 (8.1.5) App.

    It seems the registration of types is simply not being picked up. Here is the error from my controller (inheriting surfacecontroller) ...

    Unresolved dependency [Target Type: ttweb.Controllers.MemberController], [Parameter: myAwesome(ttweb.common.Helpers.IMyAwesomeContext)], [Requested dependency: ServiceType:ttweb.common.Helpers.IMyAwesomeContext, ServiceName:]

    You can see I am just using a simple test class - MyAwesomeContext which implements IMyAwesomContext.

    I am using the composer / component setup to fire during startup (because ApplicationEventHandler has been removed from Umbraco8).

    So Autofac registration (of test class and a keyed index) takes place as follows ...

        *[RuntimeLevel(MinLevel = RuntimeLevel.Run)]
        public class MyComposer : IUserComposer
        {
            public void Compose(Composition composition)
            {
                composition.Components().Append<MyComponent>();
    
                var domainAssemblies = AppDomain.CurrentDomain.GetAssemblies();
                var assembly = GetType().Assembly;
                var builder = new ContainerBuilder();
                // register all controllers found in your assembly
                builder.RegisterControllers(domainAssemblies);
                builder
                    .RegisterAssemblyTypes(assembly)
                    .Where(type => type.GetInterfaces().Contains(typeof(ITransform)))
                    .Keyed<ITransform>(transform => transform.Name);
                builder.RegisterType<MyAwesomeContext>().As<IMyAwesomeContext>().SingleInstance();
                var container = builder.Build();
                DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            }
        } ......*
    

    This all worked absolutely fine in another Umbraco7 (7.6.5) app I am running. Is the lighinject or umbraco DI somehow interfering with Autofac ? .....

    Any help would be much appreciated.

    Matt

  • Yakov Lebski 590 posts 2344 karma points
    Oct 08, 2019 @ 02:11
    Yakov Lebski
    0

    Umbraco 8 has internal IoC using LightInject, maybe you can use LightInject instead of Autofac?

  • Matthew 14 posts 84 karma points
    Oct 17, 2019 @ 21:33
    Matthew
    0

    Thanks for the reply. I guessed by the lack of any kind of response to this question that Umbraco removed the flexibility of using our own dependency injection model. In my case Autofac. There could be a way of doing it, but clearly not as straight forward as with Umbraco 7. I had a DI pattern for my Umbraco 7 sites, which I wanted to use with Umbraco 8. So have had to change afew things to fit in with their lightInject :/.

  • Yakov Lebski 590 posts 2344 karma points
    Oct 18, 2019 @ 01:19
    Yakov Lebski
    0

    I think the right sentace is insert flexability to core :) In my expirince LightInject is much faster than Autofac you ca see here how to use it, it should be very simple to use https://our.umbraco.com/documentation/Reference/Using-Ioc/

Please Sign in or register to post replies

Write your reply to:

Draft