Initial_commit_SecMPS_v2
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
using Autofac;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyModel;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Loader;
|
||||
using VolPro.Core.CacheManager;
|
||||
using VolPro.Core.Configuration;
|
||||
using VolPro.Core.Const;
|
||||
using VolPro.Core.Dapper;
|
||||
using VolPro.Core.DBManager;
|
||||
using VolPro.Core.EFDbContext;
|
||||
using VolPro.Core.Enums;
|
||||
using VolPro.Core.Extensions.AutofacManager;
|
||||
using VolPro.Core.ManageUser;
|
||||
using VolPro.Core.ObjectActionValidator;
|
||||
using VolPro.Core.Services;
|
||||
using Microsoft.EntityFrameworkCore.Internal;
|
||||
using System.Text;
|
||||
|
||||
namespace VolPro.Core.Extensions
|
||||
{
|
||||
public static class AutofacContainerModuleExtension
|
||||
{
|
||||
// private static bool _isMysql = false;
|
||||
public static IServiceCollection AddModule(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
//services.AddSession();
|
||||
//services.AddMemoryCache();
|
||||
//初始化配置文件
|
||||
AppSetting.Init(services, configuration);
|
||||
Type baseType = typeof(IDependency);
|
||||
var compilationLibrary = DependencyContext.Default
|
||||
.RuntimeLibraries
|
||||
.Where(x => !x.Serviceable
|
||||
&& x.Type == "project")
|
||||
.ToList();
|
||||
var count1 = compilationLibrary.Count;
|
||||
List<Assembly> assemblyList = new List<Assembly>();
|
||||
|
||||
foreach (var _compilation in compilationLibrary)
|
||||
{
|
||||
try
|
||||
{
|
||||
assemblyList.Add(AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(_compilation.Name)));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(_compilation.Name + ex.Message);
|
||||
}
|
||||
}
|
||||
//插件开发
|
||||
//try
|
||||
//{
|
||||
// var provider = services.BuildServiceProvider();
|
||||
// IWebHostEnvironment webHostEnvironment = provider.GetRequiredService<IWebHostEnvironment>();
|
||||
// string rootPath = (webHostEnvironment.ContentRootPath + "\\plugs").ReplacePath();
|
||||
// foreach (var item in Directory.GetFiles(rootPath).Where(x => x.EndsWith(".dll")))
|
||||
// {
|
||||
// string path = ($"{item}").ReplacePath();
|
||||
// AssemblyName assemblyName = Assembly.LoadFrom(path).GetName(); ;
|
||||
// assemblyList.Add(AssemblyLoadContext.Default.LoadFromAssemblyName(assemblyName));
|
||||
// }
|
||||
//}
|
||||
//catch (Exception ex)
|
||||
//{
|
||||
// Console.WriteLine($"解析类库异常:{ex.Message + ex.StackTrace}");
|
||||
//}
|
||||
int count = 0;
|
||||
foreach (var _compilation in compilationLibrary)
|
||||
{
|
||||
var types = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(_compilation.Name)).GetTypes();
|
||||
|
||||
var implementedInterfaces = types.Where(t => t.IsClass && !t.IsAbstract && t.GetInterfaces().Length > 0)
|
||||
.Where(type => baseType.IsAssignableFrom(type) && !type.IsAbstract)
|
||||
.Select(t => (serviceType: t.GetInterfaces(), implementationType: t))
|
||||
.ToList();
|
||||
|
||||
foreach (var (serviceType, implementationType) in implementedInterfaces)
|
||||
{
|
||||
if (serviceType.Length==1&& serviceType.Any(x => x == baseType))
|
||||
{
|
||||
services.AddScoped(implementationType);
|
||||
}
|
||||
else
|
||||
{
|
||||
services.AddScoped(serviceType[0], implementationType);
|
||||
//Console.WriteLine($"注入:{serviceType[0].Name},{implementationType.Name}");
|
||||
}
|
||||
count++;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Console.WriteLine(count);
|
||||
services.AddScoped<UserContext>();
|
||||
services.AddScoped<ActionObserver>();
|
||||
services.AddScoped<ObjectModelValidatorState>();
|
||||
// services.AddScoped<WeChatService>();
|
||||
services.AddSingleton(typeof(ICacheService), AppSetting.UseRedis ? typeof(RedisCacheService) : typeof(MemoryCacheService));
|
||||
//if (AppSetting.UseRedis)
|
||||
//{
|
||||
// services.AddSingleton<RedisCacheService>();
|
||||
//}
|
||||
if (DBType.Name == DbCurrentType.PgSql.ToString())
|
||||
{
|
||||
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
|
||||
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);
|
||||
}
|
||||
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
||||
|
||||
DapperParseGuidTypeHandler.InitParseGuid();
|
||||
DbCache.Init();
|
||||
//kafka注入
|
||||
//if (AppSetting.Kafka.UseConsumer)
|
||||
// builder.RegisterType<KafkaConsumer<string, string>>().As<IKafkaConsumer<string, string>>().SingleInstance();
|
||||
//if (AppSetting.Kafka.UseProducer)
|
||||
// builder.RegisterType<KafkaProducer<string, string>>().As<IKafkaProducer<string, string>>().SingleInstance();
|
||||
return services;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user