0

I'm getting an 500 error on request if adding the IVoltRepository parameters in constructor.

If I remove the parameter IVoltRepository from the constructor, request will be handled ok.

I've already registered services for DI:

public class VoltProcessing : IVoltProcessing
{
        private readonly IVoltRepository _voltRepository;
        private readonly ILoggingAttributes _loggingAttributes;

        public VoltProcessing(IVoltRepository voltrepository, ILoggingAttributes loggingAttributes)
        {
            _voltRepository = voltrepository;
            _loggingAttributes = loggingAttributes;
        }
}

services.AddScoped<IVoltRepository, VoltRepository>();                 
services.AddSingleton<IVoltProcessing, VoltProcessing>();

I'm getting the 500 error if am using IVoltRepository as a parameter in the VoltProcessing constructor.

How can I resolve this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

Cannot consume scoped service from singleton, try:

services.AddScoped<IVoltProcessing, VoltProcessing>();

Read this to know more.

You can't use a service with a smaller lifetime. Scoped services only exist per-request, while singleton services are created once and the instance is shared.

Qing Guo
  • 6,041
  • 1
  • 2
  • 10