Warm tip: This article is reproduced from serverfault.com, please click

ERROR DI (AggregateException: Some services are not able to be constructed)

发布于 2020-12-02 06:26:35

I beginner in .NET , I learn to follow tutorial but I Have this Message error when run program

System.InvalidOperationException: Error while validating the service descriptor 'ServiceType: pwiapi.Data.ILineRepo Lifetime: Scoped ImplementationType: pwiapi.Data.SqlLineRepo': Unable to resolve service for type 'pwiapi.Data.LineContext' while attempting to activate 'pwiapi.Data.SqlLineRepo'.
 ---> System.InvalidOperationException: Unable to resolve service for type 'pwiapi.Data.LineContext' while attempting to activate 'pwiapi.Data.SqlLineRepo'.
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, Type serviceType, Type implementationType, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, Int32 slot)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(ServiceDescriptor serviceDescriptor, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.ValidateService(ServiceDescriptor descriptor)
   --- End of inner exception stack trace ---
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.ValidateService(ServiceDescriptor descriptor)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable`1 serviceDescriptors, IServiceProviderEngine engine, ServiceProviderOptions options)

And This My Code startup.cs:

 public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<ConfigurationContext>(options => { options.UseSqlServer(Configuration.GetConnectionString("ConnectionOne"));
            });
           
            services.AddControllers();
            services.AddScoped<ILineRepo,SqlLineRepo>();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "pwiapi", Version = "v1" });
            });
        }

so this my Data (SqlLineRepo.cs):

 public class SqlLineRepo : ILineRepo
    {
        private readonly LineContext _context;

        public SqlLineRepo(LineContext context)
        {
            _context = context;
        }

        public Line GetLineByNo(string lineNo)
        {
            return _context.Lines.FirstOrDefault(p => p.LINE_NO == lineNo);
        }

        public IEnumerable<Line> GetLines()
        {
            return _context.Lines.ToList();
        }
    }

this my interface (ILineRepo.cs)


using pwiapi.Models;
using System.Collections.Generic;

namespace pwiapi.Data
{
    public interface ILineRepo
    {
        IEnumerable<Line> GetLines();
        Line GetLineByNo(string lineNo);
    }
}

this my controller :

namespace pwiapi.Controllers
{
    [Route("api/commands")]
    [ApiController]
    public class LineController : ControllerBase
    {
        private readonly ILineRepo _repository;

        public LineController(ILineRepo repository) {
            _repository = repository;
        }
        //private readonly MockLineRepo _repository = new MockLineRepo();

        [HttpGet]
        public ActionResult<IEnumerable<Line>> GetAllLines() {
            var lineItems = _repository.GetLines();
            return Ok(lineItems);
        }

        [HttpGet("{lineno}")]
        public ActionResult<Line> GetLineByNo(string lineno) {
            var lineItem = _repository.GetLineByNo(lineno);
            return Ok(lineItem);
        }
    }
}

any body can help to fix and explain what happen here? I have fooking for how to fixed this, but program still error, i cant undestand

Questioner
Welldy Rosman
Viewed
0
Kahbazi 2020-12-02 15:00:05

You need to register LineContext to the IServiceCollection.

services.AddDbContext<LineContext>(options => ...)