我是AutoFac的新手,目前正在我的应用程序配置中使用自定义模块来启动一些核心F#系统。我正在使用的代码是
var builder = new ContainerBuilder();
builder.RegisterType<DefaultLogger>().As<IDefaultLogger>();
builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
builder.Build();
在我的应用程序配置中,我具有启动相关系统的适当逻辑。我想访问模块内的DefaultLogger。我可以使用Module基类的元数据以下选项:
protected virtual void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration);
protected virtual void AttachToRegistrationSource(IComponentRegistry componentRegistry, IRegistrationSource registrationSource);
public void Configure(IComponentRegistry componentRegistry);
protected virtual void Load(ContainerBuilder builder);
到目前为止,我仅使用过Load,并且在构建器上看不到任何允许我使用日志记录服务的方法。
答案非常简单。我刚刚将IComponentContext添加为模块实现的依赖项
public class LocalActorSystemModule : Module {
private IComponentContext m_ComponentContext; // A service for resolving dependencies required by this module
public LocalActorSystemModule(IComponentContext componentContext) {
m_ComponentContext = componentContext;
}
然后让AutoFac为我注入IComponentContext。这样,我可以解决模块内部所需的所有依赖项。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句