2020-02-11 10:47:04 +00:00
|
|
|
|
using System.Collections.Generic;
|
2019-06-10 16:15:34 +00:00
|
|
|
|
using System.IO;
|
2019-06-26 09:26:36 +00:00
|
|
|
|
using System.Linq;
|
2019-06-10 16:15:34 +00:00
|
|
|
|
using System.Reflection;
|
2019-06-26 09:26:36 +00:00
|
|
|
|
using System.Runtime.Loader;
|
2019-12-20 11:17:01 +00:00
|
|
|
|
|
2019-06-10 16:15:34 +00:00
|
|
|
|
using Autofac;
|
|
|
|
|
using Autofac.Configuration;
|
2019-12-20 11:17:01 +00:00
|
|
|
|
using Autofac.Extensions.DependencyInjection;
|
|
|
|
|
|
2019-06-10 16:15:34 +00:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2019-10-31 11:28:30 +00:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
2019-06-10 16:15:34 +00:00
|
|
|
|
|
|
|
|
|
namespace ASC.Common.DependencyInjection
|
|
|
|
|
{
|
|
|
|
|
internal class AutofacComponent
|
|
|
|
|
{
|
|
|
|
|
public string Type { get; set; }
|
|
|
|
|
public IEnumerable<AutofacService> Services { get; set; }
|
|
|
|
|
}
|
|
|
|
|
internal class AutofacService
|
|
|
|
|
{
|
|
|
|
|
public string Type { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class AutofacExtension
|
|
|
|
|
{
|
2020-06-04 13:46:58 +00:00
|
|
|
|
public static IContainer AddAutofac(this IServiceCollection services, IConfiguration configuration, string currentDir, bool loadproducts = true, bool loadconsumers = true, params string[] intern)
|
2019-06-10 16:15:34 +00:00
|
|
|
|
{
|
2019-07-10 14:49:00 +00:00
|
|
|
|
var folder = configuration["core:products:folder"];
|
|
|
|
|
var subfolder = configuration["core:products:subfolder"];
|
|
|
|
|
string productsDir;
|
|
|
|
|
|
2020-06-04 11:01:25 +00:00
|
|
|
|
if (!Path.IsPathRooted(folder))
|
2019-07-10 14:49:00 +00:00
|
|
|
|
{
|
2020-06-04 11:01:25 +00:00
|
|
|
|
if (currentDir.EndsWith(Path.Combine(Path.GetFileName(folder), Assembly.GetCallingAssembly().GetName().Name, subfolder)))
|
|
|
|
|
{
|
|
|
|
|
productsDir = Path.GetFullPath(Path.Combine("..", ".."));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
productsDir = Path.GetFullPath(Path.Combine(currentDir, folder));
|
|
|
|
|
}
|
2019-07-10 14:49:00 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-06-04 11:01:25 +00:00
|
|
|
|
productsDir = folder;
|
2019-07-10 14:49:00 +00:00
|
|
|
|
}
|
2019-08-15 12:04:42 +00:00
|
|
|
|
|
2019-06-26 09:26:36 +00:00
|
|
|
|
var builder = new ContainerBuilder();
|
2020-04-29 14:11:31 +00:00
|
|
|
|
var modules = new List<(bool, string)>
|
|
|
|
|
{
|
2020-05-21 08:03:35 +00:00
|
|
|
|
(true, "autofac.json")
|
2020-04-29 14:11:31 +00:00
|
|
|
|
};
|
|
|
|
|
|
2020-05-15 11:02:05 +00:00
|
|
|
|
if (loadproducts)
|
|
|
|
|
{
|
|
|
|
|
modules.Add((true, "autofac.products.json"));
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-21 08:03:35 +00:00
|
|
|
|
if (loadconsumers)
|
|
|
|
|
{
|
|
|
|
|
modules.Add((true, "autofac.consumers.json"));
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 14:11:31 +00:00
|
|
|
|
if (intern != null)
|
|
|
|
|
{
|
|
|
|
|
modules.AddRange(intern.Select(r => (false, r)));
|
|
|
|
|
}
|
2019-12-20 11:17:01 +00:00
|
|
|
|
|
|
|
|
|
foreach (var p in modules)
|
|
|
|
|
{
|
2020-04-29 14:11:31 +00:00
|
|
|
|
var config = new ConfigurationBuilder();
|
|
|
|
|
if (p.Item1)
|
|
|
|
|
{
|
|
|
|
|
config.SetBasePath(configuration["pathToConf"]);
|
|
|
|
|
}
|
|
|
|
|
config.AddJsonFile(p.Item2);
|
2019-12-20 11:17:01 +00:00
|
|
|
|
|
|
|
|
|
var root = config.Build();
|
|
|
|
|
var module = new ConfigurationModule(root);
|
|
|
|
|
builder.RegisterModule(module);
|
|
|
|
|
|
2020-04-29 14:11:31 +00:00
|
|
|
|
if (p.Item2 == "autofac.products.json")
|
2019-12-20 11:17:01 +00:00
|
|
|
|
{
|
|
|
|
|
FindAndLoad(root.GetSection("components"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
builder.Populate(services);
|
|
|
|
|
|
|
|
|
|
var container = builder.Build();
|
2019-06-10 16:15:34 +00:00
|
|
|
|
|
2019-12-20 11:17:01 +00:00
|
|
|
|
services.TryAddSingleton(container);
|
|
|
|
|
|
|
|
|
|
return container;
|
2019-06-10 16:15:34 +00:00
|
|
|
|
|
2019-12-20 11:17:01 +00:00
|
|
|
|
void FindAndLoad(IConfigurationSection sectionSettings)
|
2019-06-10 16:15:34 +00:00
|
|
|
|
{
|
2019-12-20 11:17:01 +00:00
|
|
|
|
if (sectionSettings == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-06-26 09:26:36 +00:00
|
|
|
|
var cs = new List<AutofacComponent>();
|
|
|
|
|
sectionSettings.Bind(cs);
|
|
|
|
|
|
|
|
|
|
foreach (var component in cs)
|
2019-06-10 16:15:34 +00:00
|
|
|
|
{
|
2019-06-26 09:26:36 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var types = new List<string>();
|
|
|
|
|
LoadAssembly(component.Type);
|
2019-06-10 16:15:34 +00:00
|
|
|
|
|
2019-06-26 09:26:36 +00:00
|
|
|
|
foreach (var s in component.Services)
|
|
|
|
|
{
|
|
|
|
|
//LoadAssembly(s.Type);
|
|
|
|
|
types.Add(s.Type);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (System.Exception)
|
2019-06-10 16:15:34 +00:00
|
|
|
|
{
|
2019-06-26 09:26:36 +00:00
|
|
|
|
//TODO
|
2019-06-10 16:15:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LoadAssembly(string type)
|
|
|
|
|
{
|
2019-06-26 09:26:36 +00:00
|
|
|
|
var dll = type.Substring(type.IndexOf(",") + 1).Trim();
|
2020-02-11 10:47:04 +00:00
|
|
|
|
var path = GetFullPath(dll);
|
2019-06-26 09:26:36 +00:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(path))
|
|
|
|
|
{
|
2020-02-11 10:47:04 +00:00
|
|
|
|
AssemblyLoadContext.Default.Resolving += (c, n) =>
|
2019-06-26 09:26:36 +00:00
|
|
|
|
{
|
2020-02-11 10:47:04 +00:00
|
|
|
|
var path = GetFullPath(n.Name);
|
2020-06-04 13:04:17 +00:00
|
|
|
|
|
2020-02-19 08:37:52 +00:00
|
|
|
|
return path != null ?
|
|
|
|
|
c.LoadFromAssemblyPath(Path.Combine(Path.GetDirectoryName(path), $"{n.Name}.dll")) :
|
|
|
|
|
null;
|
2020-02-11 10:47:04 +00:00
|
|
|
|
};
|
2019-06-26 09:26:36 +00:00
|
|
|
|
}
|
2019-06-10 16:15:34 +00:00
|
|
|
|
}
|
2019-07-08 12:15:24 +00:00
|
|
|
|
|
2020-02-11 10:47:04 +00:00
|
|
|
|
string GetFullPath(string n)
|
|
|
|
|
{
|
|
|
|
|
var productPath = Path.Combine(productsDir, n, subfolder);
|
|
|
|
|
return GetPath(Path.Combine(productPath, "bin"), n, SearchOption.AllDirectories) ?? GetPath(productPath, n, SearchOption.TopDirectoryOnly);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-08 12:15:24 +00:00
|
|
|
|
string GetPath(string dirPath, string dll, SearchOption searchOption)
|
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(dirPath)) return null;
|
|
|
|
|
|
|
|
|
|
return Directory.GetFiles(dirPath, $"{dll}.dll", searchOption).FirstOrDefault();
|
|
|
|
|
}
|
2019-06-10 16:15:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|