Home > Back-end >  ASP.NET CORE : Unable to resolve service for type 'API.SQLConnection.IDBConnection' while
ASP.NET CORE : Unable to resolve service for type 'API.SQLConnection.IDBConnection' while

Time:10-22

I've started to mess around with ASP.NET Core, ive been using dependency injection to inject the configuration into my Controller class but its giving me the following error:

InvalidOperationException: Unable to resolve service for type 'API.SQLConnection.IDBConnection' while attempting to activate 'API.Controllers.UsersController'

Users controller (UserController.cs)

  public class UsersController : ControllerBase
  {
    private readonly IDBConnection _isqlConnection;
    private readonly DataContext _context; // here we have used dependency injection so that we can access the data from the DataContext class.
    public UsersController(DataContext context)
    {         
        _context = context;
    }
    public UsersController(IDBConnection connection) 
    {
        _isqlConnection = connection;
    }

    [HttpGet]
    public ActionResult<IEnumerable<AppUser>>GetUsers() // we are going to be returning a type of action result. 
    {
        _isqlConnection.GetConnection().Open();
        var user = _context.Users.ToList();
        _isqlConnection.GetConnection().Close();
        return user;
       
    }

Start up (Startup.cs)

 public class Startup
 {
    private readonly IConfiguration _config;
    public Startup(IConfiguration configuration)
    {
        _config = configuration;
    }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<DataContext>(options =>
        {
            options.UseSqlServer(_config.GetConnectionString("DefaultConnection"));
        });           
        services.AddHttpContextAccessor();
        services.AddControllers();
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "API", Version = "v1" });
        });
    }

AppUser (AppUser.cs)

public class AppUser 
{
    public int Id { get; set; }
    public string Username { get; set; }
}

DataContext (DataContext.cs)

public class DataContext : DbContext
{
    public DbSet<AppUser> Users { get; set; }

    public DataContext(DbContextOptions options) : base(options)
    {
    }        
}

DBConnection (DBConnection.cs)

public interface IDBConnection 
{
    SqlConnection GetConnection();
    
}
public class DBConnection : IDBConnection
{
    public SqlConnection con = new SqlConnection(@"Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=DatingSite;Integrated Security=True");
    public SqlConnection GetConnection()
    {
        return con;
    }

}

Not really sure what is wrong does anyone have any ideas?

CodePudding user response:

I would assume that you need to register the service with the dependency injection container, since you are using it for dependency injection. In Startup.cs in ConfigureServices(), try to add:

services.AddScoped<IDBConnection, DBConnection>();

  • Related