簡體   English   中英

所請求的服務尚未注冊

[英]The requested service has not been registered

我正在嘗試調用我的存儲庫,並且出現以下Autofac錯誤:

所請求的服務“ DOL.DTLLicense.DataAccess.Repositories.ApplicationTypeRepository”尚未注冊。 為避免此異常,請注冊一個組件以提供服務,使用IsRegistered()檢查服務注冊,或使用ResolveOptional()方法解決可選的依賴項。

我猜我的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:

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

服務層:

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

工作單位:

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

向您的UnitOfWork添加一個私有字段:

private readonly IComponentContext _componentContext;

將其注入到您的構造函數中:

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

然后從中解決:

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