Open Closed

How to Inject service in static class #4033


User avatar
0
yasin.hallak.89@gmail.com created
  • ABP Framework version: v6.0.0
  • UI type: MVC
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no

Hi there ...

I need to inject service in static class. Is there any way in abp to do this please :)


5 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Can you share your static class?

  • User Avatar
    0
    yasin.hallak.89@gmail.com created

    hi

    Can you share your static class?

    public static class AssignmentExtensions
        {
            public static string AssignmentTypeHelper(this Assignment target, IStringLocalizer localize)
            {
                string assignmentType = string.Empty;
    
                if (target.IsTemporary)
                {
                    if (target.IsGuard()) assignmentType = $"{localize["Guard"]} - {localize["General.Temporary"]}";
    
                    if (target.IsReservedGuard()) assignmentType = $"{localize["ReservedGuard"]} - {localize["General.Temporary"]}";
    
                    if (target.IsFieldSupervisor())
                    {
                        assignmentType = $"{localize["FieldSupervisor"]} - {localize["General.Temporary"]}";
                    }
                }
                else
                {
                    assignmentType = $"{localize["Guard"]} - {localize["General.Permanently"]}";
                }
    
                return assignmentType;
            }
        }
    
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    You can consider passing services or IServiceProvider to static method.

  • User Avatar
    0
    yasin.hallak.89@gmail.com created

    You can consider passing services or IServiceProvider to static method.

    This method deep nested , I need to pass IServiceProvider to all methods !.

    I need efficient way more than this

  • User Avatar
    1
    gterdem created
    Support Team Senior .NET Developer

    This is not related to ABP but the language itself. Check How to inject dependency to static class.

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