Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Repository and Dependency Injection Error

$
0
0

Hi,

I have this Repository and Dependency implementation to be injected to my controller but I'm getting this error...

"Unable to resolve service for type 'WebsiteName.ApplicationContext' while attempting to activate 'WebsiteName.Repository+EfRepository`1[Customers]'."

Here are the implementing classes/interface/repo/services/controller...

Repository

public class Repository
    {
        public class EfRepository<T> : IEfRepository<T> where T : DomainEntity
        {
            internal ApplicationContext appContext;
            internal DbSet<T> dbSet;

            public EfRepository(ApplicationContext appContext)
            {
                this.appContext = appContext;
                this.dbSet = (DbSet<T>)appContext.Set<T>();
            }

            public virtual T GetByID(object id)
            {
                return dbSet.Find(id);
            }
        }
    }

    public partial interface IEfRepository<T> where T : DomainEntity
    {
        T GetByID(object id);
    }

    public abstract class DomainEntity
    {
        public int Id { get; set; }
    }

Services

    public class CustomersServices : ICustomersServices
    {
        readonly IEfRepository<Customers> _customersServices;

        public CustomersServices(IEfRepository<Customers> customersServices)
        {
            _customersServices= customersServices;
        }

        public Customers GetById(int customerId)
        {
            return _customersServices.GetByID(customerId);
        }
    }

    public interface ICustomersServices
    {
        Customers GetById(int customerId);
    }

ApplicationContext

    public class ApplicationContext : DbContext
    {
        public ApplicationContext()
        {
        }

        public ApplicationContext(DbContextOptions<ApplicationContext> options)
            : base(options)
        {
        }

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

Startup.cs

        public void ConfigureServices(IServiceCollection services)
        {
	    // Add framework services.
	    services.AddDbContext<BloggingContext>(options =>
        	options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

            services.AddMvc();

            // Add application services.
            services.AddTransient<IEmailSender, AuthMessageSender>();
            services.AddTransient<ISmsSender, AuthMessageSender>();

            //register the repository and context
            services.AddTransient(typeof(IEfRepository<>), typeof(EfRepository<>));
            services.AddTransient(typeof(DbContext), typeof(ApplicationContext));

            //register the services
            services.AddScoped<ICustomersServices, CustomersServices>();
	    services.AddTransient<ICustomersServices, CustomersServices>();
        }

Controller

    public class IndexController : Controller
    {
        readonly ICustomersServices _customersServices;

        public IndexController(ICustomersServices customersServices)
        {
            _customersServices = customersServices;
        }

        public IActionResult Index()
        {
            return View(_customersServices.GetById(1));
        }
    }

 

Thanks,


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>