2020-02-17 08:58:14 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-10-18 16:03:14 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
2020-07-17 10:52:28 +00:00
|
|
|
|
|
2020-07-07 20:00:39 +00:00
|
|
|
|
using ASC.Common.Threading.Progress;
|
|
|
|
|
using ASC.Common.Threading.Workers;
|
2020-07-17 10:52:28 +00:00
|
|
|
|
|
2020-10-22 17:57:18 +00:00
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
2020-10-21 11:52:44 +00:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
2020-02-17 08:58:14 +00:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
2020-10-18 16:03:14 +00:00
|
|
|
|
using Microsoft.Extensions.Options;
|
2020-02-17 08:58:14 +00:00
|
|
|
|
|
|
|
|
|
namespace ASC.Common
|
|
|
|
|
{
|
2020-10-19 15:53:15 +00:00
|
|
|
|
public class TransientAttribute : DIAttribute
|
2020-10-18 16:03:14 +00:00
|
|
|
|
{
|
2020-10-19 15:53:15 +00:00
|
|
|
|
public TransientAttribute()
|
|
|
|
|
{
|
|
|
|
|
}
|
2020-10-18 16:03:14 +00:00
|
|
|
|
|
2020-10-20 13:24:46 +00:00
|
|
|
|
public TransientAttribute(Type service) : base(service)
|
2020-10-19 15:53:15 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-20 13:24:46 +00:00
|
|
|
|
public TransientAttribute(Type service, Type implementation) : base(service, implementation)
|
2020-10-19 15:53:15 +00:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ScopeAttribute : DIAttribute
|
|
|
|
|
{
|
2020-10-18 16:03:14 +00:00
|
|
|
|
public ScopeAttribute()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-20 13:24:46 +00:00
|
|
|
|
public ScopeAttribute(Type service) : base(service)
|
2020-10-18 16:03:14 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-20 13:24:46 +00:00
|
|
|
|
public ScopeAttribute(Type service, Type implementation) : base(service, implementation)
|
2020-10-18 16:03:14 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class SingletoneAttribute : DIAttribute
|
|
|
|
|
{
|
|
|
|
|
public SingletoneAttribute()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-20 13:24:46 +00:00
|
|
|
|
public SingletoneAttribute(Type service) : base(service)
|
2020-10-18 16:03:14 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-20 13:24:46 +00:00
|
|
|
|
public SingletoneAttribute(Type service, Type implementation) : base(service, implementation)
|
2020-10-18 16:03:14 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class DIAttribute : Attribute
|
|
|
|
|
{
|
2020-10-20 13:24:46 +00:00
|
|
|
|
public Type Implementation { get; }
|
|
|
|
|
public Type Service { get; }
|
|
|
|
|
public Type Additional { get; set; }
|
2020-10-18 16:03:14 +00:00
|
|
|
|
|
|
|
|
|
public DIAttribute()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-20 13:24:46 +00:00
|
|
|
|
public DIAttribute(Type service)
|
2020-10-18 16:03:14 +00:00
|
|
|
|
{
|
2020-10-20 13:24:46 +00:00
|
|
|
|
Service = service;
|
2020-10-18 16:03:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-20 13:24:46 +00:00
|
|
|
|
public DIAttribute(Type service, Type implementation)
|
2020-10-18 16:03:14 +00:00
|
|
|
|
{
|
2020-10-20 13:24:46 +00:00
|
|
|
|
Implementation = implementation;
|
|
|
|
|
Service = service;
|
2020-10-18 16:03:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-17 08:58:14 +00:00
|
|
|
|
public class DIHelper
|
|
|
|
|
{
|
2020-10-20 20:10:17 +00:00
|
|
|
|
public List<string> Added { get; set; }
|
2020-02-17 08:58:14 +00:00
|
|
|
|
public List<string> Singleton { get; set; }
|
|
|
|
|
public List<string> Scoped { get; set; }
|
|
|
|
|
public List<string> Transient { get; set; }
|
2020-02-17 12:13:40 +00:00
|
|
|
|
public List<string> Configured { get; set; }
|
2020-10-18 16:34:30 +00:00
|
|
|
|
public IServiceCollection ServiceCollection { get; private set; }
|
2020-02-17 08:58:14 +00:00
|
|
|
|
|
2020-10-18 16:34:30 +00:00
|
|
|
|
public DIHelper()
|
2020-02-17 08:58:14 +00:00
|
|
|
|
{
|
2020-10-20 20:10:17 +00:00
|
|
|
|
Added = new List<string>();
|
2020-02-17 08:58:14 +00:00
|
|
|
|
Singleton = new List<string>();
|
|
|
|
|
Scoped = new List<string>();
|
|
|
|
|
Transient = new List<string>();
|
2020-02-17 12:13:40 +00:00
|
|
|
|
Configured = new List<string>();
|
2020-10-18 16:34:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-26 16:38:23 +00:00
|
|
|
|
public DIHelper(IServiceCollection serviceCollection) : this()
|
2020-10-18 16:34:30 +00:00
|
|
|
|
{
|
|
|
|
|
ServiceCollection = serviceCollection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Configure(IServiceCollection serviceCollection)
|
|
|
|
|
{
|
2020-02-17 08:58:14 +00:00
|
|
|
|
ServiceCollection = serviceCollection;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-17 10:52:28 +00:00
|
|
|
|
public bool TryAddScoped<TService>() where TService : class
|
2020-02-17 08:58:14 +00:00
|
|
|
|
{
|
|
|
|
|
var serviceName = $"{typeof(TService)}";
|
|
|
|
|
if (!Scoped.Contains(serviceName))
|
|
|
|
|
{
|
|
|
|
|
Scoped.Add(serviceName);
|
|
|
|
|
ServiceCollection.TryAddScoped<TService>();
|
2020-07-17 10:52:28 +00:00
|
|
|
|
return true;
|
2020-02-17 08:58:14 +00:00
|
|
|
|
}
|
2020-07-17 10:52:28 +00:00
|
|
|
|
return false;
|
2020-02-17 08:58:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 16:03:14 +00:00
|
|
|
|
public bool TryAdd<TService>() where TService : class
|
|
|
|
|
{
|
|
|
|
|
return TryAdd(typeof(TService));
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-20 20:10:17 +00:00
|
|
|
|
public bool TryAdd<TService, TImplementation>() where TService : class
|
|
|
|
|
{
|
|
|
|
|
return TryAdd(typeof(TService), typeof(TImplementation));
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 16:03:14 +00:00
|
|
|
|
public bool TryAdd(Type service, Type implementation = null)
|
|
|
|
|
{
|
2020-10-26 16:38:23 +00:00
|
|
|
|
if (service.IsInterface && service.IsGenericType && (service.GetGenericTypeDefinition() == typeof(IOptionsSnapshot<>) || service.GetGenericTypeDefinition() == typeof(IOptions<>)))
|
2020-10-22 17:57:18 +00:00
|
|
|
|
{
|
|
|
|
|
service = service.GetGenericArguments().FirstOrDefault();
|
|
|
|
|
if (service == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-20 20:10:17 +00:00
|
|
|
|
var serviceName = $"{service}{implementation}";
|
|
|
|
|
if (Added.Contains(serviceName)) return false;
|
|
|
|
|
Added.Add(serviceName);
|
|
|
|
|
|
2020-10-27 15:34:22 +00:00
|
|
|
|
if (serviceName == "ASC.Notify.DbWorker")
|
2020-10-21 13:39:06 +00:00
|
|
|
|
{
|
2020-10-22 17:57:18 +00:00
|
|
|
|
var qqaz = 0;
|
2020-10-21 13:39:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-26 13:38:21 +00:00
|
|
|
|
var di = service.IsGenericType && (service.GetGenericTypeDefinition() == typeof(IConfigureOptions<>) || service.GetGenericTypeDefinition() == typeof(IPostConfigureOptions<>)) && implementation != null ? implementation.GetCustomAttribute<DIAttribute>() : service.GetCustomAttribute<DIAttribute>();
|
2020-10-18 16:03:14 +00:00
|
|
|
|
var isnew = false;
|
|
|
|
|
|
|
|
|
|
if (di != null)
|
|
|
|
|
{
|
2020-10-20 20:10:17 +00:00
|
|
|
|
if (di.Additional != null)
|
|
|
|
|
{
|
|
|
|
|
var m = di.Additional.GetMethod("Register", BindingFlags.Public | BindingFlags.Static);
|
|
|
|
|
m.Invoke(null, new[] { this });
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-19 07:19:32 +00:00
|
|
|
|
if (!service.IsInterface || implementation != null)
|
2020-10-18 16:03:14 +00:00
|
|
|
|
{
|
2020-10-19 07:19:32 +00:00
|
|
|
|
isnew = implementation != null ? Register(service, implementation) : Register(service);
|
2020-10-20 13:24:46 +00:00
|
|
|
|
if (!isnew) return false;
|
2020-10-19 07:19:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (service.IsInterface && implementation == null || !service.IsInterface)
|
|
|
|
|
{
|
2020-10-20 13:24:46 +00:00
|
|
|
|
if (di.Service != null)
|
2020-10-18 16:03:14 +00:00
|
|
|
|
{
|
2020-10-26 13:38:21 +00:00
|
|
|
|
var a = di.Service.GetInterfaces().FirstOrDefault(x => x.IsGenericType && (x.GetGenericTypeDefinition() == typeof(IConfigureOptions<>) || x.GetGenericTypeDefinition() == typeof(IPostConfigureOptions<>)));
|
2020-10-19 07:19:32 +00:00
|
|
|
|
if (a != null)
|
|
|
|
|
{
|
2020-10-20 13:24:46 +00:00
|
|
|
|
if (!a.ContainsGenericParameters)
|
2020-10-19 07:19:32 +00:00
|
|
|
|
{
|
2020-10-20 13:24:46 +00:00
|
|
|
|
var b = a.GetGenericArguments();
|
|
|
|
|
|
|
|
|
|
foreach (var g in b)
|
2020-10-19 19:04:07 +00:00
|
|
|
|
{
|
2020-10-20 13:24:46 +00:00
|
|
|
|
if (g != service)
|
|
|
|
|
{
|
|
|
|
|
TryAdd(g);
|
2020-10-21 13:39:06 +00:00
|
|
|
|
if (service.IsInterface && di.Implementation == null)
|
2020-10-20 13:24:46 +00:00
|
|
|
|
{
|
|
|
|
|
TryAdd(service, g);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-19 19:04:07 +00:00
|
|
|
|
}
|
2020-10-18 16:03:14 +00:00
|
|
|
|
|
2020-10-20 13:24:46 +00:00
|
|
|
|
TryAdd(a, di.Service);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-10-20 20:10:17 +00:00
|
|
|
|
Type c = null;
|
|
|
|
|
var a1 = a.GetGenericTypeDefinition();
|
|
|
|
|
var b = a.GetGenericArguments().FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (b != null && b.IsGenericType)
|
|
|
|
|
{
|
2020-10-21 11:12:08 +00:00
|
|
|
|
var b1 = b.GetGenericTypeDefinition().MakeGenericType(service.GetGenericArguments());
|
|
|
|
|
|
|
|
|
|
TryAdd(b1);
|
|
|
|
|
c = a1.MakeGenericType(b1);
|
2020-10-20 20:10:17 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
c = a1.MakeGenericType(service.GetGenericArguments());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TryAdd(c, di.Service.MakeGenericType(service.GetGenericArguments()));
|
2020-10-20 13:24:46 +00:00
|
|
|
|
//a, di.Service
|
|
|
|
|
}
|
2020-10-19 07:19:32 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2020-10-18 16:03:14 +00:00
|
|
|
|
{
|
2020-10-21 13:39:06 +00:00
|
|
|
|
isnew = di.Implementation == null ? Register(service, di.Service) : Register(di.Service);
|
2020-10-18 16:03:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-20 13:24:46 +00:00
|
|
|
|
if (di.Implementation != null)
|
2020-10-18 16:03:14 +00:00
|
|
|
|
{
|
2020-10-26 13:38:21 +00:00
|
|
|
|
var a = di.Implementation.GetInterfaces().FirstOrDefault(x => x.IsGenericType && (x.GetGenericTypeDefinition() == typeof(IConfigureOptions<>) || x.GetGenericTypeDefinition() == typeof(IPostConfigureOptions<>)));
|
2020-10-18 16:03:14 +00:00
|
|
|
|
if (a != null)
|
|
|
|
|
{
|
2020-10-20 13:24:46 +00:00
|
|
|
|
if (!a.ContainsGenericParameters)
|
|
|
|
|
{
|
|
|
|
|
var b = a.GetGenericArguments();
|
|
|
|
|
|
|
|
|
|
foreach (var g in b)
|
|
|
|
|
{
|
2020-10-21 13:39:06 +00:00
|
|
|
|
if (g != service)
|
|
|
|
|
{
|
|
|
|
|
//TryAdd(g);
|
|
|
|
|
if (service.IsInterface && implementation == null)
|
|
|
|
|
{
|
|
|
|
|
TryAdd(service, g);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-20 13:24:46 +00:00
|
|
|
|
}
|
2020-10-18 16:03:14 +00:00
|
|
|
|
|
2020-10-20 13:24:46 +00:00
|
|
|
|
TryAdd(a, di.Implementation);
|
|
|
|
|
}
|
|
|
|
|
else
|
2020-10-18 16:03:14 +00:00
|
|
|
|
{
|
2020-10-21 13:39:06 +00:00
|
|
|
|
Type c = null;
|
|
|
|
|
var a1 = a.GetGenericTypeDefinition();
|
|
|
|
|
var b = a.GetGenericArguments().FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (b != null && b.IsGenericType)
|
|
|
|
|
{
|
|
|
|
|
var b1 = b.GetGenericTypeDefinition().MakeGenericType(service.GetGenericArguments());
|
|
|
|
|
|
|
|
|
|
TryAdd(b1);
|
|
|
|
|
c = a1.MakeGenericType(b1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
c = a1.MakeGenericType(service.GetGenericArguments());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TryAdd(c, di.Implementation.MakeGenericType(service.GetGenericArguments()));
|
|
|
|
|
//a, di.Service
|
2020-10-18 16:03:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-19 07:19:32 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2020-10-21 13:39:06 +00:00
|
|
|
|
isnew = TryAdd(service, di.Implementation);
|
2020-10-19 07:19:32 +00:00
|
|
|
|
}
|
2020-10-18 16:03:14 +00:00
|
|
|
|
}
|
2020-10-20 13:24:46 +00:00
|
|
|
|
}
|
2020-10-18 16:03:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isnew)
|
|
|
|
|
{
|
2020-10-19 19:04:07 +00:00
|
|
|
|
ConstructorInfo[] props = null;
|
2020-10-18 16:03:14 +00:00
|
|
|
|
|
2020-10-19 19:04:07 +00:00
|
|
|
|
if (!service.IsInterface)
|
2020-10-18 16:03:14 +00:00
|
|
|
|
{
|
2020-10-19 19:04:07 +00:00
|
|
|
|
props = service.GetConstructors();
|
|
|
|
|
}
|
|
|
|
|
else if (implementation != null)
|
|
|
|
|
{
|
|
|
|
|
props = implementation.GetConstructors();
|
|
|
|
|
}
|
2020-10-20 13:24:46 +00:00
|
|
|
|
else if (di.Service != null)
|
2020-10-19 19:04:07 +00:00
|
|
|
|
{
|
2020-10-20 13:24:46 +00:00
|
|
|
|
props = di.Service.GetConstructors();
|
2020-10-19 19:04:07 +00:00
|
|
|
|
}
|
2020-10-18 16:03:14 +00:00
|
|
|
|
|
2020-10-19 19:04:07 +00:00
|
|
|
|
if (props != null)
|
|
|
|
|
{
|
2020-10-20 20:10:17 +00:00
|
|
|
|
var par = props.SelectMany(r => r.GetParameters()).Distinct();
|
|
|
|
|
foreach (var p1 in par)
|
2020-10-18 16:03:14 +00:00
|
|
|
|
{
|
2020-10-20 20:10:17 +00:00
|
|
|
|
TryAdd(p1.ParameterType);
|
2020-10-18 16:03:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return isnew;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool Register(Type service)
|
|
|
|
|
{
|
2020-10-22 17:57:18 +00:00
|
|
|
|
if (service.IsSubclassOf(typeof(ControllerBase)) || service.GetInterfaces().Contains(typeof(IResourceFilter)) || service.GetInterfaces().Contains(typeof(IAuthenticationHandler))) return true;
|
2020-10-18 16:03:14 +00:00
|
|
|
|
var c = service.GetCustomAttribute<DIAttribute>();
|
|
|
|
|
var serviceName = $"{service}";
|
|
|
|
|
if (c is ScopeAttribute)
|
|
|
|
|
{
|
|
|
|
|
if (!Scoped.Contains(serviceName))
|
|
|
|
|
{
|
|
|
|
|
Scoped.Add(serviceName);
|
|
|
|
|
ServiceCollection.TryAddScoped(service);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (c is SingletoneAttribute)
|
|
|
|
|
{
|
|
|
|
|
if (!Singleton.Contains(serviceName))
|
|
|
|
|
{
|
|
|
|
|
Singleton.Add(serviceName);
|
|
|
|
|
ServiceCollection.TryAddSingleton(service);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-19 15:53:15 +00:00
|
|
|
|
else if (c is TransientAttribute)
|
|
|
|
|
{
|
|
|
|
|
if (!Transient.Contains(serviceName))
|
|
|
|
|
{
|
|
|
|
|
Transient.Add(serviceName);
|
|
|
|
|
ServiceCollection.TryAddTransient(service);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-18 16:03:14 +00:00
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool Register(Type service, Type implementation)
|
|
|
|
|
{
|
2020-10-21 11:52:44 +00:00
|
|
|
|
if (service.IsSubclassOf(typeof(ControllerBase)) || service.GetInterfaces().Contains(typeof(IResourceFilter))) return true;
|
2020-10-26 13:38:21 +00:00
|
|
|
|
var c = service.IsGenericType && (service.GetGenericTypeDefinition() == typeof(IConfigureOptions<>) || service.GetGenericTypeDefinition() == typeof(IPostConfigureOptions<>)) && implementation != null ? implementation.GetCustomAttribute<DIAttribute>() : service.GetCustomAttribute<DIAttribute>();
|
2020-10-18 16:03:14 +00:00
|
|
|
|
var serviceName = $"{service}{implementation}";
|
|
|
|
|
if (c is ScopeAttribute)
|
|
|
|
|
{
|
|
|
|
|
if (!Scoped.Contains(serviceName))
|
|
|
|
|
{
|
|
|
|
|
Scoped.Add(serviceName);
|
|
|
|
|
ServiceCollection.TryAddScoped(service, implementation);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (c is SingletoneAttribute)
|
|
|
|
|
{
|
|
|
|
|
if (!Singleton.Contains(serviceName))
|
|
|
|
|
{
|
|
|
|
|
Singleton.Add(serviceName);
|
2020-10-21 11:12:08 +00:00
|
|
|
|
ServiceCollection.AddSingleton(service, implementation);
|
2020-10-18 16:03:14 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-19 15:53:15 +00:00
|
|
|
|
else if (c is TransientAttribute)
|
|
|
|
|
{
|
|
|
|
|
if (!Transient.Contains(serviceName))
|
|
|
|
|
{
|
|
|
|
|
Transient.Add(serviceName);
|
|
|
|
|
ServiceCollection.TryAddTransient(service, implementation);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-18 16:03:14 +00:00
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-08 08:36:47 +00:00
|
|
|
|
public bool TryAddScoped<TService, TImplementation>() where TService : class where TImplementation : class, TService
|
2020-02-17 08:58:14 +00:00
|
|
|
|
{
|
|
|
|
|
var serviceName = $"{typeof(TService)}{typeof(TImplementation)}";
|
|
|
|
|
if (!Scoped.Contains(serviceName))
|
|
|
|
|
{
|
|
|
|
|
Scoped.Add(serviceName);
|
|
|
|
|
ServiceCollection.TryAddScoped<TService, TImplementation>();
|
2020-10-08 08:36:47 +00:00
|
|
|
|
return true;
|
2020-02-17 08:58:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-08 08:36:47 +00:00
|
|
|
|
return false;
|
2020-02-17 08:58:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-03 09:15:05 +00:00
|
|
|
|
|
2020-02-17 08:58:14 +00:00
|
|
|
|
public DIHelper TryAddSingleton<TService>() where TService : class
|
|
|
|
|
{
|
|
|
|
|
var serviceName = $"{typeof(TService)}";
|
|
|
|
|
if (!Singleton.Contains(serviceName))
|
|
|
|
|
{
|
|
|
|
|
Singleton.Add(serviceName);
|
|
|
|
|
ServiceCollection.TryAddSingleton<TService>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DIHelper TryAddSingleton<TService>(Func<IServiceProvider, TService> implementationFactory) where TService : class
|
|
|
|
|
{
|
|
|
|
|
var serviceName = $"{typeof(TService)}";
|
|
|
|
|
if (!Singleton.Contains(serviceName))
|
|
|
|
|
{
|
|
|
|
|
Singleton.Add(serviceName);
|
|
|
|
|
ServiceCollection.TryAddSingleton(implementationFactory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DIHelper TryAddSingleton<TService, TImplementation>() where TService : class where TImplementation : class, TService
|
|
|
|
|
{
|
|
|
|
|
var serviceName = $"{typeof(TService)}{typeof(TImplementation)}";
|
|
|
|
|
if (!Singleton.Contains(serviceName))
|
|
|
|
|
{
|
|
|
|
|
Singleton.Add(serviceName);
|
|
|
|
|
ServiceCollection.TryAddSingleton<TService, TImplementation>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DIHelper AddSingleton<TService, TImplementation>() where TService : class where TImplementation : class, TService
|
|
|
|
|
{
|
|
|
|
|
var serviceName = $"{typeof(TService)}{typeof(TImplementation)}";
|
|
|
|
|
if (!Singleton.Contains(serviceName))
|
|
|
|
|
{
|
|
|
|
|
Singleton.Add(serviceName);
|
2020-10-12 19:39:23 +00:00
|
|
|
|
ServiceCollection.AddSingleton<TService, TImplementation>();
|
2020-02-17 08:58:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DIHelper TryAddSingleton<TService, TImplementation>(TService tservice, TImplementation tImplementation) where TService : Type where TImplementation : Type
|
|
|
|
|
{
|
|
|
|
|
var serviceName = $"{tservice}{tImplementation}";
|
|
|
|
|
if (!Singleton.Contains(serviceName))
|
|
|
|
|
{
|
|
|
|
|
Singleton.Add(serviceName);
|
|
|
|
|
ServiceCollection.TryAddSingleton(tservice, tImplementation);
|
|
|
|
|
}
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DIHelper Configure<TOptions>(Action<TOptions> configureOptions) where TOptions : class
|
|
|
|
|
{
|
2020-02-17 12:13:40 +00:00
|
|
|
|
var serviceName = $"{typeof(TOptions)}";
|
|
|
|
|
if (!Configured.Contains(serviceName))
|
|
|
|
|
{
|
|
|
|
|
Configured.Add(serviceName);
|
2020-10-12 19:39:23 +00:00
|
|
|
|
ServiceCollection.Configure(configureOptions);
|
2020-02-17 12:13:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-17 08:58:14 +00:00
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-07 20:00:39 +00:00
|
|
|
|
private void AddToConfigured<TOptions>(string type, Action<TOptions> action) where TOptions : class
|
|
|
|
|
{
|
|
|
|
|
if (!Configured.Contains(type))
|
|
|
|
|
{
|
|
|
|
|
Configured.Add(type);
|
2020-10-12 19:39:23 +00:00
|
|
|
|
ServiceCollection.Configure(action);
|
2020-07-07 20:00:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-17 10:52:28 +00:00
|
|
|
|
public DIHelper AddWorkerQueue<T1>(int workerCount, int waitInterval, bool stopAfterFinsih, int errorCount)
|
|
|
|
|
{
|
2020-10-12 13:52:31 +00:00
|
|
|
|
void action(WorkerQueue<T1> a)
|
2020-07-07 20:00:39 +00:00
|
|
|
|
{
|
|
|
|
|
a.workerCount = workerCount;
|
|
|
|
|
a.waitInterval = waitInterval;
|
|
|
|
|
a.stopAfterFinsih = stopAfterFinsih;
|
|
|
|
|
a.errorCount = errorCount;
|
2020-10-12 13:52:31 +00:00
|
|
|
|
}
|
|
|
|
|
AddToConfigured($"{typeof(WorkerQueue<T1>)}", (Action<WorkerQueue<T1>>)action);
|
2020-07-07 20:00:39 +00:00
|
|
|
|
return this;
|
|
|
|
|
}
|
2020-10-22 17:57:18 +00:00
|
|
|
|
|
2020-07-07 20:00:39 +00:00
|
|
|
|
public DIHelper AddProgressQueue<T1>(int workerCount, int waitInterval, bool removeAfterCompleted, bool stopAfterFinsih, int errorCount) where T1 : class, IProgressItem
|
|
|
|
|
{
|
2020-10-12 13:52:31 +00:00
|
|
|
|
void action(ProgressQueue<T1> a)
|
2020-07-07 20:00:39 +00:00
|
|
|
|
{
|
|
|
|
|
a.workerCount = workerCount;
|
|
|
|
|
a.waitInterval = waitInterval;
|
|
|
|
|
a.stopAfterFinsih = stopAfterFinsih;
|
|
|
|
|
a.errorCount = errorCount;
|
|
|
|
|
a.removeAfterCompleted = removeAfterCompleted;
|
2020-10-12 13:52:31 +00:00
|
|
|
|
}
|
|
|
|
|
AddToConfigured($"{typeof(ProgressQueue<T1>)}", (Action<ProgressQueue<T1>>)action);
|
2020-07-07 20:00:39 +00:00
|
|
|
|
return this;
|
|
|
|
|
}
|
2020-10-22 17:57:18 +00:00
|
|
|
|
|
2020-02-17 08:58:14 +00:00
|
|
|
|
public DIHelper Configure<TOptions>(string name, Action<TOptions> configureOptions) where TOptions : class
|
|
|
|
|
{
|
2020-02-17 12:13:40 +00:00
|
|
|
|
var serviceName = $"{typeof(TOptions)}{name}";
|
|
|
|
|
if (!Configured.Contains(serviceName))
|
|
|
|
|
{
|
|
|
|
|
Configured.Add(serviceName);
|
2020-10-12 19:39:23 +00:00
|
|
|
|
ServiceCollection.Configure(name, configureOptions);
|
2020-02-17 12:13:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-17 08:58:14 +00:00
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|