Administrator
2 天以前 4b4db960821387e8e29151ca15a1a7a2825f5f6f
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using Autofac;
using AutoMapper;
using Newtonsoft.Json;
using Quartz;
using Serilog;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Utility.Entity;
 
namespace Utility.Job
{
    public class InternalMethodJob : JobBase<LogModel>, IJob
    {
 
        public InternalMethodJob() : base(new LogModel())
        {
 
        }
 
        public override async Task NextExecute(IJobExecutionContext context)
        {
            // 获取相关参数
            var className = context.JobDetail.JobDataMap.GetString(Constant.CLASSNAME)?.Trim();
            var methodName = context.JobDetail.JobDataMap.GetString(Constant.METHODNAME)?.Trim();
            var methodParameters = context.JobDetail.JobDataMap.GetString(Constant.METHODPARAMETERS);
 
            try
            {
                /// 加载 WMS.BLL 类库
                Assembly assembly;
                try
                {
                    assembly = Assembly.LoadFrom("WMS.BLL.dll");
                }
                catch (Exception)
                {
                    assembly = Assembly.LoadFrom("bin\\Debug\\netcoreapp3.1\\WMS.BLL.dll");
                }
 
                // 获取 WMS.BLL的类型
                Type classType = assembly.GetType("WMS.BLL." + className);
 
                if (classType == null)
                {
                    throw new TypeLoadException($"找不到该类型{className}");
                }
                #region 容器创建示例  有报错搞不定抽空再弄
 
                //var builder = new ContainerBuilder();
 
                //// 注册需要的服务和类型
                //builder.RegisterAssemblyTypes(Assembly.Load("WMS.IDAL"), Assembly.Load("WMS.DAL"), Assembly.Load("WMS.IBLL"),
                //    Assembly.Load("WMS.BLL"));
                //builder.RegisterType<Mapper>().As<IMapper>();
 
                //// 使用 Autofac 容器来解析类的实例
                //var scope = builder.Build();
 
                //var instance = scope.Resolve(classType);
                #endregion
 
                // 创建实例(如果方法是实例方法) 无注入 必须有空参数的构造函数
                object instance = Activator.CreateInstance(classType);
 
                // 获取方法信息
                MethodInfo methodInfo = classType.GetMethod(methodName);
 
                if (methodInfo == null)
                {
                    throw new ArgumentException($"找不到该方法{methodName}");
                }
 
                // 调用方法
                object result = null;
                if (methodInfo.GetParameters().Length == 0)
                {
                    // 无参方法
                    result = methodInfo.Invoke(instance, null);
                }
                else
                {
                    // 有参方法
                    var parameters = JsonConvert.DeserializeObject<object[]>(methodParameters); // 解析方法参数
                    result = await (Task<object>)methodInfo.Invoke(instance, parameters);
                }
 
                // 处理方法执行结果
                LogInfo.Result = JsonConvert.SerializeObject(result);
            }
            catch (Exception ex)
            {
                LogInfo.ErrorMsg = ex.Message;
                context.JobDetail.JobDataMap[Constant.EXCEPTION] = $"<div class='err-time'>{LogInfo.BeginTime}</div>{JsonConvert.SerializeObject(LogInfo)}";
            }
            //_logger.Error(ex, $"执行方法时出错 {methodName}: {ex.Message}");
        }
    }
}