205 lines
8.7 KiB
C#
Executable File
205 lines
8.7 KiB
C#
Executable File
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Opsoft.NetCore.O3App.Shared;
|
|
using Opsoft.NetCore.O3App.Shared.Common;
|
|
using Opsoft.NetCore.O3App.Shared.Data;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
|
|
namespace Microsoft.Extensions.DependencyInjection
|
|
{
|
|
public static class ServiceCollectionExtensions
|
|
{
|
|
|
|
//
|
|
// 摘要:
|
|
// Register services defined specified System.Reflection.Assembly.
|
|
//
|
|
// 参数:
|
|
// services:
|
|
// The Microsoft.Extensions.DependencyInjection.IServiceCollection storing all service
|
|
// registrations.
|
|
//
|
|
// builderContext:
|
|
// The Microsoft.AspNetCore.Hosting.WebHostBuilderContext which carries hosting
|
|
// environment and configuration.
|
|
//
|
|
// assembly:
|
|
// The System.Reflection.Assembly in which services to register are defined.
|
|
//
|
|
// 返回结果:
|
|
// The current Microsoft.Extensions.DependencyInjection.IServiceCollection.
|
|
//
|
|
// 异常:
|
|
// T:System.ArgumentNullException:
|
|
// Specified services is null.
|
|
//
|
|
// T:System.ArgumentNullException:
|
|
// Specified builderContext is null.
|
|
//
|
|
// T:System.ArgumentNullException:
|
|
// Specified assembly is null.
|
|
public static IServiceCollection AddEnumerable(this IServiceCollection services, WebHostBuilderContext builderContext, Assembly assembly)
|
|
{
|
|
services = services ?? throw new ArgumentNullException("services");
|
|
builderContext = builderContext ?? throw new ArgumentNullException("builderContext");
|
|
assembly = assembly ?? throw new ArgumentNullException("assembly");
|
|
services.TryAddSingleton<IErrorFormatter, DefaultErrorFormatter>();
|
|
ApplyRegisterServiceAttributes(services, assembly);
|
|
ApplyServiceConfigurators(builderContext, services, assembly);
|
|
return services;
|
|
}
|
|
//
|
|
// 摘要:
|
|
// Register services defined specified System.Reflection.Assembly.
|
|
//
|
|
// 参数:
|
|
// services:
|
|
// The Microsoft.Extensions.DependencyInjection.IServiceCollection storing all service
|
|
// registrations.
|
|
//
|
|
// builderContext:
|
|
// The Microsoft.Extensions.Hosting.HostBuilderContext which carries hosting environment
|
|
// and configuration.
|
|
//
|
|
// assembly:
|
|
// The System.Reflection.Assembly in which services to register are defined.
|
|
//
|
|
// 返回结果:
|
|
// The current Microsoft.Extensions.DependencyInjection.IServiceCollection.
|
|
//
|
|
// 异常:
|
|
// T:System.ArgumentNullException:
|
|
// Specified services is null.
|
|
//
|
|
// T:System.ArgumentNullException:
|
|
// Specified builderContext is null.
|
|
//
|
|
// T:System.ArgumentNullException:
|
|
// Specified assembly is null.
|
|
public static IServiceCollection AddEnumerable(this IServiceCollection services, HostBuilderContext builderContext, Assembly assembly)
|
|
{
|
|
services = services ?? throw new ArgumentNullException("services");
|
|
builderContext = builderContext ?? throw new ArgumentNullException("builderContext");
|
|
assembly = assembly ?? throw new ArgumentNullException("assembly");
|
|
ApplyRegisterServiceAttributes(services, assembly);
|
|
ApplyServiceConfigurators(builderContext, services, assembly);
|
|
return services;
|
|
}
|
|
//
|
|
// 摘要:
|
|
// Register services defined specified System.Reflection.Assembly.
|
|
//
|
|
// 参数:
|
|
// services:
|
|
// The Microsoft.Extensions.DependencyInjection.IServiceCollection storing all service
|
|
// registrations.
|
|
//
|
|
// builderContext:
|
|
// The Microsoft.AspNetCore.Hosting.WebHostBuilderContext which carries hosting
|
|
// environment and configuration.
|
|
//
|
|
// assemblyName:
|
|
// The name of System.Reflection.Assembly in which services to register are defined.
|
|
//
|
|
// 返回结果:
|
|
// The current Microsoft.Extensions.DependencyInjection.IServiceCollection.
|
|
//
|
|
// 异常:
|
|
// T:System.ArgumentNullException:
|
|
// Specified services is null.
|
|
//
|
|
// T:System.ArgumentNullException:
|
|
// Specified builderContext is null.
|
|
//
|
|
// T:System.ArgumentNullException:
|
|
// Specified assemblyName is null.
|
|
public static IServiceCollection AddEnumerable(this IServiceCollection services, WebHostBuilderContext builderContext, string assemblyName)
|
|
{
|
|
Guard.ArgumentNotNullOrWhiteSpace(assemblyName, "assemblyName");
|
|
Assembly assembly = Assembly.Load(new AssemblyName(assemblyName));
|
|
return services.AddEnumerable(builderContext, assembly);
|
|
}
|
|
//
|
|
// 摘要:
|
|
// Register services defined specified System.Reflection.Assembly.
|
|
//
|
|
// 参数:
|
|
// services:
|
|
// The Microsoft.Extensions.DependencyInjection.IServiceCollection storing all service
|
|
// registrations.
|
|
//
|
|
// builderContext:
|
|
// The Microsoft.Extensions.Hosting.HostBuilderContext which carries hosting environment
|
|
// and configuration.
|
|
//
|
|
// assemblyName:
|
|
// The name of System.Reflection.Assembly in which services to register are defined.
|
|
//
|
|
// 返回结果:
|
|
// The current Microsoft.Extensions.DependencyInjection.IServiceCollection.
|
|
//
|
|
// 异常:
|
|
// T:System.ArgumentNullException:
|
|
// Specified services is null.
|
|
//
|
|
// T:System.ArgumentNullException:
|
|
// Specified builderContext is null.
|
|
//
|
|
// T:System.ArgumentNullException:
|
|
// Specified assemblyName is null.
|
|
public static IServiceCollection AddEnumerable(this IServiceCollection services, HostBuilderContext builderContext, string assemblyName)
|
|
{
|
|
Guard.ArgumentNotNullOrWhiteSpace(assemblyName, "assemblyName");
|
|
Assembly assembly = Assembly.Load(new AssemblyName(assemblyName));
|
|
return services.AddEnumerable(builderContext, assembly);
|
|
}
|
|
|
|
public static IServiceCollection AddDataSource(this IServiceCollection services, Action<DataSourceBuilder> configure)
|
|
{
|
|
DataSourceBuilder obj = new DataSourceBuilder(services);
|
|
configure?.Invoke(obj);
|
|
services.AddSingleton<IDbConnectionFactory, DbConnectionFactory>();
|
|
return services;
|
|
}
|
|
|
|
private static void ApplyServiceConfigurators(HostBuilderContext builderContext, IServiceCollection services, Assembly assembly)
|
|
{
|
|
foreach (ServiceConfiguratorAttribute customAttribute in assembly.GetCustomAttributes<ServiceConfiguratorAttribute>())
|
|
{
|
|
((IServiceConfigurator)Activator.CreateInstance(customAttribute.ServiceConfiguratorType)).ConfigureServices(builderContext, services);
|
|
}
|
|
}
|
|
|
|
private static void ApplyServiceConfigurators(WebHostBuilderContext builderContext, IServiceCollection services, Assembly assembly)
|
|
{
|
|
foreach (ServiceConfiguratorAttribute customAttribute in assembly.GetCustomAttributes<ServiceConfiguratorAttribute>())
|
|
{
|
|
((IServiceConfigurator)Activator.CreateInstance(customAttribute.ServiceConfiguratorType)).ConfigureServices(builderContext, services);
|
|
}
|
|
}
|
|
|
|
private static void ApplyRegisterServiceAttributes(IServiceCollection services, Assembly assembly)
|
|
{
|
|
Type[] types = assembly.GetTypes();
|
|
foreach (Type type in types)
|
|
{
|
|
IEnumerable<MapToAttribute> customAttributes = type.GetCustomAttributes<MapToAttribute>();
|
|
if (!customAttributes.Any())
|
|
{
|
|
continue;
|
|
}
|
|
foreach (MapToAttribute item2 in customAttributes)
|
|
{
|
|
ServiceDescriptor item = new ServiceDescriptor(item2.ServiceType, type, item2.Lifetime);
|
|
services.Add(item);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|