2022-03-15 18:00:53 +00:00
|
|
|
|
// (c) Copyright Ascensio System SIA 2010-2022
|
|
|
|
|
//
|
|
|
|
|
// This program is a free software product.
|
|
|
|
|
// You can redistribute it and/or modify it under the terms
|
|
|
|
|
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
|
|
|
|
|
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
|
|
|
|
|
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
|
|
|
|
|
// any third-party rights.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
|
|
|
|
|
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
|
|
|
|
|
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
|
|
|
|
|
//
|
|
|
|
|
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
|
|
|
|
|
//
|
|
|
|
|
// The interactive user interfaces in modified source and object code versions of the Program must
|
|
|
|
|
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
|
|
|
|
|
//
|
|
|
|
|
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
|
|
|
|
|
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
|
|
|
|
|
// trademark law for use of our trademarks.
|
|
|
|
|
//
|
|
|
|
|
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
|
|
|
|
|
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
|
|
|
|
|
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
|
|
|
|
|
|
|
|
|
|
namespace ASC.Common.DependencyInjection;
|
2022-02-03 15:26:44 +00:00
|
|
|
|
|
|
|
|
|
internal class AutofacComponent
|
2019-06-10 16:15:34 +00:00
|
|
|
|
{
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public string Type { get; set; }
|
|
|
|
|
public IEnumerable<AutofacService> Services { get; set; }
|
|
|
|
|
}
|
2022-02-03 14:18:58 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
internal class AutofacService
|
|
|
|
|
{
|
|
|
|
|
public string Type { get; set; }
|
|
|
|
|
}
|
2019-06-10 16:15:34 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public static class AutofacExtension
|
|
|
|
|
{
|
|
|
|
|
public static void Register(this ContainerBuilder builder, IConfiguration configuration,
|
|
|
|
|
bool loadproducts = true, bool loadconsumers = true, params string[] intern)
|
2019-06-10 16:15:34 +00:00
|
|
|
|
{
|
2022-02-03 15:26:44 +00:00
|
|
|
|
var modules = new List<(bool, string)>
|
2020-04-29 14:11:31 +00:00
|
|
|
|
{
|
2020-05-21 08:03:35 +00:00
|
|
|
|
(true, "autofac.json")
|
2020-04-29 14:11:31 +00:00
|
|
|
|
};
|
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
if (loadproducts)
|
2022-02-08 11:07:28 +00:00
|
|
|
|
{
|
2022-02-03 15:26:44 +00:00
|
|
|
|
modules.Add((true, "autofac.products.json"));
|
2022-02-08 11:07:28 +00:00
|
|
|
|
}
|
2020-05-15 11:02:05 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
if (loadconsumers)
|
2022-02-08 11:07:28 +00:00
|
|
|
|
{
|
2022-02-03 15:26:44 +00:00
|
|
|
|
modules.Add((true, "autofac.consumers.json"));
|
2022-02-08 11:07:28 +00:00
|
|
|
|
}
|
2020-05-21 08:03:35 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
if (intern != null)
|
2022-02-08 11:07:28 +00:00
|
|
|
|
{
|
2022-02-03 15:26:44 +00:00
|
|
|
|
modules.AddRange(intern.Select(r => (false, r)));
|
2022-02-08 11:07:28 +00:00
|
|
|
|
}
|
2019-12-20 11:17:01 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
foreach (var p in modules)
|
|
|
|
|
{
|
|
|
|
|
var config = new ConfigurationBuilder();
|
2022-02-01 14:51:29 +00:00
|
|
|
|
|
2022-02-08 11:07:28 +00:00
|
|
|
|
if (p.Item1)
|
|
|
|
|
{
|
|
|
|
|
config.SetBasePath(configuration["pathToConf"]);
|
|
|
|
|
}
|
2019-12-20 11:17:01 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
config.AddJsonFile(p.Item2);
|
2021-01-26 09:29:50 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
var root = config.Build();
|
|
|
|
|
var module = new ConfigurationModule(root);
|
|
|
|
|
builder.RegisterModule(module);
|
2021-01-26 09:29:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2019-12-20 11:17:01 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public static List<string> FindAndLoad(IConfiguration configuration, string currentDir, string section = "autofac.products.json")
|
|
|
|
|
{
|
|
|
|
|
var config = new ConfigurationBuilder();
|
|
|
|
|
config.SetBasePath(configuration["pathToConf"]);
|
|
|
|
|
config.AddJsonFile(section);
|
|
|
|
|
var root = config.Build();
|
2021-01-26 09:29:50 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
var sectionSettings = root.GetSection("components");
|
2022-02-08 11:07:28 +00:00
|
|
|
|
if (sectionSettings == null)
|
|
|
|
|
{
|
|
|
|
|
return new List<string>();
|
|
|
|
|
}
|
2021-01-26 09:29:50 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
var folder = configuration["core:products:folder"];
|
|
|
|
|
var subfolder = configuration["core:products:subfolder"];
|
|
|
|
|
string productsDir;
|
2022-02-01 14:51:29 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
if (!Path.IsPathRooted(folder))
|
|
|
|
|
{
|
2022-03-03 08:04:21 +00:00
|
|
|
|
if (currentDir.TrimEnd('\\').EndsWith(CrossPlatform.PathCombine(Path.GetFileName(folder), Assembly.GetEntryAssembly().GetName().Name, subfolder)))
|
2022-02-08 11:07:28 +00:00
|
|
|
|
{
|
2022-02-03 15:26:44 +00:00
|
|
|
|
productsDir = Path.GetFullPath(CrossPlatform.PathCombine("..", ".."));
|
2022-02-08 11:07:28 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
productsDir = Path.GetFullPath(CrossPlatform.PathCombine(currentDir, folder));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
productsDir = folder;
|
2022-02-03 15:26:44 +00:00
|
|
|
|
}
|
2022-02-01 14:51:29 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
var cs = new List<AutofacComponent>();
|
|
|
|
|
sectionSettings.Bind(cs);
|
2021-01-26 09:29:50 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
var types = new List<string>();
|
2019-06-10 16:15:34 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
foreach (var component in cs)
|
|
|
|
|
{
|
|
|
|
|
try
|
2019-06-10 16:15:34 +00:00
|
|
|
|
{
|
2022-02-03 15:26:44 +00:00
|
|
|
|
LoadAssembly(component.Type);
|
|
|
|
|
types.Add(component.Type);
|
2019-06-10 16:15:34 +00:00
|
|
|
|
}
|
2022-02-03 15:26:44 +00:00
|
|
|
|
catch (System.Exception)
|
|
|
|
|
{
|
|
|
|
|
//TODO
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-10 16:15:34 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
return types;
|
2021-01-26 09:29:50 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
void LoadAssembly(string type)
|
|
|
|
|
{
|
2022-03-03 08:04:21 +00:00
|
|
|
|
var dll = type.Substring(type.IndexOf(',') + 1).Trim();
|
2022-02-03 15:26:44 +00:00
|
|
|
|
var path = GetFullPath(dll);
|
2019-06-26 09:26:36 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(path))
|
2022-02-08 11:07:28 +00:00
|
|
|
|
{
|
2022-02-03 15:26:44 +00:00
|
|
|
|
AssemblyLoadContext.Default.Resolving += new Resolver(path).Resolving;
|
2022-02-08 11:07:28 +00:00
|
|
|
|
}
|
2022-02-03 15:26:44 +00:00
|
|
|
|
}
|
2019-07-08 12:15:24 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
string GetFullPath(string n)
|
|
|
|
|
{
|
|
|
|
|
var productPath = CrossPlatform.PathCombine(productsDir, n, subfolder);
|
2022-02-01 14:51:29 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
return GetPath(CrossPlatform.PathCombine(productPath, "bin"), n, SearchOption.AllDirectories)
|
|
|
|
|
?? GetPath(productPath, n, SearchOption.TopDirectoryOnly);
|
|
|
|
|
}
|
2020-02-11 10:47:04 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
static string GetPath(string dirPath, string dll, SearchOption searchOption)
|
|
|
|
|
{
|
2022-02-08 11:07:28 +00:00
|
|
|
|
if (!Directory.Exists(dirPath))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2019-07-08 12:15:24 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
return Directory.GetFiles(dirPath, $"{dll}.dll", searchOption).FirstOrDefault();
|
2019-06-10 16:15:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-03 15:26:44 +00:00
|
|
|
|
}
|
2020-07-16 09:08:20 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
class Resolver
|
|
|
|
|
{
|
|
|
|
|
private readonly string _resolvePath;
|
2020-07-16 09:08:20 +00:00
|
|
|
|
|
2022-02-08 11:07:28 +00:00
|
|
|
|
public Resolver(string assemblyPath)
|
|
|
|
|
{
|
|
|
|
|
_resolvePath = assemblyPath;
|
|
|
|
|
}
|
2020-07-16 09:08:20 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public Assembly Resolving(AssemblyLoadContext context, AssemblyName assemblyName)
|
|
|
|
|
{
|
|
|
|
|
var path = CrossPlatform.PathCombine(Path.GetDirectoryName(_resolvePath), $"{assemblyName.Name}.dll");
|
2020-07-16 09:08:20 +00:00
|
|
|
|
|
2022-02-08 11:07:28 +00:00
|
|
|
|
if (!File.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-07-16 09:08:20 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
return context.LoadFromAssemblyPath(path);
|
2020-07-16 09:08:20 +00:00
|
|
|
|
}
|
2022-02-03 14:18:58 +00:00
|
|
|
|
}
|