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;
|
2022-02-03 15:26:44 +00:00
|
|
|
|
|
|
|
|
|
public enum DIAttributeEnum
|
2020-02-17 08:58:14 +00:00
|
|
|
|
{
|
2022-02-03 15:26:44 +00:00
|
|
|
|
Singletone,
|
|
|
|
|
Scope,
|
|
|
|
|
Transient
|
|
|
|
|
}
|
2020-10-29 09:31:55 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public class TransientAttribute : DIAttribute
|
|
|
|
|
{
|
|
|
|
|
public override DIAttributeEnum DIAttributeEnum => DIAttributeEnum.Transient;
|
2020-10-18 16:03:14 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public TransientAttribute() { }
|
2020-10-19 15:53:15 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public TransientAttribute(Type service) : base(service) { }
|
2020-10-29 09:31:55 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public TransientAttribute(Type service, Type implementation) : base(service, implementation) { }
|
2020-10-19 15:53:15 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public override void TryAdd(IServiceCollection services, Type service, Type implementation = null)
|
|
|
|
|
{
|
2022-02-08 11:07:28 +00:00
|
|
|
|
if (implementation != null)
|
|
|
|
|
{
|
|
|
|
|
services.AddTransient(service, implementation);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
services.AddTransient(service);
|
|
|
|
|
}
|
2020-10-19 15:53:15 +00:00
|
|
|
|
}
|
2022-02-03 15:26:44 +00:00
|
|
|
|
}
|
2020-10-19 15:53:15 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public class ScopeAttribute : DIAttribute
|
|
|
|
|
{
|
|
|
|
|
public override DIAttributeEnum DIAttributeEnum => DIAttributeEnum.Scope;
|
2020-10-18 16:03:14 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public ScopeAttribute() { }
|
2020-10-18 16:03:14 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public ScopeAttribute(Type service) : base(service) { }
|
2020-10-29 09:31:55 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public ScopeAttribute(Type service, Type implementation) : base(service, implementation) { }
|
2020-10-29 09:31:55 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public override void TryAdd(IServiceCollection services, Type service, Type implementation = null)
|
|
|
|
|
{
|
2022-02-08 11:07:28 +00:00
|
|
|
|
if (implementation != null)
|
|
|
|
|
{
|
|
|
|
|
services.AddScoped(service, implementation);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
services.AddScoped(service);
|
|
|
|
|
}
|
2020-10-18 16:03:14 +00:00
|
|
|
|
}
|
2022-02-03 15:26:44 +00:00
|
|
|
|
}
|
2020-10-18 16:03:14 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public class SingletoneAttribute : DIAttribute
|
|
|
|
|
{
|
|
|
|
|
public override DIAttributeEnum DIAttributeEnum => DIAttributeEnum.Singletone;
|
2020-10-18 16:03:14 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public SingletoneAttribute() { }
|
2020-10-29 09:31:55 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public SingletoneAttribute(Type service) : base(service) { }
|
2020-10-29 09:31:55 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public SingletoneAttribute(Type service, Type implementation) : base(service, implementation) { }
|
2020-10-18 16:03:14 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public override void TryAdd(IServiceCollection services, Type service, Type implementation = null)
|
|
|
|
|
{
|
2022-02-08 11:07:28 +00:00
|
|
|
|
if (implementation != null)
|
|
|
|
|
{
|
|
|
|
|
services.AddSingleton(service, implementation);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
services.AddSingleton(service);
|
|
|
|
|
}
|
2020-10-18 16:03:14 +00:00
|
|
|
|
}
|
2022-02-03 15:26:44 +00:00
|
|
|
|
}
|
2020-10-18 16:03:14 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public abstract class DIAttribute : Attribute
|
|
|
|
|
{
|
|
|
|
|
public abstract DIAttributeEnum DIAttributeEnum { get; }
|
2022-04-15 09:08:06 +00:00
|
|
|
|
protected internal Type Implementation { get; }
|
|
|
|
|
protected internal Type Service { get; }
|
|
|
|
|
public Type Additional { get; init; }
|
2020-10-18 16:03:14 +00:00
|
|
|
|
|
2022-03-03 08:04:21 +00:00
|
|
|
|
protected DIAttribute() { }
|
2020-10-18 16:03:14 +00:00
|
|
|
|
|
2022-03-03 08:04:21 +00:00
|
|
|
|
protected DIAttribute(Type service)
|
2022-02-08 11:07:28 +00:00
|
|
|
|
{
|
|
|
|
|
Service = service;
|
|
|
|
|
}
|
2020-10-29 09:31:55 +00:00
|
|
|
|
|
2022-03-03 08:04:21 +00:00
|
|
|
|
protected DIAttribute(Type service, Type implementation)
|
2022-02-03 15:26:44 +00:00
|
|
|
|
{
|
|
|
|
|
Implementation = implementation;
|
|
|
|
|
Service = service;
|
2020-10-18 16:03:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public abstract void TryAdd(IServiceCollection services, Type service, Type implementation = null);
|
|
|
|
|
}
|
2020-02-17 08:58:14 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public class DIHelper
|
|
|
|
|
{
|
|
|
|
|
public Dictionary<DIAttributeEnum, List<string>> Services { get; set; }
|
|
|
|
|
public List<string> Added { get; set; }
|
|
|
|
|
public List<string> Configured { get; set; }
|
|
|
|
|
public IServiceCollection ServiceCollection { get; private set; }
|
|
|
|
|
|
|
|
|
|
public DIHelper()
|
|
|
|
|
{
|
|
|
|
|
Services = new Dictionary<DIAttributeEnum, List<string>>()
|
2020-10-29 09:31:55 +00:00
|
|
|
|
{
|
|
|
|
|
{ DIAttributeEnum.Singletone, new List<string>() },
|
|
|
|
|
{ DIAttributeEnum.Scope, new List<string>() },
|
|
|
|
|
{ DIAttributeEnum.Transient, new List<string>() }
|
|
|
|
|
};
|
2022-02-01 14:51:29 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
Added = new List<string>();
|
|
|
|
|
Configured = new List<string>();
|
|
|
|
|
}
|
2020-10-18 16:34:30 +00:00
|
|
|
|
|
2022-02-08 11:07:28 +00:00
|
|
|
|
public DIHelper(IServiceCollection serviceCollection) : this()
|
|
|
|
|
{
|
|
|
|
|
ServiceCollection = serviceCollection;
|
|
|
|
|
}
|
2020-02-17 08:58:14 +00:00
|
|
|
|
|
2022-02-08 11:07:28 +00:00
|
|
|
|
public void Configure(IServiceCollection serviceCollection)
|
|
|
|
|
{
|
|
|
|
|
ServiceCollection = serviceCollection;
|
|
|
|
|
}
|
2020-02-17 08:58:14 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public void RegisterProducts(IConfiguration configuration, string path)
|
|
|
|
|
{
|
|
|
|
|
var types = AutofacExtension.FindAndLoad(configuration, path);
|
2021-01-26 09:29:50 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
foreach (var t in types.Select(Type.GetType).Where(r => r != null))
|
2022-02-08 11:07:28 +00:00
|
|
|
|
{
|
2022-02-03 15:26:44 +00:00
|
|
|
|
TryAdd(t);
|
2022-02-08 11:07:28 +00:00
|
|
|
|
}
|
2022-02-03 15:26:44 +00:00
|
|
|
|
}
|
2020-02-17 08:58:14 +00:00
|
|
|
|
|
2022-03-03 08:04:21 +00:00
|
|
|
|
public void AddControllers()
|
|
|
|
|
{
|
2022-03-14 10:57:59 +00:00
|
|
|
|
foreach (var a in Assembly.GetEntryAssembly().GetTypes().Where(r => r.IsAssignableTo<ControllerBase>() && !r.IsAbstract))
|
2022-03-03 08:04:21 +00:00
|
|
|
|
{
|
|
|
|
|
_ = TryAdd(a);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-08 11:07:28 +00:00
|
|
|
|
public bool TryAdd<TService>() where TService : class
|
|
|
|
|
{
|
|
|
|
|
return TryAdd(typeof(TService));
|
|
|
|
|
}
|
2020-10-18 16:03:14 +00:00
|
|
|
|
|
2022-02-08 11:07:28 +00:00
|
|
|
|
public bool TryAdd<TService, TImplementation>() where TService : class
|
|
|
|
|
{
|
|
|
|
|
return TryAdd(typeof(TService), typeof(TImplementation));
|
|
|
|
|
}
|
2020-02-17 08:58:14 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public bool TryAdd(Type service, Type implementation = null)
|
|
|
|
|
{
|
|
|
|
|
if (service.IsInterface && service.IsGenericType && implementation == null &&
|
|
|
|
|
(service.GetGenericTypeDefinition() == typeof(IOptionsSnapshot<>) ||
|
|
|
|
|
service.GetGenericTypeDefinition() == typeof(IOptions<>) ||
|
|
|
|
|
service.GetGenericTypeDefinition() == typeof(IOptionsMonitor<>)
|
|
|
|
|
))
|
2020-10-18 16:03:14 +00:00
|
|
|
|
{
|
2022-02-03 15:26:44 +00:00
|
|
|
|
service = service.GetGenericArguments().FirstOrDefault();
|
2022-02-01 14:51:29 +00:00
|
|
|
|
|
2022-02-08 11:07:28 +00:00
|
|
|
|
if (service == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-02-03 15:26:44 +00:00
|
|
|
|
}
|
2020-10-22 17:57:18 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
var serviceName = $"{service}{implementation}";
|
2022-02-01 14:51:29 +00:00
|
|
|
|
|
2022-02-08 11:07:28 +00:00
|
|
|
|
if (Added.Contains(serviceName))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-02-01 14:51:29 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
Added.Add(serviceName);
|
2020-02-17 08:58:14 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
var di = service.IsGenericType && (
|
|
|
|
|
service.GetGenericTypeDefinition() == typeof(IConfigureOptions<>) ||
|
|
|
|
|
service.GetGenericTypeDefinition() == typeof(IPostConfigureOptions<>) ||
|
|
|
|
|
service.GetGenericTypeDefinition() == typeof(IOptionsMonitor<>)
|
|
|
|
|
) && implementation != null ? implementation.GetCustomAttribute<DIAttribute>() : service.GetCustomAttribute<DIAttribute>();
|
2022-02-01 14:51:29 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
var isnew = false;
|
2020-10-18 16:03:14 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
if (di != null)
|
|
|
|
|
{
|
|
|
|
|
if (di.Additional != null)
|
2020-02-17 08:58:14 +00:00
|
|
|
|
{
|
2022-02-03 15:26:44 +00:00
|
|
|
|
var m = di.Additional.GetMethod("Register", BindingFlags.Public | BindingFlags.Static);
|
|
|
|
|
m.Invoke(null, new[] { this });
|
|
|
|
|
}
|
2020-02-17 08:58:14 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
if (!service.IsInterface || implementation != null)
|
|
|
|
|
{
|
|
|
|
|
isnew = implementation != null ? Register(service, implementation) : Register(service);
|
2022-02-08 11:07:28 +00:00
|
|
|
|
if (!isnew)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-02-03 15:26:44 +00:00
|
|
|
|
}
|
2020-02-17 08:58:14 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
if (service.IsInterface && implementation == null || !service.IsInterface)
|
|
|
|
|
{
|
|
|
|
|
if (di.Service != null)
|
2020-10-19 07:19:32 +00:00
|
|
|
|
{
|
2022-02-03 15:26:44 +00:00
|
|
|
|
var a = di.Service.GetInterfaces().FirstOrDefault(x => x.IsGenericType && (
|
|
|
|
|
x.GetGenericTypeDefinition() == typeof(IConfigureOptions<>) ||
|
|
|
|
|
x.GetGenericTypeDefinition() == typeof(IPostConfigureOptions<>) ||
|
|
|
|
|
x.GetGenericTypeDefinition() == typeof(IOptionsMonitor<>)
|
|
|
|
|
));
|
2022-02-01 14:51:29 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
if (a != null)
|
|
|
|
|
{
|
|
|
|
|
if (!a.ContainsGenericParameters)
|
2020-10-19 07:19:32 +00:00
|
|
|
|
{
|
2022-02-03 15:26:44 +00:00
|
|
|
|
var b = a.GetGenericArguments();
|
2020-02-17 08:58:14 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
foreach (var g in b)
|
2020-10-20 13:24:46 +00:00
|
|
|
|
{
|
2022-02-03 15:26:44 +00:00
|
|
|
|
if (g != service)
|
2021-10-28 13:07:14 +00:00
|
|
|
|
{
|
2022-02-03 15:26:44 +00:00
|
|
|
|
TryAdd(g);
|
2020-10-21 11:12:08 +00:00
|
|
|
|
|
2022-02-08 11:07:28 +00:00
|
|
|
|
if (service.IsInterface && di.Implementation == null)
|
|
|
|
|
{
|
|
|
|
|
TryAdd(service, g);
|
|
|
|
|
}
|
2021-10-28 13:07:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-03 15:26:44 +00:00
|
|
|
|
|
|
|
|
|
TryAdd(a, di.Service);
|
2020-10-19 07:19:32 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2020-10-18 16:03:14 +00:00
|
|
|
|
{
|
2022-03-03 08:04:21 +00:00
|
|
|
|
Type c;
|
2022-02-03 15:26:44 +00:00
|
|
|
|
var a1 = a.GetGenericTypeDefinition();
|
|
|
|
|
var b = a.GetGenericArguments().FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (b != null && b.IsGenericType)
|
2020-10-29 14:30:08 +00:00
|
|
|
|
{
|
2022-02-03 15:26:44 +00:00
|
|
|
|
var b1 = b.GetGenericTypeDefinition().MakeGenericType(service.GetGenericArguments());
|
|
|
|
|
|
|
|
|
|
TryAdd(b1);
|
|
|
|
|
c = a1.MakeGenericType(b1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
c = a1.MakeGenericType(service.GetGenericArguments());
|
2020-10-29 14:30:08 +00:00
|
|
|
|
}
|
2022-02-03 15:26:44 +00:00
|
|
|
|
|
|
|
|
|
TryAdd(c, di.Service.MakeGenericType(service.GetGenericArguments()));
|
|
|
|
|
//a, di.Service
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (di.Implementation == null)
|
|
|
|
|
{
|
|
|
|
|
isnew = Register(service, di.Service);
|
|
|
|
|
TryAdd(di.Service);
|
2020-10-18 16:03:14 +00:00
|
|
|
|
}
|
2022-02-08 11:07:28 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Register(di.Service);
|
|
|
|
|
}
|
2020-10-18 16:03:14 +00:00
|
|
|
|
}
|
2022-02-03 15:26:44 +00:00
|
|
|
|
}
|
2020-10-18 16:03:14 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
if (di.Implementation != null)
|
|
|
|
|
{
|
|
|
|
|
var a = di.Implementation.GetInterfaces().FirstOrDefault(x => x.IsGenericType &&
|
|
|
|
|
(x.GetGenericTypeDefinition() == typeof(IConfigureOptions<>) ||
|
|
|
|
|
x.GetGenericTypeDefinition() == typeof(IPostConfigureOptions<>) ||
|
|
|
|
|
x.GetGenericTypeDefinition() == typeof(IOptionsMonitor<>))
|
|
|
|
|
);
|
|
|
|
|
if (a != null)
|
2021-10-28 13:07:14 +00:00
|
|
|
|
{
|
2022-02-03 15:26:44 +00:00
|
|
|
|
if (!a.ContainsGenericParameters)
|
2021-10-28 13:07:14 +00:00
|
|
|
|
{
|
2022-02-03 15:26:44 +00:00
|
|
|
|
var b = a.GetGenericArguments();
|
2020-10-20 13:24:46 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
foreach (var g in b)
|
|
|
|
|
{
|
|
|
|
|
if (g != service)
|
2020-10-20 13:24:46 +00:00
|
|
|
|
{
|
2022-02-03 15:26:44 +00:00
|
|
|
|
//TryAdd(g);
|
2022-02-08 11:07:28 +00:00
|
|
|
|
if (service.IsInterface && implementation == null)
|
|
|
|
|
{
|
|
|
|
|
TryAdd(service, g);
|
|
|
|
|
}
|
2020-10-20 13:24:46 +00:00
|
|
|
|
}
|
2021-10-28 13:07:14 +00:00
|
|
|
|
}
|
2020-02-17 08:58:14 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
TryAdd(a, di.Implementation);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-03-03 08:04:21 +00:00
|
|
|
|
Type c;
|
2022-02-03 15:26:44 +00:00
|
|
|
|
var a1 = a.GetGenericTypeDefinition();
|
|
|
|
|
var b = a.GetGenericArguments().FirstOrDefault();
|
2020-10-21 13:39:06 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
if (b != null && b.IsGenericType)
|
|
|
|
|
{
|
|
|
|
|
var b1 = b.GetGenericTypeDefinition().MakeGenericType(service.GetGenericArguments());
|
2020-02-17 08:58:14 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
TryAdd(b1);
|
|
|
|
|
c = a1.MakeGenericType(b1);
|
2021-10-28 13:07:14 +00:00
|
|
|
|
}
|
2022-02-08 11:07:28 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
c = a1.MakeGenericType(service.GetGenericArguments());
|
|
|
|
|
}
|
2022-02-01 14:51:29 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
TryAdd(c, di.Implementation.MakeGenericType(service.GetGenericArguments()));
|
|
|
|
|
//a, di.Service
|
|
|
|
|
}
|
2020-10-18 16:03:14 +00:00
|
|
|
|
}
|
2022-02-03 15:26:44 +00:00
|
|
|
|
|
2022-02-08 11:07:28 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
isnew = TryAdd(service, di.Implementation);
|
|
|
|
|
}
|
2020-10-20 13:24:46 +00:00
|
|
|
|
}
|
2020-10-18 16:03:14 +00:00
|
|
|
|
}
|
2022-02-03 15:26:44 +00:00
|
|
|
|
}
|
2020-02-17 08:58:14 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
if (isnew)
|
|
|
|
|
{
|
|
|
|
|
ConstructorInfo[] props = null;
|
2020-10-18 16:03:14 +00:00
|
|
|
|
|
2022-02-08 11:07:28 +00:00
|
|
|
|
if (!service.IsInterface)
|
|
|
|
|
{
|
|
|
|
|
props = service.GetConstructors();
|
|
|
|
|
}
|
|
|
|
|
else if (implementation != null)
|
|
|
|
|
{
|
|
|
|
|
props = implementation.GetConstructors();
|
|
|
|
|
}
|
|
|
|
|
else if (di.Service != null)
|
|
|
|
|
{
|
|
|
|
|
props = di.Service.GetConstructors();
|
|
|
|
|
}
|
2020-02-17 08:58:14 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
if (props != null)
|
|
|
|
|
{
|
|
|
|
|
var par = props.SelectMany(r => r.GetParameters()).Distinct();
|
2022-02-01 14:51:29 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
foreach (var p1 in par)
|
2022-02-08 11:07:28 +00:00
|
|
|
|
{
|
2022-02-03 15:26:44 +00:00
|
|
|
|
TryAdd(p1.ParameterType);
|
2022-02-08 11:07:28 +00:00
|
|
|
|
}
|
2020-10-18 16:03:14 +00:00
|
|
|
|
}
|
2020-02-17 08:58:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
return isnew;
|
|
|
|
|
}
|
2022-02-01 14:51:29 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public DIHelper TryAddSingleton<TService>(Func<IServiceProvider, TService> implementationFactory) where TService : class
|
|
|
|
|
{
|
|
|
|
|
var serviceName = $"{typeof(TService)}";
|
2020-02-17 08:58:14 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
if (!Services[DIAttributeEnum.Singletone].Contains(serviceName))
|
|
|
|
|
{
|
|
|
|
|
Services[DIAttributeEnum.Singletone].Add(serviceName);
|
|
|
|
|
ServiceCollection.TryAddSingleton(implementationFactory);
|
2020-02-17 08:58:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
return this;
|
|
|
|
|
}
|
2022-02-01 14:51:29 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public DIHelper TryAddSingleton<TService, TImplementation>() where TService : class where TImplementation : class, TService
|
|
|
|
|
{
|
|
|
|
|
var serviceName = $"{typeof(TService)}{typeof(TImplementation)}";
|
2020-02-17 08:58:14 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
if (!Services[DIAttributeEnum.Singletone].Contains(serviceName))
|
|
|
|
|
{
|
|
|
|
|
Services[DIAttributeEnum.Singletone].Add(serviceName);
|
|
|
|
|
ServiceCollection.TryAddSingleton<TService, TImplementation>();
|
2020-02-17 08:58:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
return this;
|
|
|
|
|
}
|
2022-02-01 14:51:29 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public DIHelper Configure<TOptions>(Action<TOptions> configureOptions) where TOptions : class
|
|
|
|
|
{
|
|
|
|
|
var serviceName = $"{typeof(TOptions)}";
|
2020-02-17 12:13:40 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
if (!Configured.Contains(serviceName))
|
|
|
|
|
{
|
|
|
|
|
Configured.Add(serviceName);
|
|
|
|
|
ServiceCollection.Configure(configureOptions);
|
2020-02-17 08:58:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
return this;
|
|
|
|
|
}
|
2022-02-01 14:51:29 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public DIHelper Configure<TOptions>(string name, Action<TOptions> configureOptions) where TOptions : class
|
|
|
|
|
{
|
|
|
|
|
var serviceName = $"{typeof(TOptions)}{name}";
|
2020-02-17 12:13:40 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
if (!Configured.Contains(serviceName))
|
|
|
|
|
{
|
|
|
|
|
Configured.Add(serviceName);
|
|
|
|
|
ServiceCollection.Configure(name, configureOptions);
|
2020-02-17 08:58:14 +00:00
|
|
|
|
}
|
2022-02-01 14:51:29 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
return this;
|
|
|
|
|
}
|
2022-02-01 14:51:29 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
private bool Register(Type service, Type implementation = null)
|
|
|
|
|
{
|
|
|
|
|
if (service.IsSubclassOf(typeof(ControllerBase)) || service.GetInterfaces().Contains(typeof(IResourceFilter))
|
|
|
|
|
|| service.GetInterfaces().Contains(typeof(IDictionary<string, string>)))
|
2022-02-08 11:07:28 +00:00
|
|
|
|
{
|
2022-02-03 15:26:44 +00:00
|
|
|
|
return true;
|
2022-02-08 11:07:28 +00:00
|
|
|
|
}
|
2022-02-01 14:51:29 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
var c = service.IsGenericType && (
|
|
|
|
|
service.GetGenericTypeDefinition() == typeof(IConfigureOptions<>) ||
|
|
|
|
|
service.GetGenericTypeDefinition() == typeof(IPostConfigureOptions<>) ||
|
|
|
|
|
service.GetGenericTypeDefinition() == typeof(IOptionsMonitor<>)
|
|
|
|
|
) && implementation != null ? implementation.GetCustomAttribute<DIAttribute>() : service.GetCustomAttribute<DIAttribute>();
|
2022-02-01 14:51:29 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
var serviceName = $"{service}{implementation}";
|
2022-02-01 14:51:29 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
if (!Services[c.DIAttributeEnum].Contains(serviceName))
|
|
|
|
|
{
|
|
|
|
|
c.TryAdd(ServiceCollection, service, implementation);
|
|
|
|
|
Services[c.DIAttributeEnum].Add(serviceName);
|
2022-02-01 14:51:29 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
return true;
|
2022-02-01 14:51:29 +00:00
|
|
|
|
}
|
2022-02-03 15:26:44 +00:00
|
|
|
|
|
|
|
|
|
return false;
|
2020-02-17 08:58:14 +00:00
|
|
|
|
}
|
2022-02-08 11:07:28 +00:00
|
|
|
|
}
|