我的Umbraco项目中的一个课程取决于IContentService
。我尝试使用IOC容器向此类提供IContentService
。
以下是我如何向我的IOC(Autofac)注册IContentService
builder.RegisterType<Umbraco.Core.Services.ContentService>().As<IContentService>();
但是,Umbraco.Core.Services.ContentService
需要以下构造函数参数:
public ContentService(IDatabaseUnitOfWorkProvider provider, RepositoryFactory repositoryFactory, ILogger logger, IEventMessagesFactory eventMessagesFactory, IPublishingStrategy publishingStrategy, IDataTypeService dataTypeService, IUserService userService);
Autofac卡住了,因为它还不知道这些类型中的任何一种。这是一个堆栈跟踪:
Exception Details: Autofac.Core.DependencyResolutionException: None of the constructors found with
'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Umbraco.Core.Services.ContentService' can be
invoked with the available services and parameters:Cannot resolve parameter
'Umbraco.Core.Persistence.RepositoryFactory repositoryFactory' of constructor 'Void
.ctor(Umbraco.Core.Persistence.UnitOfWork.IDatabaseUnitOfWorkProvider, Umbraco.Core.Persistence.RepositoryFactory,
Umbraco.Core.Logging.ILogger, Umbraco.Core.Events.IEventMessagesFactory, Umbraco.Core.Publishing.IPublishingStrategy,
Umbraco.Core.Services.IDataTypeService, Umbraco.Core.Services.IUserService)'.
我应该在Autofac中注册哪些类型,以便可以实例化我的ContentService?
答案 0 :(得分:3)
这是我如何解决这个特殊问题的方法。感谢Claus的帮助。
我需要使用RegisterType<T>()
(docs here)代替public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
var builder = new ContainerBuilder();
// Register the components we need resolving with Autofac
builder.RegisterInstance(applicationContext.Services.MemberService).As<IMemberService>();
builder.RegisterInstance(applicationContext.Services.ContentService).As<IContentService>();
// ... Configuration for dependency resolution here ...
}
ContentService
原因是Umbraco创建了{{1}}的实例并正确配置它。需要告知Autofac使用此实例,而不是尝试创建自己的实例。