From 2c00f5902f5fad1a104896877cb044a24c4c2e10 Mon Sep 17 00:00:00 2001
From: falcon <9504402@qq.com>
Date: Fri, 22 Mar 2019 15:53:23 +0800
Subject: [PATCH] =?UTF-8?q?(#15)=E5=8C=BB=E7=96=97=E6=9C=BA=E6=9E=84?=
=?UTF-8?q?=E5=89=8D=E7=BD=AE=E6=9C=BA=E5=A2=9E=E5=8A=A0IOC=E6=A8=A1?=
=?UTF-8?q?=E5=9D=97=EF=BC=8C=E6=95=B0=E6=8D=AE=E5=BA=93=E6=A8=A1=E5=9D=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Cmdjy/CmdjyHisFront/Bll/BackgroundTask.cs | 119 ++++++++++++++++++
.../Cmdjy/CmdjyHisFront/CmdjyHisFront.csproj | 21 ++++
.../CmdjyHisFront/CommonClass.Factory.Log.txt | 2 +
.../Cmdjy/CmdjyHisFront/Dal/DbConfig.cs | 12 ++
.../CmdjyHisFront/Dal/HisFrontDbContext.cs | 12 ++
.../Cmdjy/CmdjyHisFront/Global.asax.cs | 7 ++
WebSiteCode/Cmdjy/CmdjyHisFront/IocFactory.cs | 28 +++++
WebSiteCode/Cmdjy/CmdjyHisFront/Web.config | 94 ++++++++------
.../Cmdjy/CmdjyHisFront/WebSettings.cs | 30 +++++
.../Cmdjy/CmdjyHisFront/packages.config | 4 +
10 files changed, 289 insertions(+), 40 deletions(-)
create mode 100644 WebSiteCode/Cmdjy/CmdjyHisFront/Bll/BackgroundTask.cs
create mode 100644 WebSiteCode/Cmdjy/CmdjyHisFront/CommonClass.Factory.Log.txt
create mode 100644 WebSiteCode/Cmdjy/CmdjyHisFront/Dal/DbConfig.cs
create mode 100644 WebSiteCode/Cmdjy/CmdjyHisFront/Dal/HisFrontDbContext.cs
create mode 100644 WebSiteCode/Cmdjy/CmdjyHisFront/IocFactory.cs
create mode 100644 WebSiteCode/Cmdjy/CmdjyHisFront/WebSettings.cs
diff --git a/WebSiteCode/Cmdjy/CmdjyHisFront/Bll/BackgroundTask.cs b/WebSiteCode/Cmdjy/CmdjyHisFront/Bll/BackgroundTask.cs
new file mode 100644
index 0000000..0124518
--- /dev/null
+++ b/WebSiteCode/Cmdjy/CmdjyHisFront/Bll/BackgroundTask.cs
@@ -0,0 +1,119 @@
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using System.Timers;
+using Autofac;
+using CommonClass.Factory;
+
+namespace CmdjyHisFront.Bll
+{
+ public interface ITask
+ {
+ void Run();
+ }
+ ///
+ /// 任务管理器
+ ///
+ public interface ITaskManager
+ {
+ ///
+ /// 心跳间隔。毫秒
+ ///
+ double BackgroundTaskHeartbeat { get; set; }
+
+ ///
+ /// 执行任务
+ ///
+ void Start();
+ ///
+ /// 结束任务
+ ///
+ void Stop();
+ }
+ ///
+ /// 背景任务注册管理器
+ ///
+ public class TaskManagerRegister:IRegister
+ {
+ public void Register(ContainerBuilder builder) {
+ builder.Register(c => new TaskManager());
+ }
+ }
+
+ ///
+ /// 背景任务管理器
+ ///
+ public class TaskManager:ITaskManager
+ {
+ ///
+ /// 任务心跳触发计时器
+ ///
+ public static Timer CTimer { get; private set; }
+
+ ///
+ /// 要执行的背景线程
+ ///
+ public IEnumerable BackTasks { get; set; }
+
+ public double BackgroundTaskHeartbeat { get; set; } = 1000;
+
+ ///
+ /// 创建一个背景任务管理器
+ ///
+ public TaskManager() { }
+
+ ///
+ /// 通过提供任务和背景心跳间隔创建背景任务管理器
+ ///
+ /// 背景任务枚举
+ /// 心跳
+ public TaskManager(IEnumerable bt) {
+ this.BackTasks = bt;
+ }
+
+ ///
+ /// 通过提供任务和背景心跳间隔创建背景任务管理器
+ ///
+ /// 背景任务枚举
+ /// 心跳
+ public TaskManager(IEnumerable bt,double bh) {
+ this.BackTasks = bt; this.BackgroundTaskHeartbeat = bh;
+ }
+
+ ///
+ /// 开始执行背景任务
+ ///
+ public void Start() {
+ if(this.BackTasks == null || this.BackTasks.Count() == 0) return;
+ CTimer = CTimer ?? new Timer(this.BackgroundTaskHeartbeat);
+ CTimer.Elapsed += this.run;
+ CTimer.AutoReset = false;
+ CTimer.Start();
+ }
+
+ ///
+ /// 任务处理事件
+ ///
+ private void run(object sender,ElapsedEventArgs e) {
+ //启动具体任务
+ foreach(var t in this.BackTasks) {
+ Task.Factory.StartNew(m => {
+ if(m is ITask task) {
+ task.Run();
+ }
+ },t as object);
+ }
+ //任务启动后重启心跳计时器
+ if(sender is Timer timer) {
+ timer.Start();
+ }
+ }
+
+ public void Stop() {
+ if(CTimer == null) return;
+ CTimer.Stop();
+ CTimer.Elapsed -= run;
+ CTimer = null;
+ }
+ }
+}
\ No newline at end of file
diff --git a/WebSiteCode/Cmdjy/CmdjyHisFront/CmdjyHisFront.csproj b/WebSiteCode/Cmdjy/CmdjyHisFront/CmdjyHisFront.csproj
index de59dce..6fc496d 100644
--- a/WebSiteCode/Cmdjy/CmdjyHisFront/CmdjyHisFront.csproj
+++ b/WebSiteCode/Cmdjy/CmdjyHisFront/CmdjyHisFront.csproj
@@ -45,6 +45,21 @@
4
+
+ ..\packages\Autofac.4.9.1\lib\net45\Autofac.dll
+
+
+ ..\packages\Autofac.Mvc5.4.0.2\lib\net45\Autofac.Integration.Mvc.dll
+
+
+ ..\packages\CommonClass.Factory.1.2.0.8\lib\net45\CommonClass.Factory.dll
+
+
+ ..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.dll
+
+
+ ..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.SqlServer.dll
+
@@ -147,13 +162,19 @@
+
+
+
Global.asax
+
+
+
diff --git a/WebSiteCode/Cmdjy/CmdjyHisFront/CommonClass.Factory.Log.txt b/WebSiteCode/Cmdjy/CmdjyHisFront/CommonClass.Factory.Log.txt
new file mode 100644
index 0000000..426a96c
--- /dev/null
+++ b/WebSiteCode/Cmdjy/CmdjyHisFront/CommonClass.Factory.Log.txt
@@ -0,0 +1,2 @@
+V8 2018年1月8日
+1、特性注入支持属性注入。接口注入方式暂不支持。
\ No newline at end of file
diff --git a/WebSiteCode/Cmdjy/CmdjyHisFront/Dal/DbConfig.cs b/WebSiteCode/Cmdjy/CmdjyHisFront/Dal/DbConfig.cs
new file mode 100644
index 0000000..80fca20
--- /dev/null
+++ b/WebSiteCode/Cmdjy/CmdjyHisFront/Dal/DbConfig.cs
@@ -0,0 +1,12 @@
+using System.Data.Entity.Migrations;
+
+namespace CmdjyHisFront.Dal
+{
+ public class DbConfig:DbMigrationsConfiguration
+ {
+ public DbConfig() {
+ AutomaticMigrationsEnabled = WebSettings.AutoMigrations;
+ AutomaticMigrationDataLossAllowed = false;
+ }
+ }
+}
\ No newline at end of file
diff --git a/WebSiteCode/Cmdjy/CmdjyHisFront/Dal/HisFrontDbContext.cs b/WebSiteCode/Cmdjy/CmdjyHisFront/Dal/HisFrontDbContext.cs
new file mode 100644
index 0000000..bb9a7e7
--- /dev/null
+++ b/WebSiteCode/Cmdjy/CmdjyHisFront/Dal/HisFrontDbContext.cs
@@ -0,0 +1,12 @@
+using System.Data.Entity;
+
+namespace CmdjyHisFront.Dal
+{
+ ///
+ /// HIS前置机数据库h
+ ///
+ public partial class HisFrontDbContext:DbContext
+ {
+ public HisFrontDbContext() : base("HisFrontDbContext") { }
+ }
+}
\ No newline at end of file
diff --git a/WebSiteCode/Cmdjy/CmdjyHisFront/Global.asax.cs b/WebSiteCode/Cmdjy/CmdjyHisFront/Global.asax.cs
index c4dc2d0..6b65e8b 100644
--- a/WebSiteCode/Cmdjy/CmdjyHisFront/Global.asax.cs
+++ b/WebSiteCode/Cmdjy/CmdjyHisFront/Global.asax.cs
@@ -1,16 +1,23 @@
using System;
using System.Collections.Generic;
+using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
+using Autofac.Integration.Mvc;
+using CmdjyHisFront.Dal;
namespace CmdjyHisFront
{
public class MvcApplication:System.Web.HttpApplication
{
protected void Application_Start() {
+ Database.SetInitializer(new MigrateDatabaseToLatestVersion());
+ //配置autofac依赖注入
+ DependencyResolver.SetResolver(new AutofacDependencyResolver(IocFactory.Factory));
+
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
diff --git a/WebSiteCode/Cmdjy/CmdjyHisFront/IocFactory.cs b/WebSiteCode/Cmdjy/CmdjyHisFront/IocFactory.cs
new file mode 100644
index 0000000..15e9002
--- /dev/null
+++ b/WebSiteCode/Cmdjy/CmdjyHisFront/IocFactory.cs
@@ -0,0 +1,28 @@
+using Autofac;
+using Autofac.Integration.Mvc;
+using CommonClass.Factory;
+
+namespace CmdjyHisFront
+{
+ public class IocFactory
+ {
+ private static IOCFactory _iocFactory = null;
+ ///
+ /// 控制反转容器工厂
+ ///
+ public static ILifetimeScope Factory {
+ get {
+ if(_iocFactory == null) {
+ _iocFactory = new IOCFactory();
+ _iocFactory.BeforeBuild += _iocFactory_BeforeBuild;
+ _iocFactory.Init();
+ }
+ return _iocFactory.Container.BeginLifetimeScope();
+ }
+ }
+
+ private static void _iocFactory_BeforeBuild(IOCFactory arg1,ContainerBuilder arg2) {
+ arg2.RegisterControllers();
+ }
+ }
+}
\ No newline at end of file
diff --git a/WebSiteCode/Cmdjy/CmdjyHisFront/Web.config b/WebSiteCode/Cmdjy/CmdjyHisFront/Web.config
index cef5a52..8f50f4a 100644
--- a/WebSiteCode/Cmdjy/CmdjyHisFront/Web.config
+++ b/WebSiteCode/Cmdjy/CmdjyHisFront/Web.config
@@ -4,75 +4,89 @@
https://go.microsoft.com/fwlink/?LinkId=301880
-->
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
-
-
+
+
-
+
-
-
+
+
-
-
-
-
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
-
+
-
-
+
+
-
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/WebSiteCode/Cmdjy/CmdjyHisFront/WebSettings.cs b/WebSiteCode/Cmdjy/CmdjyHisFront/WebSettings.cs
new file mode 100644
index 0000000..c35962f
--- /dev/null
+++ b/WebSiteCode/Cmdjy/CmdjyHisFront/WebSettings.cs
@@ -0,0 +1,30 @@
+using System.Configuration;
+
+namespace CmdjyHisFront
+{
+ ///
+ /// 网站配置
+ ///
+ public static class WebSettings
+ {
+ ///
+ /// 获取配置的值
+ ///
+ public static string GetValue(string key) {
+ var val = ConfigurationManager.AppSettings[key];
+ return val;
+ }
+ ///
+ /// 自动升级数据库
+ ///
+ public static bool AutoMigrations {
+ get {
+ var val = GetValue("AutoMigrations");
+ if(bool.TryParse(val,out bool v)) {
+ return v;
+ }
+ return true;
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/WebSiteCode/Cmdjy/CmdjyHisFront/packages.config b/WebSiteCode/Cmdjy/CmdjyHisFront/packages.config
index 1e75bba..a60057e 100644
--- a/WebSiteCode/Cmdjy/CmdjyHisFront/packages.config
+++ b/WebSiteCode/Cmdjy/CmdjyHisFront/packages.config
@@ -1,7 +1,11 @@
+
+
+
+