简体   繁体   English

所请求的服务尚未注册

[英]The requested service has not been registered

I am trying to call my repository and I am getting the following Autofac error: 我正在尝试调用我的存储库,并且出现以下Autofac错误:

The requested service 'DOL.DTLLicense.DataAccess.Repositories.ApplicationTypeRepository' has not been registered. 所请求的服务“ DOL.DTLLicense.DataAccess.Repositories.ApplicationTypeRepository”尚未注册。 To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency. 为避免此异常,请注册一个组件以提供服务,使用IsRegistered()检查服务注册,或使用ResolveOptional()方法解决可选的依赖项。

I am guessing there is something wrong in my IocConfig, but I'm not sure what I'm missing. 我猜我的IocConfig中有问题,但是我不确定我缺少什么。 I'm binding my repositories. 我正在绑定我的存储库。 Any help would be appreciated. 任何帮助,将不胜感激。

Here is my IocConfig: 这是我的IocConfig:

public static void RegisterDependencies()
    {
        var builder = new ContainerBuilder();

        // Register MVC Controllers
        builder.RegisterControllers(typeof(MvcApplication).Assembly);

        // Bind all repositories to Ef repos (entity framework)
        builder.RegisterAssemblyTypes(typeof(DataAccess.Repositories.ApplicationRepository).Assembly)
            .Where(repo => repo.Name.EndsWith("Repository"))
            .WithParameter("connectionstring", Environment.MachineName)
            .AsImplementedInterfaces()
            .AsSelf();

        // Unit of Work
        builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerRequest();

        builder.RegisterAssemblyTypes(typeof(ApplicationBusiness).Assembly)
            .Where(b => b.Name.EndsWith("Business"))                
            .AsImplementedInterfaces()
            .InstancePerRequest();

        builder.RegisterType<ApplicationViewModelBuilder>().As<IApplicationViewModelBuilder>().InstancePerRequest();
        builder.RegisterType<ApplicationCommand>().As<IApplicationCommand>().InstancePerRequest();

        // Enable property injection into action filters
        builder.RegisterFilterProvider();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(builder.Build()));
    }

ViewModelBuilder: ViewModelBuilder:

public class ApplicationViewModelBuilder : IApplicationViewModelBuilder
{
    private readonly IApplicationBusiness _applicationBusiness;
    private readonly IApplicationTypeBusiness _applicationTypeBusiness;

    public ApplicationViewModelBuilder(IApplicationBusiness applicationBusiness, IApplicationTypeBusiness applicationTypeBusiness)
    {
        _applicationBusiness = applicationBusiness;
        _applicationTypeBusiness = applicationTypeBusiness;
    }

    public ApplicationApplyNowViewModel BuildApplyNow()
    {
        IEnumerable<SelectListItem> applicationTypes = SetApplicationTypesDropdown();
        var vm = new ApplicationApplyNowViewModel(applicationTypes);

        return vm;
    }
}

Service Layer: 服务层:

public class ApplicationTypeBusiness : BusinessBase, IApplicationTypeBusiness
{
    private readonly Logger _logger = LogManager.GetLogger(typeof(ApplicationTypeBusiness).FullName);

    private readonly IApplicationTypeRepository applicationTypeRepository;

    public ApplicationTypeBusiness(IUnitOfWork unitOfWork)
    {
        applicationTypeRepository = unitOfWork.GetRepository<ApplicationTypeRepository>();
    }

    public IEnumerable<ApplicationType> GetAll()
    {
        return applicationTypeRepository.GetAll();
    }

    public ApplicationType GetApplicationType(int applicationTypeId)
    {
        return applicationTypeRepository.GetApplicationType(applicationTypeId);
    }
}

UnitOfWork: 工作单位:

public class UnitOfWork : IUnitOfWork
{
    private readonly SqlContext _context;

    public UnitOfWork() : this(new SqlContext()) { }

    public UnitOfWork(SqlContext context)
    {
        _context = context;
    }

    public T GetRepository<T>() where T : class
    {
        var builder = new ContainerBuilder();
        builder.RegisterAssemblyModules(Assembly.GetExecutingAssembly());
        var container = builder.Build();

        using (var scope = container.BeginLifetimeScope())
        {
            var result = scope.Resolve<T>(new NamedParameter("context", _context));

            if (result != null && result.GetType().GetInterfaces().Contains(typeof(ICompanyRepository)))
                return result;
        }

        return null;   
    }

    public void Commit()
    {
        _context.SaveChanges();
    }

    public void Rollback()
    {
        _context
            .ChangeTracker
            .Entries()
            .ToList()
            .ForEach(x => x.Reload());
    }

    public void Dispose()
    {
        if (_context != null)
        {
            _context.Dispose();
        }
    }
}

Add a private field to your UnitOfWork : 向您的UnitOfWork添加一个私有字段:

private readonly IComponentContext _componentContext;

Inject it into your constructor: 将其注入到您的构造函数中:

public UnitOfWork(SqlContext context, IComponentContext componentContext)
{
    _context = context;
    _componentContext = componentContext;
}

Then resolve from that: 然后从中解决:

public T GetRepository<T>() where T : class
{
    var result = _componentContext.Resolve<T>(new NamedParameter("context", _context));
    if (result != null && result.GetType().GetInterfaces().Contains(typeof(ICompanyRepository)))
    {
        return result;
    }

    return null;   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM