c# - 未使用“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”找到任何构造函数

标签 c# azure azure-functions autofac

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

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

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

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

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

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);
        }
    }

我的基本服务类别:

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);
        }

    }

请帮助我。过去三天我都快崩溃了。

enter image description here

最佳答案

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尚未注册。

关于c# - 未使用“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”找到任何构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55849625/

相关文章:

Azure Functions - 如何返回位置 header

c# - 计算选中复选框的数量

azure - 使用 Postman 中的 HttpRequest 将图像上传到 Azure Functions

Azure 共享网站模式和可用性

c# - 当文件上传到 azure 文件共享时,如何添加触发器以将文件从 azure 文件共享移动到 azure blob?

azure - 我应该改变微服务的通信方式吗?

c# - 弹出 - 阻止了带有来源的框架

c# - 位数组到字符串再返回位数组

javascript - 从服务器端调用 Javascript 代码是一种好习惯吗?

如果已登录到另一个 Microsoft 帐户,使用 WAAD 作为 IdP 的 Azure ACS 会出现 WS-Federation 协议(protocol)错误