我是 AutoFac 的新手,目前正在我的应用程序配置中使用自定义模块来启动一些核心 F# 系统。我正在使用的代码是
var builder = new ContainerBuilder();
builder.RegisterType<DefaultLogger>().As<IDefaultLogger>();
builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
builder.Build();
在我的应用程序配置中,我有适当的逻辑来启动相关系统。我想访问我的模块内的 DefaultLogger。模块基类的元数据有以下可用选项:
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,在构建器上看不到任何允许我访问日志服务的方法。
当使用 autofac 而不是使用 RegisterType 方法在模块中注册某些内容时,您可以使用 Register 方法:
builder.Register(c =>
{
IComponentContext ctx = c.Resolve<IComponentContext();
IDefaultLogger logger = ctx.Resolve<IDefaultLogger>();
...do something with logger...
return ...return object you want to register...;
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)