适用于某些类别类型的 Autofac 扫描组件

发布于 2025-01-02 10:46:42 字数 588 浏览 3 评论 0原文

我已经开始使用 Autofac 并希望扫描一些 DLL 并让 Autofac 注册其中的一些类。

我感兴趣的类都继承自 PluginBase 类,但下面的代码似乎没有注册它们。有人可以帮忙吗?

        var assemblies = AppDomain.CurrentDomain.GetAssemblies();


        var builder = new ContainerBuilder();
        builder.RegisterAssemblyTypes(assemblies)
            .Where(t => t.BaseType == typeof(PluginBase))
            .AsImplementedInterfaces()
            .AsSelf();

        var container = builder.Build();
        var pluginClasses = container.Resolve<IEnumerable<PluginBase>>();

        //pluginClasses is empty!!!!

I've started using Autofac and want to scan some DLL's and get Autofac to register some of the classes within them.

The classes that I'm interested in all inherit from a PluginBase class but the below code doesn't seem to be registering them. Can anyone help?

        var assemblies = AppDomain.CurrentDomain.GetAssemblies();


        var builder = new ContainerBuilder();
        builder.RegisterAssemblyTypes(assemblies)
            .Where(t => t.BaseType == typeof(PluginBase))
            .AsImplementedInterfaces()
            .AsSelf();

        var container = builder.Build();
        var pluginClasses = container.Resolve<IEnumerable<PluginBase>>();

        //pluginClasses is empty!!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深海蓝天 2025-01-09 10:46:42

我认为您需要在注册时指定插件的基类。调用 AsImplementedInterfaces 使用其实现的接口而不是其基类型来注册类型。您应该更新您的注册以将您的插件注册为 PluginBase。

这是代码:

var assemblies = AppDomain.CurrentDomain.GetAssemblies();


    var builder = new ContainerBuilder();
    builder.RegisterAssemblyTypes(assemblies)
        .Where(t => t.BaseType == typeof(PluginBase))
        .As<PluginBase>();

    var container = builder.Build();
    var pluginClasses = container.Resolve<IEnumerable<PluginBase>>();

I think you need to specify the base class of your Plugins on registration. The call AsImplementedInterfaces registers the type with its implemented interfaces and not by its base type. You should update your registration to register your plugins as PluginBase.

Here´s the code:

var assemblies = AppDomain.CurrentDomain.GetAssemblies();


    var builder = new ContainerBuilder();
    builder.RegisterAssemblyTypes(assemblies)
        .Where(t => t.BaseType == typeof(PluginBase))
        .As<PluginBase>();

    var container = builder.Build();
    var pluginClasses = container.Resolve<IEnumerable<PluginBase>>();
牵强ㄟ 2025-01-09 10:46:42

也许是这样的:

builder
    .RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
    .Where(t => t.GetInterfaces()
        .Any(i => i.IsAssignableFrom(typeof (IDependency))))
    .AsImplementedInterfaces()
    .InstancePerDependency();

在这段代码中,我使用 IDependency 作为标记接口。您可以将其替换为 PluginBase 类并删除 Where 方法。

重点是使用 IsAssignableFrom 方法。

Maybe do is this way:

builder
    .RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
    .Where(t => t.GetInterfaces()
        .Any(i => i.IsAssignableFrom(typeof (IDependency))))
    .AsImplementedInterfaces()
    .InstancePerDependency();

In this code I use IDependency as a marker interface. You may replace it with your PluginBase class and remove Where method.

The point is to use IsAssignableFrom method.

盗琴音 2025-01-09 10:46:42

请注意,由于与重新启动 IIS 相关的问题,您不应使用 AppDomain.CurrentDomain.GetAssemblies()

在 IIS 中托管应用程序时,所有程序集都会加载到
AppDomain 当应用程序第一次启动时,但是当 AppDomain 是
由 IIS 回收,然后仅按需加载程序集

要避免此问题,请使用 GetReferencedAssemblies()< 方法rel="nofollow noreferrer">System.Web.Compilation.BuildManager 来获取引用的程序集的列表:

var assemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>();

这将强制引用的程序集立即加载到 AppDomain 中可用于模块扫描。

有关更多详细信息,您可以在这里阅读: https://autofac.readthedocs .io/en/latest/faq/iis-restart.html

Pls, note that you shouldn't use AppDomain.CurrentDomain.GetAssemblies() due to an issue related to restarting IIS.

When hosting applications in IIS all assemblies are loaded into the
AppDomain when the application first starts, but when the AppDomain is
recycled by IIS the assemblies are then only loaded on demand
.

To avoid this issue use the GetReferencedAssemblies() method on System.Web.Compilation.BuildManager to get a list of the referenced assemblies instead:

var assemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>();

That will force the referenced assemblies to be loaded into the AppDomain immediately making them available for module scanning.

For more detail, you can read here: https://autofac.readthedocs.io/en/latest/faq/iis-restart.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文