Copied to clipboard

Flag this post as spam?

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


  • Ben Margevicius 6 posts 77 karma points
    Nov 12, 2019 @ 15:08
    Ben Margevicius
    0

    Light Inject with Parameters

    Simple question.

    I need to inject a parameter in with light inject. I want to do something like this. I can't figure out how to do this.

    public void Compose(Composition composition)
            {    
                composition.Register<IGeocoder, GoogleGeocoder>(new GoogleGeocoder { APIKEY = "ApiKey"} );
    
            }
    

    edit: I figured it out. You have to get the underlying light inject container in your startup composer, and use that to register a constructor dependency

               public void Compose(Composition composition)
                {
                    #region Injection Registration 
                    var container = composition.Concrete as LightInject.ServiceContainer; //Advanced access to the injector https://www.zpqrtbnk.net/posts/composing-umbraco-v8/
                    container.RegisterConstructorDependency<IGeocoder>((factory, parameterInfo) => new GoogleGeocoder { ApiKey = "APIKEY" }); //https://www.lightinject.net/ #Implicit service registration
    
    
        ...
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft