Copied to clipboard

Flag this post as spam?

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


  • Chris Spanellis 45 posts 191 karma points
    Feb 28, 2019 @ 00:34
    Chris Spanellis
    0

    Umbraco 8 and IoC

    The documentation for v7 and IoC doesn't apply to v8. Is it as simple as implementing IComposer, and then registering the types we need? What about the controllers and the API pieces as well?

    Thanks

  • louisjrdev 107 posts 344 karma points c-trib
    Feb 28, 2019 @ 13:36
    louisjrdev
    0

    Not sure about the IOC stuff, but im pretty sure controllers are all the same

  • Daniël Knippers 153 posts 1117 karma points MVP 2x c-trib
    Feb 28, 2019 @ 13:41
    Daniël Knippers
    107

    You can indeed just implement IUserComposer to register your types with the DI container and then they will be injected when needed.

    Small example:

    using Umbraco.Core;
    using Umbraco.Core.Composing;
    
    public class SomeComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.Register<IServiceOne, ServiceOne>();
            composition.Register<IServiceTwo, ServiceTwo>();        
        }
    }
    

    And then in your ApiController:

    using Umbraco.Web.WebApi;
    
    public class SomeApiController : UmbracoApiController
    {
        private readonly IServiceOne _serviceOne;
        private readonly IServiceTwo _serviceTwo;
    
        public SomeApiController(IServiceOne serviceOne, IServiceTwo serviceTwo) 
        {
            _serviceOne = serviceOne;
            _serviceTwo = serviceTwo;
        }        
    
        // API methods here as usual
    }
    
  • Chris Spanellis 45 posts 191 karma points
    Feb 28, 2019 @ 16:15
    Chris Spanellis
    0

    Thanks! At least I was close ;)

  • Marcio Goularte 388 posts 1360 karma points
    Feb 28, 2019 @ 19:44
    Marcio Goularte
    0

    very good!

  • Mauri Pardo 2 posts 72 karma points
    Aug 31, 2019 @ 15:46
    Mauri Pardo
    0

    Hi Daniël, i installed umbraco 8.1.3 and i'm trying to configure dependecy injection following this post, but not work. Me code is:

    Controller:

        public class ContactController : UmbracoApiController
        {
            private readonly IContactService contactService;
    
            public ContactController(IContactService contactService)
            {
                this.contactService = contactService;
            }
    
            [HttpPost]
            public void Post([FromBody]ContactDto request)
            {
                this.contactService.Register(request);
            }
        }
    

    Service:

    public interface IContactService
    {
        void Register(ContactDto contact);
    }
    
    
    
    public class ContactService : IContactService
    {
        public ContactService()
        {
        }
    
        public void Register(ContactDto contact)
        {
          //Implement
        }
    }
    

    DI Config:

    public class ApplicationComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.RegisterFor<IContactService, ContactService>();
        }
    }
    

    Error:

      "Message": "An error has occurred.",
    "ExceptionMessage": "An error occurred when trying to create a controller of type 'ContactController'. Make sure that the controller has a parameterless public constructor.",
    "ExceptionType": "System.InvalidOperationException",
    "StackTrace": "   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()",
    "InnerException": {
        "Message": "An error has occurred.",
        "ExceptionMessage": "Unable to resolve type: MySite.Controllers.ContactController, service name: ",
        "ExceptionType": "System.InvalidOperationException",
        "StackTrace": "   at LightInject.ServiceContainer.CreateDelegate(Type serviceType, String serviceName, Boolean throwError) in C:\\projects\\lightinject\\src\\LightInject\\LightInject.cs:line 4748\r\n   at LightInject.ServiceContainer.CreateDefaultDelegate(Type serviceType, Boolean throwError) in C:\\projects\\lightinject\\src\\LightInject\\LightInject.cs:line 4705\r\n   at LightInject.ServiceContainer.TryGetInstance(Type serviceType) in C:\\projects\\lightinject\\src\\LightInject\\LightInject.cs:line 3493\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)",
        "InnerException": {
            "Message": "An error has occurred.",
            "ExceptionMessage": "Unresolved dependency [Target Type: MySite.Controllers.ContactController], [Parameter: contactService(IServices.Contacts.IContactService)], [Requested dependency: ServiceType:IServices.Contacts.IContactService, ServiceName:]",
            "ExceptionType": "System.InvalidOperationException",
            "StackTrace": "   at LightInject.ServiceContainer.EmitConstructorDependency(IEmitter emitter, Dependency dependency) in C:\\projects\\lightinject\\src\\LightInject\\LightInject.cs:line 4163\r\n   at LightInject.ServiceContainer.EmitConstructorDependencies(ConstructionInfo constructionInfo, IEmitter emitter, Action`1 decoratorTargetEmitter) in C:\\projects\\lightinject\\src\\LightInject\\LightInject.cs:line 4120\r\n   at LightInject.ServiceContainer.EmitNewInstanceUsingImplementingType(IEmitter emitter, ConstructionInfo constructionInfo, Action`1 decoratorTargetEmitMethod) in C:\\projects\\lightinject\\src\\LightInject\\LightInject.cs:line 4081\r\n   at LightInject.ServiceContainer.EmitNewInstance(ServiceRegistration serviceRegistration, IEmitter emitter) in C:\\projects\\lightinject\\src\\LightInject\\LightInject.cs:line 4038\r\n   at LightInject.ServiceContainer.CreateDynamicMethodDelegate(Action`1 serviceEmitter) in C:\\projects\\lightinject\\src\\LightInject\\LightInject.cs:line 3777\r\n   at LightInject.ServiceContainer.CreateInstanceDelegateIndex(Action`1 emitMethod) in C:\\projects\\lightinject\\src\\LightInject\\LightInject.cs:line 4693\r\n   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)\r\n   at LightInject.ServiceContainer.EmitLifetime(ServiceRegistration serviceRegistration, Action`1 emitMethod, IEmitter emitter) in C:\\projects\\lightinject\\src\\LightInject\\LightInject.cs:line 4664\r\n   at LightInject.ServiceContainer.<>c__DisplayClass153_0.<CreateEmitMethodWrapper>b__0(IEmitter ms) in C:\\projects\\lightinject\\src\\LightInject\\LightInject.cs:line 3856\r\n   at LightInject.ServiceContainer.CreateDynamicMethodDelegate(Action`1 serviceEmitter) in C:\\projects\\lightinject\\src\\LightInject\\LightInject.cs:line 3777\r\n   at LightInject.ServiceContainer.CreateDelegate(Type serviceType, String serviceName, Boolean throwError) in C:\\projects\\lightinject\\src\\LightInject\\LightInject.cs:line 4743",
            "InnerException": {
                "Message": "An error has occurred.",
                "ExceptionMessage": "Missing public constructor for Type: IServices.Contacts.IContactService",
                "ExceptionType": "System.InvalidOperationException",
                "StackTrace": "   at LightInject.MostResolvableConstructorSelector.Execute(Type implementingType) in C:\\projects\\lightinject\\src\\LightInject\\LightInject.cs:line 5558\r\n   at LightInject.TypeConstructionInfoBuilder.Execute(Registration registration) in C:\\projects\\lightinject\\src\\LightInject\\LightInject.cs:line 5719\r\n   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)\r\n   at LightInject.ServiceContainer.EmitNewInstance(ServiceRegistration serviceRegistration, IEmitter emitter) in C:\\projects\\lightinject\\src\\LightInject\\LightInject.cs:line 4028\r\n   at LightInject.ServiceContainer.<>c__DisplayClass153_0.<CreateEmitMethodWrapper>b__0(IEmitter ms) in C:\\projects\\lightinject\\src\\LightInject\\LightInject.cs:line 3856\r\n   at LightInject.ServiceContainer.<>c__DisplayClass153_0.<CreateEmitMethodWrapper>b__0(IEmitter ms) in C:\\projects\\lightinject\\src\\LightInject\\LightInject.cs:line 3856\r\n   at LightInject.ServiceContainer.EmitConstructorDependency(IEmitter emitter, Dependency dependency) in C:\\projects\\lightinject\\src\\LightInject\\LightInject.cs:line 4158"
            }
        }
    }
    

    Can you help me? Thanks

  • Mauri Pardo 2 posts 72 karma points
    Sep 02, 2019 @ 21:21
    Mauri Pardo
    0

    i found the issue. I explain that here

  • Daniël Knippers 153 posts 1117 karma points MVP 2x c-trib
    Sep 16, 2019 @ 06:45
    Daniël Knippers
    0

    Hi Mauri,

    You are using RegisterFor(), but you are looking for the Register() method instead, which is a built-in extension method in the Umbraco.Core namespace (hence my example has the using Umbraco.Core; line). I see in your referenced StackOverflow that you have added this extension method yourself but that is not necessary as it is provided by Umbraco.

  • Jeffrey Schoemaker 408 posts 2138 karma points MVP 8x c-trib
    Feb 28, 2019 @ 13:59
    Jeffrey Schoemaker
    3

    Hi Chris,

    and maybe this blogpost helps you out a bit as well: https://www.perplex.nl/en/blog/2019/umbraco-v8-changes-for-developers/

    Cheerio,

    Jeffrey

  • Chris Spanellis 45 posts 191 karma points
    Feb 28, 2019 @ 16:16
    Chris Spanellis
    0

    Great article... thanks!

  • bronzewind 42 posts 125 karma points
    Jun 24, 2019 @ 17:00
    bronzewind
    0

    Hi Chris! Were you able to make it work? I'm in the exact same point and I can't make mine work. Any suggestions?

  • Chris Spanellis 45 posts 191 karma points
    Jun 24, 2019 @ 20:48
    Chris Spanellis
    0

    Yup! I just implemented my own IUserComposer.

    public class ApplicationComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.Register(..., ...);
        }
    }
    
  • andrew shearer 512 posts 661 karma points
    Sep 12, 2019 @ 05:23
    andrew shearer
    0

    hi Jeffrey

    thanks for that blog post – that’s very helpful for me too.

    Do you know what DI container this is using - Is it LightInject? I suspect the umbraco doc https://our.umbraco.com/documentation/reference/using-ioc saying IoC isn’t used in the v8 core is out of date?

    I have some pre-existing DI code using Autofac in an umbraco 7 project, and looking for the equivalent best practice in umbraco 8. It sounds like ‘Compositions’ is what I should port the code to.

    Thanks

    Andrew

  • Søren Kottal 713 posts 4571 karma points MVP 6x c-trib
    Sep 12, 2019 @ 10:25
    Søren Kottal
    1

    Hi Andrew

    That article is for v7. Umbraco 8 uses LightInject.

  • andrew shearer 512 posts 661 karma points
    Sep 18, 2019 @ 01:30
    andrew shearer
    0

    hi - does anyone know of a blog or thread explaining the various composition methods in a bit more detail / real world examples etc?

    Register
    RegisterFor
    RegisterAuto
    RegisterUnique
    RegisterUniqueFor
    

    thanks

  • Perry Cope 31 posts 195 karma points
    Feb 11, 2020 @ 12:33
    Perry Cope
    0

    In 8.5.3 i have to use Register like this

    composition.Register(typeof(IMyService), typeofMyService));

    Is this correct or am i missing something. all the stuff i seen online suggest i dont need to use the typeof wrapper but i cant get it to work without it?

  • Daniël Knippers 153 posts 1117 karma points MVP 2x c-trib
    Feb 11, 2020 @ 12:35
    Daniël Knippers
    2

    Hi Perry,

    Make sure to include using Umbraco.Core; at the top of your file. The Register<T>(IService, Service) method and some others are defined as extension methods in the Umbraco.Core namespace.

Please Sign in or register to post replies

Write your reply to:

Draft