c# - ASP.NET 样板插件模块或动态模块

标签 c# asp.net plugins module aspnetboilerplate

我是一名 .NET 开发人员,目前正在尝试学习 ASP.NET 样板。我遇到了PlugIn Modules我知道它可以用于模块依赖性,但我试图理解它们有这些行:

The AbpBootstrapper class defines the PlugInSources property which can be used to add sources to dynamically load plugin modules. A plugin source can be any class implementing the IPlugInSource interface. The PlugInFolderSource class implements it to get the plugin modules from assemblies located in a folder.

所以在尝试实现 IPlugInSource 接口(interface)之后:

using Abp.Modules;
using Abp.PlugIns;
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

public class EmpDetails : IPlugInSource
{
    public string EmpName { get; set; }
    public string Address { get; set; }
    public string Email { get; set; }

    public List<Assembly> GetAssemblies()
    {
        throw new NotImplementedException();
    }

    public List<Type> GetModules()
    {
        throw new NotImplementedException();
    }
}

我的疑问是:我必须在 GetAssemblies()GetModules() 方法中执行什么操作,如 AssembliesType 我必须返回吗?我已经引用了官方网站文档,但我找不到他们是否正确提供了示例。提前致谢。

最佳答案

您不需要实现 IPlugInSource

该文档提供了如何Startup 类中添加插件源的清晰示例:

services.AddAbp<MyStartupModule>(options =>
{
    options.PlugInSources.AddFolder(@"C:\MyPlugIns");
});

要消除您的疑问,请参阅 FolderPlugInSource 中的 GetAssembliesGetModules 方法:

public class FolderPlugInSource : IPlugInSource
{
    public string Folder { get; }

    public SearchOption SearchOption { get; set; }

    private readonly Lazy<List<Assembly>> _assemblies;

    public FolderPlugInSource(string folder, SearchOption searchOption = SearchOption.TopDirectoryOnly)
    {
        Folder = folder;
        SearchOption = searchOption;

        _assemblies = new Lazy<List<Assembly>>(LoadAssemblies, true);
    }

    public List<Assembly> GetAssemblies()
    {
        return _assemblies.Value;
    }

    public List<Type> GetModules()
    {
        var modules = new List<Type>();

        foreach (var assembly in GetAssemblies())
        {
            try
            {
                foreach (var type in assembly.GetTypes())
                {
                    if (AbpModule.IsAbpModule(type))
                    {
                        modules.AddIfNotContains(type);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new AbpInitializationException("Could not get module types from assembly: " + assembly.FullName, ex);
            }
        }

        return modules;
    }

    private List<Assembly> LoadAssemblies()
    {
        return AssemblyHelper.GetAllAssembliesInFolder(Folder, SearchOption);
    }
}

关于c# - ASP.NET 样板插件模块或动态模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49647249/

相关文章:

javascript - 为什么隐藏字段值不会更新

ASP.Net 5 配置依赖注入(inject)和静态 Controller 构造函数

java - 我的代码如何适应模组/插件?

java - NoSuchMethodError : com. google.common.cache.CacheBuilder.maximumSize(J)

c# - 如何在 Visual Studio Code 上将外部程序集 (.dll) 添加到 .NET Core 2.0

c# - 避免来自 ASP.NET 中的 View 的双重请求的最佳实践

c# - 如果 SQL Server 2005 中有任何列,如何获取最后一个 ID?

asp.net - 数据源不支持服务器端数据分页

android - 在不同的 Android 硬件/操作系统上获取设备信息时出错

c# - 确定文件中更改的字节的开始和结束范围