Search code examples
c#asp.netasp.net-core

Error when trying to add Irepositories to services with addScoped ASP.Net core 2.1


I'm trying to add my Irepository interface and the irepository to the services so I can use them.

I do it like this:

services.AddScoped<AuctioniRepository, IrepoAuctionInterface>();

And here is the error I get:

Error CS0311 The type 'NackowskiLillMygel.Data.IrepoAuctionInterface' cannot be used as type parameter 'TImplementation' in the generic type or method 'ServiceCollectionServiceExtensions.AddScoped(IServiceCollection)'. There is no implicit reference conversion from 'NackowskiLillMygel.Data.IrepoAuctionInterface' to 'NackowskiLillMygel.Data.AuctioniRepository'. NackowskiLillMygel source\repos\NackowskiLillMygel\NackowskiLillMygel\Startup.cs 39 Active

I don't understand what I've done wrong. Also if you need any more code from the repositories please tell me.

I'm very grateful for answers!


Solution

  • You are registering the interface as the implementation of IrepoAuctionInterface. The signature of the .AddScoped() method is as following:

    public static IServiceCollection AddScoped<TService, TImplementation>(this IServiceCollection services)
        where TService : class
        where TImplementation : class, TService;
    

    This means that the TService should implement TImplementation, you did it the other way around.

    You should flip the arguments around like this:

    services.AddScoped<IrepoAuctionInterface, AuctioniRepository>();