解决Module类中的AutoFac依赖关系

解决Module类中的AutoFac依赖关系

本文介绍了解决Module类中的AutoFac依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是AutoFac的新手,目前正在我的应用程序配置中使用自定义模块来启动一些核心F#系统.我正在使用的代码是

I'm new to AutoFac and am currently using custom modules inside my app config to boot up some core F# systems. The code I'm using is

var builder = new ContainerBuilder();
builder.RegisterType<DefaultLogger>().As<IDefaultLogger>();
builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
builder.Build();

在我的应用程序配置中,我具有启动相关系统的适当逻辑.我想访问模块内的DefaultLogger. Module基类的元数据具有以下可用选项:

And inside my app config I have the appropriate logic to start up the relevant systems. I would like to have access to the DefaultLogger inside my Modules. Metadata for the Module base class has the following options available to me:

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,在构建器上看不到任何允许我使用日志记录服务的方法.

I've only been using Load so far and I can't see any methods on the builder that would allow me to get at the logging service.

推荐答案

答案非常简单.我刚刚将IComponentContext添加为模块实现的依赖项

The answer turned out to be incredibly simple. I just added IComponentContext as a dependency to my Module's implementation

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.这样,我可以解决模块内部所需的所有依赖项.

And let AutoFac inject the IComponentContext for me. That way I can resolve any dependencies I require inside the module.

这篇关于解决Module类中的AutoFac依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:14