namespace TaskCore.Test
{
// This project can output the Class library as a NuGet Package.
// To enable this option, right-click on the project and select the Properties menu item. In the Build tab select "Produce outputs on build".
public class BlogsObj : TPlugin
{
public BlogsObj()
{
}
public override void TPlugin_Load()
{
var sbLog = new StringBuilder(string.Empty);
try
{
sbLog.Append($"这里是BlogsObj,获取配置文件:{this.XmlConfig.Name}");
//代码块
//
new WriteLog()._WriteLog($"{DateTime.Now}测试引用nuget包");
}
catch (Exception ex)
{
sbLog.Append($"异常信息:{ex.Message}");
}
finally
{
PublicClass._WriteLog(sbLog.ToString(), this.XmlConfig.Name);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TaskCore.Plugin;
namespace TaskCore.Test
{
// This project can output the Class library as a NuGet Package.
// To enable this option, right-click on the project and select the Properties menu item. In the Build tab select "Produce outputs on build".
public class BlogsObj01 : TPlugin
{
public BlogsObj01()
{
}
public override void TPlugin_Load()
{
var sbLog = new StringBuilder(string.Empty);
try
{
sbLog.Append($"这里是BlogsObj01,获取配置文件:{this.XmlConfig.Name}");
//代码块
//
}
catch (Exception ex)
{
sbLog.Append($"异常信息:{ex.Message}");
}
finally
{
//Console.WriteLine($"这里是Blogs,获取配置文件:{this.XmlConfig.Name}");
PublicClass._WriteLog(sbLog.ToString(), this.XmlConfig.Name);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TaskCore.Plugin;
namespace TaskCore.Test
{
// This project can output the Class library as a NuGet Package.
// To enable this option, right-click on the project and select the Properties menu item. In the Build tab select "Produce outputs on build".
public class BlogsObj02 : TPlugin
{
public BlogsObj02()
{
}
public override void TPlugin_Load()
{
//Console.WriteLine($"这里是Blogs,获取配置文件:{this.XmlConfig.Name}");
PublicClass._WriteLog($"这里是BlogsObj02,获取配置文件:{this.XmlConfig.Name}", this.XmlConfig.Name);
}
}
}
<!-- 1.xml配置模板 2.utf-8文件 3.复制到程序根目录下面PluginXml文件夹下 4.每个任务建议创建和程序dll名称相同xml配置文件 --> <TaskMain> <!--固定:执行任务时间计时器(分钟)--> <Timer>1</Timer> <!--固定:任务名称--> <Name>获取博客信息</Name> <!--固定:账号--> <UserName></UserName> <!--固定:密码--> <UserPwd></UserPwd> <!--固定:key--> <ApiKey></ApiKey> <!--固定:key--> <ApiUrl></ApiUrl> <!--固定:是否关闭任务 1:是 0:否--> <CloseTask>0</CloseTask> <!--固定:描述--> <Des>获取博客信息</Des> <!--自定义:其他配置信息--> <Other> <ShenNiuBuXing3>神牛步行3</ShenNiuBuXing3> </Other> </TaskMain>
<!-- CrossFiles指定对应任务的dll文件,必须存在的文件 --> <TaskMain> <File>TaskCore.Test.dll</File> </TaskMain>
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.NETCore.App": {
//"type": "platform", 跨平台发布需要注释
"version": "1.0.0"
},
"System.IO.FileSystem": "4.0.1",
"System.Reflection": "4.1.0",
"System.Text.Encoding.CodePages": "4.0.1",
"System.Threading.Timer": "4.0.1",
"System.Xml.XDocument": "4.0.11",
"TaskCore.Plugin": "1.0.0"
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
},
//跨平台发布需要添加如下节点
"runtimes": {
"ubuntu.16.04-x64": { }, //运行在ubuntu.16.04的64位系统
"win7-x64": { } //运行在win7的64位系统
}
}
namespace TaskCore.MainForm
{
/// <summary>
/// author 神牛步行3
/// contact 841202396@qq.com
/// des TaskCore.MainForm跨平台插件由神牛步行3提供
/// </summary>
public class Program
{
private static Dictionary<string, MoAssembly> dicTasks = new Dictionary<string, MoAssembly>();
public static void Main(string[] args)
{
//注册编码,防止乱码
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
//初始化程序集文件
_Init();
//是否继续开启任务,默认没有待执行任务,不提示
if (dicTasks.Count <= 0) { _LoopAlert("是否退出?(Y/N)"); return; }
_LoopAlert("是否开始执行任务?(Y / N)");
//执行任务
foreach (var item in dicTasks.Values)
{
//使用Task防止异常后相互影响
Task.Run(() =>
{
try
{
//创建任务对象
var tp = item.Asm.CreateInstance(item.FullName) as TPlugin;
if (!string.IsNullOrEmpty(tp.XmlConfig.TpError)) { _Alert($"{DateTime.Now.ToString("yyyy/MM/dd HH:mm")}:{tp.XmlConfig.Name} - 异常信息:{tp.XmlConfig.TpError}"); }
else
{
//timer定时器
var timer = new Timer((param) =>
{
var msg = $"{DateTime.Now.ToString("yyyy/MM/dd HH:mm")}:{tp.XmlConfig.Name}";
try
{
var tpObj = param as TPlugin;
//是否关闭暂停任务
if (tpObj.XmlConfig.CloseTask) { return; }
_Alert($"{msg} - 开始执行...{tp.XmlConfig.Timer}分钟一次");
//任务入口
tpObj.TPlugin_Load();
}
catch (Exception ex) { _Alert($"{msg} - 异常信息:{ex.Message}"); }
}, tp, 0, 1000 * 60 * tp.XmlConfig.Timer);
}
}
catch (Exception ex)
{
_Alert($"{DateTime.Now.ToString("yyyy/MM/dd HH:mm")}:{item.Name} - 异常信息:{ex.Message}");
}
});
}
_LoopAlert("正在监控执行的任务,是否退出?(Y / N)");
}
/// <summary>
/// 初始化程序集文件
/// </summary>
private static void _Init()
{
try
{
_Alert("初始化任务中...");
//获取文件
var files = PublicClass._GetPluginFile("");
if (files.Length <= 0) { _Alert("未能找到可用的程序集,请检查配置"); return; }
//读取任务文件
_Alert("读取CrossFiles.xml配置中...");
var baseAddr = Path.Combine(Directory.GetCurrentDirectory(), "PluginXml", "CrossFiles.xml");
var doc = XDocument.Load(baseAddr);
var fileables = files.AsEnumerable();
var taskFiles = new List<FileInfo>();
foreach (var item in doc.Root.Nodes())
{
var crossFile = item.ToString().ToUpper();
var choiceFiles = fileables.Where(b => crossFile.Contains(b.Name.ToUpper()));
if (!choiceFiles.Any()) { continue; }
taskFiles.AddRange(choiceFiles);
}
//展示文件信息
_Alert($"待遍历{taskFiles.Count}个文件信息...");
foreach (var item in taskFiles.OrderBy(b => b.CreationTime))
{
var asmName = new AssemblyName($"{item.Name.Replace(".dll", "")}");
Assembly sm = Assembly.Load(asmName);
if (sm == null) { continue; }
var ts = sm.GetTypes();
//判断特定的任务类,加入任务dic
foreach (var t in ts.Where(b => b.Name != "TPlugin" && b.GetMethod("TPlugin_Load") != null))
{
dicTasks.Add(
t.FullName,
new MoAssembly
{
Asm = sm,
FullName = t.FullName,
Name = t.Name
});
}
}
_Alert($"获取待执行任务量:{dicTasks.Count}个");
}
catch (Exception ex)
{
_Alert($"异常信息:{ ex.Message}");
}
}
/// <summary>
/// 消息提醒
/// </summary>
/// <param name="msg">提示信息</param>
/// <param name="isReadLine">是否需要用户输入指令</param>
/// <returns>用户录入的指令</returns>
private static string _Alert(string msg = "神牛步行3-消息提醒", bool isReadLine = false)
{
Console.WriteLine(msg);
if (isReadLine) { return Console.ReadLine(); }
return "";
}
private static void _LoopAlert(string msg = "是否开始执行任务?(Y/N)")
{
do
{
var readKey = _Alert(msg, true);
if (readKey.ToUpper().Contains("Y")) { break; }
} while (true);
}
}
public class MoAssembly
{
public Assembly Asm { get; set; }
public string FullName { get; set; }
public string Name { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace TaskCore.Plugin
{
/// <summary>
/// 插件基类
/// </summary>
public class TPlugin : IDisposable
{
public TPlugin()
{
XmlConfig = _InitConfig();
}
#region 初始化Xml配置文件 _InitConfig +XmlConfig
/// <summary>
/// xml配置信息
/// </summary>
public XmlConfig XmlConfig;
/// <summary>
/// 初始化配置信息
/// </summary>
/// <param name="configPath">配置文件对应路径</param>
/// <returns></returns>
public virtual XmlConfig _InitConfig(string configPath = "")
{
XmlConfig config = new XmlConfig();
config.Timer = 1;
config.Name = this.GetType().Name;
try
{
if (string.IsNullOrEmpty(configPath))
{
//默认各个dllXml配置
var defaultConfigFolder = "PluginXml";
var baseAddr = Directory.GetCurrentDirectory();
configPath = Path.Combine(baseAddr, defaultConfigFolder, config.Name + ".xml");
}
var doc = XDocument.Load(configPath);
config.doc = doc;
var taskMain = doc.Root;
config.Timer = Convert.ToInt32(taskMain.Element(XName.Get("Timer", "")).Value);
config.Name = taskMain.Element(XName.Get("Name", "")).Value;
config.Des = taskMain.Element(XName.Get("Des", "")).Value;
config.UserName = taskMain.Element(XName.Get("UserName", "")).Value;
config.UserPwd = taskMain.Element(XName.Get("UserPwd", "")).Value;
config.ApiKey = taskMain.Element(XName.Get("ApiKey", "")).Value;
config.ApiUrl = taskMain.Element(XName.Get("ApiUrl", "")).Value;
config.CloseTask = taskMain.Element(XName.Get("CloseTask", "")).Value == "1";
}
catch (Exception ex)
{
config.TpError = ex.Message;
PublicClass._WriteLog($"{config.Name}初始化配置信息异常:{ex.Message}", "BaseLog");
throw new Exception(ex.Message);
}
return config;
}
#endregion
#region 初始化-开始加载 _Load
/// <summary>
/// 初始化-开始起
/// </summary>
public virtual void TPlugin_Load()
{
PublicClass._WriteLog("测试");
}
#endregion
#region 释放资源
public void Dispose()
{
GC.SuppressFinalize(this);//不需要再调用本对象的Finalize方法
}
public virtual void Dispose(Action action)
{
action();
}
#endregion
}
#region 配置文件 XmlConfig
public class XmlConfig
{
public XmlConfig()
{
}
/// <summary>
/// 定制器时间(分钟)
/// </summary>
public int Timer { get; set; }
/// <summary>
/// 运行名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 描述(第一次获取dll描述,后面获取xml配置文件描述)
/// </summary>
public string Des { get; set; }
/// <summary>
/// 接口账号
/// </summary>
public string UserName { get; set; }
/// <summary>
/// 接口密码
/// </summary>
public string UserPwd { get; set; }
/// <summary>
/// 接口秘钥
/// </summary>
public string ApiKey { get; set; }
/// <summary>
/// 接口地址
/// </summary>
public string ApiUrl { get; set; }
/// <summary>
/// 是否关闭任务
/// </summary>
public bool CloseTask { get; set; }
/// <summary>
/// 插件中错误
/// </summary>
public string TpError { get; set; }
/// <summary>
/// xml信息
/// </summary>
public XDocument doc { get; set; }
}
#endregion
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有