我正在尝试调用我的存储库,但我收到以下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);
}
}
的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();
}
}
}
答案 0 :(得分:1)
在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;
}