DocSpace-client/common/ASC.Common/DependencyInjection/AutofacExtension.cs

173 lines
5.4 KiB
C#
Raw Normal View History

2020-02-11 10:47:04 +00:00
using System.Collections.Generic;
2019-06-10 16:15:34 +00:00
using System.IO;
using System.Linq;
2019-06-10 16:15:34 +00:00
using System.Reflection;
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-07-15 11:18:47 +00:00
if (currentDir.EndsWith(Path.Combine(Path.GetFileName(folder), Assembly.GetEntryAssembly().GetName().Name, subfolder)))
2020-06-04 11:01:25 +00:00
{
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
var builder = new ContainerBuilder();
2020-04-29 14:11:31 +00:00
var modules = new List<(bool, string)>
{
(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"));
}
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)
{
2020-09-30 11:50:39 +00:00
_ = config.SetBasePath(configuration["pathToConf"]);
2020-04-29 14:11:31 +00:00
}
2020-09-30 11:50:39 +00:00
_ = config.AddJsonFile(p.Item2);
2019-12-20 11:17:01 +00:00
var root = config.Build();
var module = new ConfigurationModule(root);
2020-09-30 11:50:39 +00:00
_ = builder.RegisterModule(module);
2019-12-20 11:17:01 +00:00
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;
}
var cs = new List<AutofacComponent>();
sectionSettings.Bind(cs);
foreach (var component in cs)
2019-06-10 16:15:34 +00:00
{
try
{
var types = new List<string>();
LoadAssembly(component.Type);
2019-06-10 16:15:34 +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
{
//TODO
2019-06-10 16:15:34 +00:00
}
}
}
void LoadAssembly(string type)
{
var dll = type.Substring(type.IndexOf(",") + 1).Trim();
2020-02-11 10:47:04 +00:00
var path = GetFullPath(dll);
if (!string.IsNullOrEmpty(path))
{
2020-07-16 09:08:20 +00:00
AssemblyLoadContext.Default.Resolving += new Resolver(path).Resolving;
}
2019-06-10 16:15:34 +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);
}
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
}
}
2020-07-16 09:08:20 +00:00
class Resolver
{
private string ResolvePath { get; set; }
public Resolver(string assemblyPath)
{
ResolvePath = assemblyPath;
}
public Assembly Resolving(AssemblyLoadContext context, AssemblyName assemblyName)
{
var path = Path.Combine(Path.GetDirectoryName(ResolvePath), $"{assemblyName.Name}.dll");
if (!File.Exists(path)) return null;
return context.LoadFromAssemblyPath(path);
}
}
2019-06-10 16:15:34 +00:00
}