None 个使用 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder 找到的构造函数

None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder

我正在使用 Autofac 容器开发 Azure functions。我在项目中实现了通用存储库模式以与后端交互。

我不确定我做错了什么,可能是在注册通用类型时出现问题。

如果我从我的项目中删除通用存储库部分,它工作正常。

我已经访问了堆栈溢出的所有链接,但没有找到任何合适的解决方案。

我在 运行 函数时遇到以下错误。

Executed 'sort' (Failed, Id=2cebaac1-a63e-4cf8-b82f-68e2ebeeb32d) [4/25/2019 12:28:18 PM] System.Private.CoreLib: Exception while executing function: sort. Microsoft.Azure.WebJobs.Host: Exception binding parameter '_chargeOptionsServcie'. Autofac: An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = ChargeOptionsService (ReflectionActivator), Services = [Awemedia.Chargestation.Business.Interfaces.IChargeOptionsServcie], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = OwnedByLifetimeScope ---> None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Awemedia.Chargestation.Business.Services.ChargeOptionsService' can be invoked with the available services and parameters:

这是我的函数代码:

[DependencyInjectionConfig(typeof(DIConfig))]
    public class Function1
    {
        [FunctionName("Function1")]
        public HttpResponseMessage Get(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequestMessage req, [Inject]IChargeOptionsServcie _chargeOptionsServcie, [Inject]IErrorHandler _errorHandler)
        {
            return req.CreateResponse(HttpStatusCode.OK, _chargeOptionsServcie.GetAll());
        }
    }

这是我的服务代码:

public class ChargeOptionsService : IChargeOptionsServcie
    {

        private readonly IBaseService<ChargeOptions> _baseService;
        private readonly IMapper _mapper;

        public ChargeOptionsService(IBaseService<ChargeOptions> baseService, IMapper mapper)
        {
            _baseService = baseService;
            _mapper = mapper;
        }


        public IEnumerable<ChargeOptionsResponse> GetAll()
        {
            return _baseService.GetAll().Select(t => _mapper.Map<ChargeOptions, ChargeOptionsResponse>(t));
        }

    }

这是我的 DIConfig.cs 代码,我在其中注册我的依赖项:

public class DIConfig
    {
        public DIConfig(string functionName)
        {
            DependencyInjection.Initialize(builder =>
            {

                builder.RegisterGeneric(typeof(BaseRepository<>)).As(typeof(IBaseRepository<>));
                builder.RegisterGeneric(typeof(BaseService<>)).As(typeof(IBaseService<>));

                builder.RegisterType<ErrorHandler>().As<IErrorHandler>();
                builder.RegisterType<ChargeOptionsService>().As<IChargeOptionsServcie>();
                builder.RegisterType<EventsService>().As<IEventsService>();
            }, functionName);
        }
    }

我的基础服务class:

public class BaseService<T> : IBaseService<T>
    {
        private readonly IBaseRepository<T> _repository;

        public BaseService(IBaseRepository<T> repository)
        {
            _repository = repository;
        }

        public IEnumerable<T> GetAll()
        {
            return _repository.GetAll();
        }

        public T GetById(int id)
        {
            return _repository.GetById(id);
        }

        public IEnumerable<T> Where(Expression<Func<T, bool>> exp)
        {
            return _repository.Where(exp);
        }

        public T AddOrUpdate(T entry, int Id)
        {
            var targetRecord = _repository.GetById(Id);
            var exists = targetRecord != null;

            if (exists)
            {
                _repository.Update(entry);
            }
            _repository.Insert(entry);
            return entry;
        }
        public T AddOrUpdate(T entry, Guid guid)
        {
            var targetRecord = _repository.GetById(guid);
            var exists = targetRecord != null;

            if (exists)
            {
                _repository.Update(entry);
            }
            _repository.Insert(entry);
            return entry;
        }
        public void Remove(int id)
        {
            var label = _repository.GetById(id);
            _repository.Delete(label);
        }

        public bool InsertBulk(IEnumerable<T> entities)
        {
            return _repository.InsertBulk(entities);
        }

        public T GetById(Guid guid)
        {
            return _repository.GetById(guid);
        }
    }

这是我的基本存储库:

public class BaseRepository<T> : IBaseRepository<T> where T : class
    {

        private readonly Context _context;

        private readonly DbSet<T> _entities;

        private readonly IErrorHandler _errorHandler;

        public BaseRepository(AwemediaContext context, IErrorHandler errorHandler)
        {
            _context = context;
            _entities = context.Set<T>();
            _errorHandler = errorHandler;
        }
        public IEnumerable<T> GetAll()
        {
            return _entities.ToList();
        }
        public T GetById(int id)
        {
            return _entities.Find(id);
        }

        public IEnumerable<T> Where(Expression<Func<T, bool>> exp)
        {
            return _entities.Where(exp);
        }
        public T Insert(T entity)
        {
            if (entity == null) throw new ArgumentNullException(string.Format(_errorHandler.GetMessage(ErrorMessagesEnum.EntityNull), "", "Input data is null"));
            _entities.AddAsync(entity);
            _context.SaveChanges();
            return entity;
        }
        public void Update(T entity)
        {
            if (entity == null) throw new ArgumentNullException(string.Format(_errorHandler.GetMessage(ErrorMessagesEnum.EntityNull), "", "Input data is null"));

            var oldEntity = _context.Find<T>(entity);
            _context.Entry(oldEntity).CurrentValues.SetValues(entity);
            _context.SaveChanges();
        }
        public void Delete(T entity)
        {
            if (entity == null) throw new ArgumentNullException(string.Format(_errorHandler.GetMessage(ErrorMessagesEnum.EntityNull), "", "Input data is null"));

            _entities.Remove(entity);
            _context.SaveChanges();
        }
        public bool InsertBulk(IEnumerable<T> entities)
        {
            bool result = false;
            if (entities.Count() > 0)
            {
                _entities.AddRange(entities);
                _context.SaveChanges();
                result = true;
            }
            return result;
        }
        public T GetById(Guid guid)
        {
            return _entities.Find(guid);
        }

    }

请帮忙 me.I 过去三天我的脑袋都碎了。

你试过这个吗:

// Register types that expose interfaces...
builder.RegisterType<BaseService<ChargeOptions>>.As(typeof(IBaseService<ChargeOptions>));

否则请更改您的 ChargeOptionsService 代码。给它一个默认的构造函数。看看是否可行,一个一个地重新添加构造函数参数,看看哪里出错了。

None of the constructors found with Autofac.Core.Activators.Reflection.DefaultConstructorFinder on type Awemedia.Chargestation.Business.Services.ChargeOptionsService can be invoked with the available services and parameters:

此错误消息表示 Autofac 无法创建 ChargeOptionsService,因为所需的依赖项之一尚未注册。如果您查看 ChargeOptionsService 构造函数,您可以看到有 2 个依赖项:

public ChargeOptionsService(IBaseService<ChargeOptions> baseService, IMapper mapper)
{
    _baseService = baseService;
    _mapper = mapper;
}
  • IBaseService<ChargeOptions> baseService。此依赖项由以下行注册

    builder.RegisterGeneric(typeof(BaseService<>)).As(typeof(IBaseService<>));
    
    • IMapper mapper。此依赖项未注册。

要修复您的错误,您应该注册一个 IMapper。像

builder.RegisterType<Mapper>().As<IMapper>();

can not resolve parameters AwemediaContext

这个错误信息几乎是一样的,它意味着你的一个依赖需要一个AwemediaContext没有注册。