Open Closed

how to user service injection for IMenuContributor #2808


User avatar
0
AlderCove created
  • ABP Framework version: v5.1.4
  • UI type: Angular / MVC
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes

For the Web.Public MVC Project, we require certain menu items are available/unavailable based on the state of the user. I think the right approach is to use DI to inject the interface for the application service I want to use to get the data.

However, I can't add the interface to the constructor of the MenuContributor class because it would be required in the module class itself:

I've read the dependency injection section of the guide and think that I should be using property injection but it's not clear to me how I can get an instance of the interface: https://docs.abp.io/en/abp/5.2/Dependency-Injection#property-injection

Can you please provide some guidance on how to achieve this?

Thanks


2 Answer(s)
  • User Avatar
    0
    enisn created
    Support Team .NET Developer

    MenuContributor will be triggered with MenuConfigurationContext and it contains ServiceProvider as a property. You can resolve any service by using it:

    public async Task ConfigureMenuAsync(MenuConfigurationContext context)
    {
        var userTaskPublicAppService = context.ServiceProvider.GetRequiredService<IUserTaskPublicAppService>();
    }
    
  • User Avatar
    0
    AlderCove created

    Worked a charm!

    Thanks so much.

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11