Reputation: 305
Greeting all, firstly let me exclaim that I have scoured the existing questions to no avail.
Forgive me if I missed one that addresses this specific concern. This .NET Core 3.1 Web API uses another project a ClassLibrary in the solution to support its HTTP Verb methods. A class in the ClassLibrary is named Cars and it is inherited from ICars, probably just for default Dependency Injection purposes of .NET Core, but of course this is used (constructor injected) for the Controller CarController.
However there is another Controller BlueCarsController that uses a ClassLibrary BlueCars via its interface IBlueCars. Note the interfaces expose completely separate respectively distinct methods implemented in the respective classes.
My attempt to properly inject the inherited interface:
services.AddScoped<IBlueCars, BlueCars>();
results in the compilation error:
" The type 'ModelsClassLibrary.BlueCars' cannot be used as type parameter 'TImplementation' in the generic type or method 'ServiceCollectionServiceExtensions.AddScoped<TService, TImplementation>(IServiceCollection)'. There is no implicit reference conversion from 'ModelsClassLibrary.BlueCars' to 'ModelsClassLibrary.IBlueCars'. "
This implementation isn't so weighty that I couldn't address this easily by combining the BlueCars' functionality into the Cars class but let's face it they are honestly "BlueCars" after all and that wouldn't be proper.
I didn't appreciate the mark down however thanks for that
//---------------------class BlueCars--------------------------
namespace ModelsClassLibrary
{
public interface IBlueCars
{
Task<CallsListDto> GetBlueCarsAsync(string Id, int OtherId, string brand);
}
public class BlueCars : Cars
{
private readonly ILogger<IBlueCars> _logger;
private readonly ISomeApiService _someWebApiService;
private readonly ICars _cars;
private readonly IBlueCars _blueCars;
private readonly IConfiguration _configuration;
public BlueCars(ISomeApiService crmWebApiService,
ILogger<IBlueCars> logger,
ICars cars,
IConfiguration configuration,
IBlueCars _blueCars
)
{
_crmWebApiService = crmWebApiService;
_logger = logger;
_cars = cars;
_configuration = configuration;
}
public async Task<Cars> GetBlueCarsAsync(
string Id,
int OtherId,
string brand)
{
var results = new List<Cars>();
// blah blah Do Stuff
}
}
}
//---------------------class Cars--------------------------
namespace CrmDataAccess
{
public interface ICars
{
Task<CarsListDto> GetCarsAsync(string user, string someBrand);
}
public class Cars: ICars
{
public const int Nopage = -1; // Move to crmStringThingie
private readonly ILogger<CallList> _logger;
private readonly ICrmWebApiService _crmWebApiService;
private readonly IConfiguration _configuration;
public CallList(ICrmWebApiService crmWebApiService, ILogger<CallList> logger, IConfiguration configuration)
{
_logger = logger;
_crmWebApiService = crmWebApiService;
_configuration = configuration;
}
public async Task<Dictionary> GetCarsAsync(string user, string someBrand);
{
var result = new CarsListDto();
// dance fly iterate harvest wash rinse repeat make cool stuff call a service whatever tada!
return result;
}
}
}
//-------------------ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
// various config stuff authentication stuff ommitted
services.AddScoped<ICars, Cars>(); // Works fine for CarsController
services.AddScoped<IBlueCars, BlueCars>(); // Since BlueCars inherits from Cars how do you do this correctly ?
// the interfaces expose completely different Get methods
//---- Various Configure stuff Swagger stuff blah blah
}
//-------------------------------
namespace SomeWebApi.Controllers
{
public class CarsController : BaseController // default logger implementation & other stuff
{
private readonly ISomeService _someService;
private readonly ICars _cars;
public CallsController(ISomeService _someService, ICars cars, ILogger<CallsController> logger) : base(_someService, logger)
{
_cars = cars;
}
[HttpGet]
public async Task<CarsListDto> GetAsync(string user, string someBrand)
{
CarsListDto results = await _cars.GetCarsAsync(user,someBrand);
return result;
}
}
}
//-------------------------------
namespace SomeWebApi.Controllers
{
public class BlueCarsController : BaseController // default logger implementation & other stuff
{
private readonly ISomeService _someService;
private readonly IBlueCars _blueCars;
public BlueCarsController(ISomeService _someService, IBlueCars blueCars, ILogger<CallsController> logger) : base(_someService, logger)
{
_blueCars = blueCars;
}
[HttpGet]
public async Task<CarsListDto> GetAsync(string id,string brand)
{
// magically get parameters for GetAsync
CarsListDto results = await _blueCars.GetBlueCarsAsync(Id, OtherId,someBrand);
}
}
}
Upvotes: 0
Views: 732
Reputation: 247551
Based on the shown code, the error message is accurate. BlueCars
does not derive from IBlueCars
Refactor BlueCars
to be derived from IBlueCars
and not Cars
public class BlueCars : IBlueCars {
//...
}
Upvotes: 1