简体   繁体   中英

The requested service has not been registered

I am trying to call my repository and I am getting the following Autofac error:

The requested service 'DOL.DTLLicense.DataAccess.Repositories.ApplicationTypeRepository' has not been registered. 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.

I am guessing there is something wrong in my IocConfig, but I'm not sure what I'm missing. I'm binding my repositories. Any help would be appreciated.

Here is my 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:

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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