最近我对 Autofac 有一些问题。
我有这样的聚合服务:
public interface ICommonServices
{
IQueryBus QueryBus { get; }
ICommandBus CommandBus { get; }
INotificationBus NotificationBus { get; }
ILoggerFactory LoggerFactory { get; }
}
我正在这样注册它:
public class AutofacModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterAggregateService<ICommonServices>();
}
}
当我这样做时:
var services = container.Resolve<ICommonServices>();
我收到此错误:
"The requested service 'Infrastructure.ICommonServices' has not been registered.
To avoid this exception, either register a component to provide the service, check for service
registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency."
我有很多程序集,我使用 Autofac 模块。如果我在模块中放置一个断点,我会看到它被调用并注册。此外,如果我检查 Container 注册,我会看到 ICommonServices 有一个注册:
Activator = Object (DelegateActivator), Services = [Infrastructure.ICommonServices],
Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = OwnedByLifetimeScope,
Pipeline = Autofac.Core.Pipeline.ResolvePipeline
如果我将注册从 AutofacModule 移到主程序集,jst 在 builder.Build() 之前,那么一切正常,Autofac 能够解决它。
如果服务已注册,为什么会出现错误?这不是唯一的一个。