最近我在使用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移至主程序集,则在builder.Build()之前的jst处完成所有工作,并且Autofac会解决该问题。
为什么注册服务后出现错误?这不是唯一的一个。
答案 0 :(得分:0)
问题是.net core 3.1中的程序集扫描。 旧的-不起作用的方式:
var assemblies= Assembly.GetExecutingAssembly()
.GetAllAssemblies()
.ToArray();
新功能-工作之一:
var l2Assemblies = Assembly.GetExecutingAssembly()
.GetAllAssembliesWithContext()
.ToArray();