简体   繁体   中英

Some services are not able to be constructed Core 5.0

when I build my project everything builds fine but when I run it in the browser I get the following errors from the Program.cs file. I have the service added in the Startup.cs as well. If I comment out the INewsModelfactory ref in the Controller it runs but I dont understand what Im missing thats throwing the error.

1 of 1

System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: websiteName.Web.Factories.INewsModelFactory Lifetime: Scoped ImplementationType: websiteName.Web.Factories.NewsModelFactory': Unable to resolve service for type 'websiteName.Services.News.INewsService' while attempting to activate 'websiteName.Web.Factories.NewsModelFactory'.)'

2 of 2

InvalidOperationException: Unable to resolve service for type 'websiteName.Services.News.INewsService' while attempting to activate 'websiteName.Web.Factories.NewsModelFactory'.

I think I've added everything that is involved, please let me know if there is anything else that is needed.

Controller

public async Task<IActionResult> List()
{
    var model = await _newsModelFactory.PrepareNewsItemListModelAsync();

    return View(model);
}

INewsModelFactory

NewsItemModel PrepareNewsItemModelAsync(NewsItemModel model, NewsItem newsItem);

NewModelFactory

private readonly INewsService _newsService;

public NewsModelFactory(INewsService newsService)
{
    _newsService = newsService;
}
public async Task<NewsItemListModel> PrepareNewsItemListModelAsync()
{
    var newsItems = await _newsService.GetAllNewsAsync();

    var model = new NewsItemListModel
    {
        NewsItems =  newsItems.Select(newsItem =>
        {
            var newsModel = new NewsItemModel();
            PrepareNewsItemModelAsync(newsModel, newsItem);
            return newsModel; 

        }).ToList()
    };
    return model; 
}

NewsService

public async Task<IEnumerable<NewsItem>> GetAllNewsAsync()
{
    var news = await _db.NewsItems.ToListAsync();

    return news;
}

DbContext

public class ApplicationDbContext : IdentityDbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) {}
    
    public DbSet<NewsItem> NewsItems { get; set; }      
}

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    . . .  
    
    services.AddScoped<INewsModelFactory, NewsModelFactory>();  
    services.AddScoped<INewsService,NewsService>();                 
}

The order of injecting the services should be reversed. You need to inject the NewsService first. Because the NewModelFactory needs to use it in its constructor. When the code tries to initialize the NewFactoryModel the INewsService it needs in its constructor is yet to be injected.

You need to reverse the lines in Startup/ConfigureServices.

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
. . .  
    services.AddScoped<INewsService,NewsService>();   
    services.AddScoped<INewsModelFactory, NewsModelFactory>();            
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM