Gobelet
Gobelet

Reputation: 483

.net Core 3 / API / DI / Repository pattern

I'm trying to create a web API in .net Core. I would like this API calls a DLL with repository design pattern implemented in it. In this whole thing I tried to use dependency injection but I have some issues to manage context for database in repository.

I would like than the context one new context per lifetime of a call to the API.

When I try to execute my code I have an exception at line CreateHostBuilder(args).Build().Run(); in Main.

This Exception is :

'Some services are not able to be constructed' InvalidOperationException : Unable to resolve service for type 'BX_Security_AccessBase.Context.SecurityContext' while attempting to activate 'BX_Security_AccessBase.Repository.UsersRepository'.

I know the code is incomplete and won't work completely but at least it should break way later than actually. I think I made a mistake in the architecture.

There is a lot of code below but I couldn't isolate my problem.

Thank you everybody.

In my API I have :

Startup.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();

        services.AddScoped<IUsersRepository, UsersRepository>();
        services.AddScoped<IUserService, UserService>();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

User.cs

public class User
{
    public User() {}

    public int UserId { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public DateTime Birthdate { get; set; }
}

UserService.cs

public class UserService : IUserService
{
    private readonly AppSettings _appSettings;
    private readonly IUsersRepository _userRepository;

    public UserService(IOptions<AppSettings> appSettings, IUsersRepository userRepository)
    {
        _appSettings = appSettings.Value;
        _userRepository = userRepository;

    }

    public IEnumerable<User> GetAll()
    {
        return _userRepository.GetAllUsers().Select(u=> new User());
    }
}

IUserService.cs

public interface IUserService
{
    public IEnumerable<User> GetAll();
}

AppSettings.cs

 public class AppSettings
{
    public string Secret { get; set; }
}

UsersController.cs

[Route("api/[controller]")]
[ApiController]
public class UsersController : ControllerBase
{
    private IUserService _userService { get; }

    public UsersController(IUserService userService)
    {
        _userService = userService;
    }

    [HttpGet]
    public IActionResult GetAll()
    {
        var users = _userService.GetAll();
        return Ok(users);
    }
}

In the DLL I have :

SecurityContext.cs

public partial class SecurityContext : DbContext
{
    public SecurityContext(DbContextOptions<SecurityContext> options) : base(options) { }

    public DbSet<Users> Users { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer("Data Source=; Database=BXDB; User Id=sa; Password=;");
        }
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Users>(entity =>
        {
            entity.HasKey(e => e.UserId).HasName("PK_User_UserId");

            entity.ToTable("Users", "sec");

            entity.Property(e => e.Birthdate).HasColumnType("date");

            entity.Property(e => e.FirstName)
                .HasMaxLength(50)
                .IsUnicode(false);

            entity.Property(e => e.LastName)
                .HasMaxLength(50)
                .IsUnicode(false);
        });

        OnModelCreatingPartial(modelBuilder);
    }

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}

Users.cs

public class Users
{
    public Users() { }

    public int UserId { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public DateTime Birthdate { get; set; }
}

IUsersRepository.cs

public interface IUsersRepository
{
    public IQueryable<Users> GetAllUsers();
}

UsersRepository.cs

public class UsersRepository : IUsersRepository
{
    public readonly SecurityContext _dbContext;

    public UsersRepository(SecurityContext dbContext)
    {
        _dbContext = dbContext;
    }

    public IQueryable<Users> GetAllUsers()
    {
        return _dbContext.Users;
    }
}

Upvotes: 0

Views: 101

Answers (1)

Xueli Chen
Xueli Chen

Reputation: 12695

'Some services are not able to be constructed' InvalidOperationException : Unable to resolve service for type 'BX_Security_AccessBase.Context.SecurityContext' while attempting to activate 'BX_Security_AccessBase.Repository.UsersRepository'.

From the error , you should register the DbContext as a service as follows:

public void ConfigureServices(IServiceCollection services)
{ 
        var connection = @"Server=(localdb)\mssqllocaldb;Database=BXDB;Trusted_Connection=True;ConnectRetryCount=0";
        services.AddDbContext<DLL.Models.SecurityContext>(options => options.UseSqlServer(connection, x => x.MigrationsAssembly("DLL")));

        services.AddControllers();

        services.AddScoped<IUsersRepository, UsersRepository>();
        services.AddScoped<IUserService, UserService>();
 }

Upvotes: 1

Related Questions