SimpleInjector: The configuration is invalid. The type is directly or indirectly depending on itself

1

I have the following problem when applying the SimpleInjector container in a simple application:

The configuration is invalid. 
The type BookAppService is directly or indirectly depending on itself. 
The cyclic graph contains the following types: BookAppService -> BookAppService -> BookAppService.

This is the BookAppService class:

public class BookAppService : AppServiceBase<Book>, IBookAppService
{
    private readonly IBookAppService _bookService;

    public BookAppService(IBookAppService bookService) : base(bookService)
    {
        _bookService = bookService;
    }

    public IEnumerable<Book> GetSpecialBooks(IEnumerable<Book> books)
    {
        return _bookService.GetSpecialBooks(_bookService.GetAll());
    }

And here is the SimpleInjectorInitializer configuration:

container.Register(typeof(IAppServiceBase<>), typeof(AppServiceBase<>));
container.Register<IBookAppService, BookAppService>(Lifestyle.Scoped);

I'm a good beginner in IoC and DI, and thank you for any help and tips. Thank you.

    
asked by anonymous 31.03.2018 / 00:25

1 answer

1

The problem is that you are asking for an instance of BookAppService within an instance of itself, causing infinite recursion.

This does not make much sense, you can access the current instance using this and AppServiceBase inherited using base .

    
31.03.2018 / 00:45