From 864cc1456aaf878bbb50baa92ab1ca229c3146c0 Mon Sep 17 00:00:00 2001 From: geffzhang Date: Fri, 5 Oct 2018 20:52:02 +0800 Subject: [PATCH] fix : dynamic load assymbly --- BotSharp.Core/BotSharp.Core.csproj | 1 + BotSharp.Core/Modules/ModulesStartup.cs | 3 ++- ...tionExtensions.cs => PlatformAssembyLoader.cs} | 15 ++++----------- BotSharp.WebHost/BotSharp.WebHost.csproj | 3 +-- BotSharp.WebHost/Startup.cs | 3 ++- BotSharp.sln | 14 -------------- 6 files changed, 10 insertions(+), 29 deletions(-) rename BotSharp.Core/{PlatformServiceCollectionExtensions.cs => PlatformAssembyLoader.cs} (73%) diff --git a/BotSharp.Core/BotSharp.Core.csproj b/BotSharp.Core/BotSharp.Core.csproj index 425bee9b..bfed6e4f 100644 --- a/BotSharp.Core/BotSharp.Core.csproj +++ b/BotSharp.Core/BotSharp.Core.csproj @@ -82,6 +82,7 @@ If you feel that this project is helpful to you, please Star on the project, we + diff --git a/BotSharp.Core/Modules/ModulesStartup.cs b/BotSharp.Core/Modules/ModulesStartup.cs index c610be07..e1bea51d 100644 --- a/BotSharp.Core/Modules/ModulesStartup.cs +++ b/BotSharp.Core/Modules/ModulesStartup.cs @@ -4,6 +4,7 @@ using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Text; namespace BotSharp.Core.Modules @@ -30,7 +31,7 @@ namespace BotSharp.Core.Modules this._modules = options.Modules .Select(s => - { + { Type type = Type.GetType(s.Type); if (type == null) diff --git a/BotSharp.Core/PlatformServiceCollectionExtensions.cs b/BotSharp.Core/PlatformAssembyLoader.cs similarity index 73% rename from BotSharp.Core/PlatformServiceCollectionExtensions.cs rename to BotSharp.Core/PlatformAssembyLoader.cs index d2447ada..56576a6b 100644 --- a/BotSharp.Core/PlatformServiceCollectionExtensions.cs +++ b/BotSharp.Core/PlatformAssembyLoader.cs @@ -5,14 +5,15 @@ using System.Collections.Generic; using System.Drawing; using System.IO; using System.Reflection; +using System.Runtime.Loader; using System.Text; using Console = Colorful.Console; namespace Microsoft.Extensions.DependencyInjection { - public static class PlatformServiceCollectionExtensions + public static class PlatformAssembyLoader { - public static IServiceCollection AddPlatformEmulator(this IServiceCollection services, IConfiguration configuration, Action action) + public static void LoadPlatformEmulatorAssemblies(IConfiguration configuration, Action action) { var platform = configuration.GetValue("Platform"); var platformAssemblyName = configuration.GetValue("platformAssemblyName"); @@ -31,7 +32,7 @@ namespace Microsoft.Extensions.DependencyInjection var platformDllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{platformAssemblyName}.dll"); if (File.Exists(platformDllPath)) { - Assembly library = Assembly.LoadFile(platformDllPath); + Assembly library = AssemblyLoadContext.Default.LoadFromAssemblyPath(platformDllPath); action(library); Console.WriteLineFormatted("Loaded {0} platform emulator from {1} assembly which is using {2} engine.", Color.White, settings); } @@ -39,15 +40,7 @@ namespace Microsoft.Extensions.DependencyInjection { Console.WriteLine($"Can't load {platformAssemblyName} assembly."); } - Console.WriteLine(); - - /*Type myClass = (from type in library.GetExportedTypes() - where typeof(IMyInterface).IsAssignableFrom(type) - select type) - .Single();*/ - - return services; } } } diff --git a/BotSharp.WebHost/BotSharp.WebHost.csproj b/BotSharp.WebHost/BotSharp.WebHost.csproj index a707ef6a..2fb92b46 100644 --- a/BotSharp.WebHost/BotSharp.WebHost.csproj +++ b/BotSharp.WebHost/BotSharp.WebHost.csproj @@ -19,6 +19,7 @@ false DEBUG;TRACE;DIALOGFLOW;NETCOREAPP;NETCOREAPP2_1 + @@ -78,8 +79,6 @@ - - diff --git a/BotSharp.WebHost/Startup.cs b/BotSharp.WebHost/Startup.cs index e8b542b7..5e2d05fb 100644 --- a/BotSharp.WebHost/Startup.cs +++ b/BotSharp.WebHost/Startup.cs @@ -40,8 +40,9 @@ namespace BotSharp.WebHost options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); }); + PlatformAssembyLoader.LoadPlatformEmulatorAssemblies(Configuration, assembly => mvcBuilder.AddApplicationPart(assembly)); + this.modulesStartup.ConfigureServices(services); - //services.AddPlatformEmulator(Configuration, assembly => mvcBuilder.AddApplicationPart(assembly)); services.AddSwaggerGen(c => { diff --git a/BotSharp.sln b/BotSharp.sln index 82dc9a1d..c52c8104 100644 --- a/BotSharp.sln +++ b/BotSharp.sln @@ -22,8 +22,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Platform.Abstracti EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Platform.Models", "BotSharp.Platform.Models\BotSharp.Platform.Models.csproj", "{C4F2EAE5-F2C7-4F52-9DB2-7E76D7080C72}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Platform.Dialogflow", "..\botsharp-dialogflow\BotSharp.Platform.Dialogflow\BotSharp.Platform.Dialogflow.csproj", "{83D56CDD-7122-48D3-9CDC-BCE21CB10681}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Platform.Rasa", "..\botsharp-rasa\BotSharp.Platform.Rasa\BotSharp.Platform.Rasa.csproj", "{2902810C-F8F2-400E-88DB-72CBE393B175}" EndProject Global @@ -120,18 +118,6 @@ Global {C4F2EAE5-F2C7-4F52-9DB2-7E76D7080C72}.Release|Any CPU.Build.0 = Release|Any CPU {C4F2EAE5-F2C7-4F52-9DB2-7E76D7080C72}.Release|x64.ActiveCfg = Release|Any CPU {C4F2EAE5-F2C7-4F52-9DB2-7E76D7080C72}.Release|x64.Build.0 = Release|Any CPU - {83D56CDD-7122-48D3-9CDC-BCE21CB10681}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {83D56CDD-7122-48D3-9CDC-BCE21CB10681}.Debug|Any CPU.Build.0 = Debug|Any CPU - {83D56CDD-7122-48D3-9CDC-BCE21CB10681}.Debug|x64.ActiveCfg = Debug|Any CPU - {83D56CDD-7122-48D3-9CDC-BCE21CB10681}.Debug|x64.Build.0 = Debug|Any CPU - {83D56CDD-7122-48D3-9CDC-BCE21CB10681}.DIALOGFLOW|Any CPU.ActiveCfg = DIALOGFLOW|Any CPU - {83D56CDD-7122-48D3-9CDC-BCE21CB10681}.DIALOGFLOW|Any CPU.Build.0 = DIALOGFLOW|Any CPU - {83D56CDD-7122-48D3-9CDC-BCE21CB10681}.DIALOGFLOW|x64.ActiveCfg = DIALOGFLOW|Any CPU - {83D56CDD-7122-48D3-9CDC-BCE21CB10681}.DIALOGFLOW|x64.Build.0 = DIALOGFLOW|Any CPU - {83D56CDD-7122-48D3-9CDC-BCE21CB10681}.Release|Any CPU.ActiveCfg = Release|Any CPU - {83D56CDD-7122-48D3-9CDC-BCE21CB10681}.Release|Any CPU.Build.0 = Release|Any CPU - {83D56CDD-7122-48D3-9CDC-BCE21CB10681}.Release|x64.ActiveCfg = Release|Any CPU - {83D56CDD-7122-48D3-9CDC-BCE21CB10681}.Release|x64.Build.0 = Release|Any CPU {2902810C-F8F2-400E-88DB-72CBE393B175}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {2902810C-F8F2-400E-88DB-72CBE393B175}.Debug|Any CPU.Build.0 = Debug|Any CPU {2902810C-F8F2-400E-88DB-72CBE393B175}.Debug|x64.ActiveCfg = Debug|Any CPU