• 00
  • :
  • 00
  • :
  • 00

.net core 抽离Controller 独立dll 加载方案

 

public static class DataServiceExtension
{
    /// <summary>
    /// 注入数据
    /// </summary>
    /// <param name="services"></param>
    public static IServiceCollection AddDataService(this IServiceCollection services)
    {
        #region 依赖注入 
        var controllers = GetControlllers();
        var mvcBuilder = services.AddMvc()
            .SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
        foreach (var assemby in controllers)
        {
            // 引入抽离的Controller类库
            mvcBuilder.AddApplicationPart(assemby);
        }
        #endregion

        return services;
    }

    private static System.Reflection.Assembly[] GetControlllers()
    {
        var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
        string assemblyPath = System.IO.Path.Combine(path, @"GZ.FrameworkAPI*.dll");
        Assembly[] referencedAssemblies = System.IO.Directory.GetFiles(path, "GZ.FrameworkAPI*.dll").Select(Assembly.LoadFrom).ToArray();
        return referencedAssemblies;
    }
}

 

Startup.cs中ConfigureServices方法:

services.AddDataService();

 

 

.

.

 

posted @ 2020-11-08 22:16  Garson_Zhang  阅读(355)  评论(0编辑  收藏  举报