Open Closed

How to reference a service directly from the IOC container in ABP Framework (like was possible with ASPNET Zero)? #4087

dev1_premierpoint created

Check the docs before asking a question: Check the samples, to see the basic tasks: The exact solution to your question may have been answered before, please use the search on the homepage.

If you're creating a bug/problem report, please include followings:

  • ABP Framework version: v5.3.1
  • UI type: Blazor
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no
  • Exception message and stack trace:
  • Steps to reproduce the issue:"

We are in the process of re-engineering our ASP.NET Zero application in ABP Commercial.

In the ASPNET Zero version, we had a custom class to enrich our Serilog-based event logs. It had to inherit from ILogEventEnricher, provided by Serilog.

Because of the way ILogEventEnricher works, we couldn't inject any dependencies in the constructor for the class. We had to directly reference the needed service from the IOC container, like this:

var abpSession = Abp.Dependency.SingletonDependency.Instance;

We understand this is something we shouldn't normally do, but we had no choice due to the constraints of Serilog.

How would we do this (directly reference a service object in the IOC container) in ABP Framework? I have looked at the documentation and search the forum, but haven't found any instructions.

2 Answer(s)
  • 0
    malik.masis created
    Support Team


    ABP introduces conventional service registration. You need not do anything to register a service by convention. It's automatically done

    There are different ways to inject dependencies. Hope it helps you. Could you have a look at it here, please?


  • 0
    dev1_premierpoint created

    I already read the documentation you pointed to, but coming from ASP.NET Boilerplate and Castle Windsor, I'm struggling some with the change in terminology and the differences between the two approaches to dependency injection.

    But, I think I have been able to figure out how it can work and it seems cleaner than what I had to do before.

    In my Application project I have created this class that inherits from Serilog's ILogEventEnricher:

    Then, in Program.cs in my Blazor project I can add this to the configuration of Serilog and it seems to work:

    In ASP.NET Boilerplate this would not work as shown above because I had to inject the AbpSession service in my class using some way other than constructor dependency injection or property dependency injection.

    The only solution was to directly reference the service from the IOC container.

    Glad that seems no longer necessary.