hutongqing
2024-12-10 8d341db9d2d5699d527c88c935f0c4ce255a57a4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using Autofac;
using Autofac.Extras.DynamicProxy;
using HslCommunication.WebSocket;
using Microsoft.Extensions.DependencyModel;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core.AOP;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Helper;
 
namespace WIDESEA_Core.Extensions
{
    public class AutofacModuleRegister : Autofac.Module
    {
        //private static readonly ILog log = LogManager.GetLogger(typeof(AutofacModuleRegister));
        protected override void Load(ContainerBuilder builder)
        {
            var basePath = AppContext.BaseDirectory;
            var cacheType = new List<Type>();
 
            builder.RegisterType<LogAOP>();
            if (AppSettings.Get("LogAopEnable").ObjToBool())
            {
                cacheType.Add(typeof(LogAOP));
            }
            
            builder.RegisterGeneric(typeof(RepositoryBase<>)).As(typeof(IRepository<>)).InstancePerDependency();//注册仓储
            builder.RegisterGeneric(typeof(ServiceBase<,>)).As(typeof(IService<>)).InstancePerDependency();//注册服务
 
            Type baseType = typeof(IDependency);
 
            List<RuntimeLibrary> compilationLibrary = DependencyContext.Default
                    .RuntimeLibraries
                    .Where(x => !x.Serviceable
                    && x.Type == "project")
                    .ToList();
            List<Assembly> assemblyList = new List<Assembly>();
            foreach (var library in compilationLibrary)
            {
                try
                {
                    string path = Path.Combine(basePath, $"{library.Name}.dll");
                    if (!File.Exists(path))
                    {
                        var msg = $"{library.Name}.dll丢失,因为项目解耦了,所以需要先F6编译,再F5运行,请检查 bin 文件夹,并拷贝。";
                        //log.Error(msg);
                        throw new Exception(msg);
                    }
                    assemblyList.Add(Assembly.LoadFrom(path));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(library.Name + ex.Message);
                }
            }
 
            builder.RegisterAssemblyTypes(assemblyList.ToArray()).Where(x => !x.IsInterface && !x.IsAbstract && baseType.IsAssignableFrom(x))
                        .AsImplementedInterfaces()
                        .PropertiesAutowired()
                        .InstancePerDependency().
                        EnableInterfaceInterceptors()
                        .InterceptedBy(cacheType.ToArray());
 
            builder.RegisterType<UnitOfWorkManage>().As<IUnitOfWorkManage>()
               .AsImplementedInterfaces()
               .InstancePerLifetimeScope()
               .PropertiesAutowired();
 
        }
    }
}