Laservall_manager_system/LM.Core/Extensions/AutofacManager/AutofacContainerModuleExten...

80 lines
3.0 KiB
C#

using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
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 LM.Core.CacheManager;
using LM.Core.Configuration;
using LM.Core.Const;
using LM.Core.DBManager;
using LM.Core.Enums;
using LM.Core.Extensions.AutofacManager;
//using LM.Core.KafkaManager.IService;
//using LM.Core.KafkaManager.Service;
using LM.Core.ManageUser;
using LM.Core.ObjectActionValidator;
using LM.Core.Services;
namespace LM.Core.Extensions
{
public static class AutofacContainerModuleExtension
{
// private static bool _isMysql = false;
public static IServiceCollection AddModule(this IServiceCollection services, ContainerBuilder builder, IConfiguration 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);
}
}
builder.RegisterAssemblyTypes(assemblyList.ToArray())
.Where(type => baseType.IsAssignableFrom(type) && !type.IsAbstract)
.AsSelf().AsImplementedInterfaces()
.InstancePerLifetimeScope();
builder.RegisterType<UserContext>().InstancePerLifetimeScope();
builder.RegisterType<ActionObserver>().InstancePerLifetimeScope();
//model校验结果
builder.RegisterType<ObjectModelValidatorState>().InstancePerLifetimeScope();
//启用缓存
if (AppSetting.UseRedis)
{
builder.RegisterType<RedisCacheService>().As<ICacheService>().SingleInstance();
}
else
{
builder.RegisterType<MemoryCacheService>().As<ICacheService>().SingleInstance();
}
//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;
}
}
}