简体   繁体   中英

Net Core 2 Console App - DI 'Unable to resolve service for type…'

I've been reading several posts but nothing seems to fit to my problem.

I'm developing a console app with .Net Core 2.1 and EF Core trying to follow Microsoft's advices but I'm facing with the next problem.

I've a project named myproject.data which contains all the interfaces and services. This one, for example

ILeagueService.cs

public interface ILeaguesService
{
    List<Leagues> GetAllLeaguesValids();
}

LeagueService.cs

private statsContext _context;

public LeaguesService(statsContext context)
{
    _context = context;
}

public List<Leagues> GetAllLeaguesValids()
{
    return _context.Leagues.Where(x => (x.DateFirstMatch != null || x.CurrentLastSeason == true) && x.Active == true).OrderBy(x => x.Id).ToList();
}

Then, I have all the methods of my app separated and all of them inherit from the same class. In this Base.cs class I setup the ServiceProvider

Base.cs

public ServiceProvider _serviceProvider;

public Base()
{
    ConfigureServices();

    _config = new HelperConfig(CONFIG_FILE);
    _html = GetHelperHtml();
    _context = GetContext();
}

private void ConfigureServices()
{
    _serviceProvider = new ServiceCollection()
        .AddScoped<ILeaguesService, LeaguesService>()
        .BuildServiceProvider();
}

When I try to use the LeagueService in one of the methods I get the 'Unable to resolve service for type myproject.stats.statsContext' error

GetNextMatches.cs

private ILeaguesService _leagueService;

public GetNextMatches()
{
   _config.GetSection(AppsettingsModel.BetExplorerUrlsSection).Bind(betExplorerSectionKeys);

    _leagueService = _serviceProvider.GetService<ILeaguesService>(); <-- In this line I get the error
}

When using the ServiceProvider DI you have to register all of the classes in the hierarchy. The DI container is trying to create your LeagueService class but to call its constructor it needs to create an instance of statsContext . However it cannot find that in its registry so it throws the exception.

The solution is to add statsContext to your services collection.

private void ConfigureServices()
{
    _serviceProvider = new ServiceCollection()
        .AddScoped<ILeaguesService, LeaguesService>()
        .AddScoped<statsContext>()
        .BuildServiceProvider();
}

I am going to assume your _context variable is the statsContext you want to inject so you can use your GetContext() method to create the context for you:

private void ConfigureServices()
{
    _serviceProvider = new ServiceCollection()
        .AddScoped<ILeaguesService, LeaguesService>()
        .AddSingleton<statsContext>(GetContext())
        .BuildServiceProvider();
}

This will call your GetContext() once to create your single instance of statsContext . Now, whenever you call

_leagueService = _serviceProvider.GetService<ILeaguesService>();

DI will inject the singleton instance of statsContext when it creates your LeageService class.

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