DNN Forums

Ask questions about your website to get help learning DNN and help resolve issues.

Dependency Injection from Provider

Sort:
You are not authorized to post a reply.





New Around Here





    I have implmented a custom profile provider class from the abstract ProfileProvider class. What is the proper way to access the DI container. I have tried this

    HttpContext.Current.GetScope().ServiceProvider.GetService(typeof(IXyz))

    But I am not sure if this is the correct way. I am currently on version 9.13.0






    Advanced Member





      Starting in DNN 10, you can just add your dependency as a constructor parameter and the platform will provide it automatically. Until then, getting the service provider via the HTTP context is a good option, though you may want to make sure HttpContext.Current isn't null (if it is, you'll need to manually construct an instance instead of using the DI container).
      DNN partner specializing in custom, enterprise DNN development https://engagesoftware.com/showcase





      New Around Here





        My reply seems to have been lost! Thanks for the response - will check that the current context is available. My problem is that the library extension project only has a reference to the abstraction and not the implementation. I could reference the component but that is not an ideal scenario - was going to add to the Component Factory from the startup class but that doesn't work due to the order that the serviceprovider and component on component factory is built. Hmm what to do ...
        You are not authorized to post a reply.

        These Forums are dedicated to the discussion of DNN Platform.

        For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

        1. If you have (suspected) security issues, please DO NOT post them in the forums but instead follow the official DNN security policy
        2. No Advertising. This includes the promotion of commercial and non-commercial products or services which are not directly related to DNN.
        3. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
        4. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
        5. No Flaming or Trolling.
        6. No Profanity, Racism, or Prejudice.
        7. Site Moderators have the final word on approving / removing a thread or post or comment.
        8. English language posting only, please.

        Would you like to help us?

        Awesome! Simply post in the forums using the link below and we'll get you started.

        Get Involved