错误无法解决类型为什么以及如何解决它的服务? [英] Error cannot resolve service of type why and how to solve it ?

查看:119
本文介绍了错误无法解决类型为什么以及如何解决它的服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

可能是您没有在 IRepositoryTab RepositoryTab

但你最需要的是指定接口而不是具体参数作为你的consturctor参数



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

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


这篇关于错误无法解决类型为什么以及如何解决它的服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆