Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
An unhandled exception occurred while processing the request.
<div class="titleerror">InvalidOperationException: Unable to resolve service for type 'TabDataAccess.Repositories.RepositoryTab`1[TabDataAccess.Dto.Employee]' while attempting to activate 'WebTabCore.Controllers.EmployeeController'.</div>
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)

<div id="stackpage" class="page">
InvalidOperationException: Unable to resolve service for type 'TabDataAccess.Repositories.RepositoryTab`1[TabDataAccess.Dto.Employee]' while attempting to activate 'WebTabCore.Controllers.EmployeeController'.
what i do as following
An unhandled exception occurred while processing the request.
InvalidOperationException: Unable to resolve service for type 'TabDataAccess.Repositories.RepositoryTab`1[TabDataAccess.Dto.Employee]' while attempting to activate 'WebTabCore.Controllers.EmployeeController'.
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)

InvalidOperationException: Unable to resolve service for type 'TabDataAccess.Repositories.RepositoryTab`1[TabDataAccess.Dto.Employee]' while attempting to activate 'WebTabCore.Controllers.EmployeeController'.
what i do as following what i do as below

What I have tried:

<pre>public class Employee
    {
       
        public int EmployeeId { get; set; }
        public int BranchCode { get; set; }
        public string EmployeeName { get; set; }
        public int EmployeeAge { get; set; }
     }
Repository
 public class RepositoryTab<T> : IrepositoryTab<T> where T : class
    {
        protected TabDbContext db { get; set; }
     
        private DbSet<T> dbSet;

        public RepositoryTab(TabDbContext Tabdb)
        {
            db = Tabdb;
            dbSet = db.Set<T>();
        }
        public IEnumerable<T> GetAll()
        {
            return dbSet.ToList();
        }
        }
 public interface IrepositoryTab<T> where T : class
        {
            IEnumerable<T> GetAll();
            
      
        }
configureservices
 services.AddDbContext<TabDbContext>(options =>
       options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
appsetting.json
  "ConnectionStrings": {
    "DefaultConnection": "Server=.\\SQL2014;Database=ErpWeb;uid=sa;pwd=abc123;ConnectRetryCount=0;" why this exceptioh happen
Posted
Updated 12-Feb-19 3:26am
Comments
Bryian Tan 16-Jan-19 23:59pm    
What in the constructor of the EmployeeController?
ahmed_sa 17-Jan-19 10:45am    
public class EmployeeController : Controller
{
private IrepositoryTab<employee> _repository = null;
public EmployeeController(RepositoryTab<employee> emp)
{
this._repository = emp;
}
public IActionResult Index()
{
var employees = _repository.GetAll();
return View(employees);
}
}

1 solution

It might be that you haven't registered you mapping fromIRepositoryTab to RepositoryTab
But most likely what you need is to specify interface instead of concrete parameter as your consturctor parameter

C#
public EmployeeController(IRepositoryTab<employee> emp)
{
    this._repository = emp;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900