LittleMygler
LittleMygler

Reputation: 632

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!

Upvotes: 0

Views: 5417

Answers (2)

Sharath BR
Sharath BR

Reputation: 29

Just implement the interface IrepoAuctionInterface in the class AuctioniRepository

like

public class AuctioniRepository : IrepoAuctionInterface
{
}

Upvotes: 3

user1336
user1336

Reputation: 7205

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>();

Upvotes: 3

Related Questions