chore: 首次簽入 Thinkyu ASP.NET 專案
- 加入 Visual Studio / ASP.NET .gitignore - 排除建置輸出、IDE 設定、NuGet packages、大型 MSI 安裝檔
This commit is contained in:
@@ -0,0 +1,269 @@
|
||||
using System;
|
||||
using System.Web;
|
||||
using System.Configuration;
|
||||
using System.Web.Configuration;
|
||||
using System.Timers;
|
||||
using WebLib;
|
||||
|
||||
namespace 報到系統
|
||||
{
|
||||
public class Global : System.Web.HttpApplication
|
||||
{
|
||||
private Timer _smsSendingTimer;
|
||||
private int _smsSendingIntervalMinutes = 1; // 預設每1分鐘執行一次
|
||||
|
||||
protected void Application_Start(object sender, EventArgs e)
|
||||
{
|
||||
StringTable.Insert = false;
|
||||
StringTable.ByPass = true;
|
||||
|
||||
#region 讀進 Web.Config 中的系統參數
|
||||
|
||||
if (ConfigurationManager.AppSettings["ConnectionString"] != null)
|
||||
PublicVariable.ConnectionString = ConfigurationManager.ConnectionStrings[ConfigurationManager.AppSettings["ConnectionString"]].ConnectionString;
|
||||
else
|
||||
PublicVariable.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
|
||||
|
||||
DAC.ConnectionString = DAC.connTypeMSSQL;
|
||||
DAC.ConnectionString = PublicVariable.ConnectionString;
|
||||
|
||||
if (ConfigurationManager.AppSettings["UserLanguage"] != null)
|
||||
PublicVariable.UserLanguage = ConfigurationManager.AppSettings["UserLanguage"];
|
||||
|
||||
if (ConfigurationManager.AppSettings["CompanyName"] != null)
|
||||
PublicVariable.CompanyName = ConfigurationManager.AppSettings["CompanyName"];
|
||||
|
||||
if (ConfigurationManager.AppSettings["CopyrightText"] != null)
|
||||
PublicVariable.CopyrightText = ConfigurationManager.AppSettings["CopyrightText"];
|
||||
|
||||
if (ConfigurationManager.AppSettings["Theme"] != null)
|
||||
PublicVariable.Theme = ConfigurationManager.AppSettings["Theme"].ToLower();
|
||||
|
||||
if (ConfigurationManager.AppSettings["SystemName"] != null)
|
||||
PublicVariable.SystemName = ConfigurationManager.AppSettings["SystemName"].ToLower();
|
||||
|
||||
if (ConfigurationManager.AppSettings["NoLoginFunction"] != null)
|
||||
PublicVariable.NoLoginFunction = Convert.ToBoolean(ConfigurationManager.AppSettings["NoLoginFunction"].ToLower());
|
||||
|
||||
#region 發郵件設定
|
||||
if (ConfigurationManager.AppSettings["SendMailBySSL"] != null)
|
||||
PublicVariable.SendMailBySSL = Convert.ToBoolean(ConfigurationManager.AppSettings["SendMailBySSL"].ToLower());
|
||||
|
||||
if (ConfigurationManager.AppSettings["SendMailHost"] != null)
|
||||
PublicVariable.SendMailHost = ConfigurationManager.AppSettings["SendMailHost"];
|
||||
|
||||
if (ConfigurationManager.AppSettings["SendMailPort"] != null)
|
||||
PublicVariable.SendMailPort = Convert.ToInt32(ConfigurationManager.AppSettings["SendMailPort"]);
|
||||
|
||||
if (ConfigurationManager.AppSettings["SendMailUserName"] != null)
|
||||
PublicVariable.SendMailUserName = ConfigurationManager.AppSettings["SendMailUserName"];
|
||||
|
||||
if (ConfigurationManager.AppSettings["SendMailPassword"] != null)
|
||||
PublicVariable.SendMailPassword = ConfigurationManager.AppSettings["SendMailPassword"];
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
#region 設定相關服務的參數
|
||||
|
||||
MailService.Host = PublicVariable.SendMailHost;
|
||||
MailService.Port = PublicVariable.SendMailPort;
|
||||
MailService.UserName = PublicVariable.SendMailUserName;
|
||||
MailService.Password = PublicVariable.SendMailPassword;
|
||||
MailService.UseSSL = PublicVariable.SendMailBySSL;
|
||||
MailService.LogPath = Server.MapPath("~/Log");
|
||||
MailService.Enabled = true;
|
||||
|
||||
DeleteFileService.DeletePatterns.Add(Server.MapPath("~/Upload") + "/*.mdb");
|
||||
DeleteFileService.Enabled = true;
|
||||
|
||||
#endregion
|
||||
|
||||
#region 設定 DEBUG 模式
|
||||
CompilationSection section = ConfigurationManager.GetSection("system.web/compilation") as CompilationSection;
|
||||
PublicVariable.DEBUG = section.Debug;
|
||||
#endregion
|
||||
|
||||
#region 從資料庫讀取系統設定
|
||||
try
|
||||
{
|
||||
using (var conn = DAC.NewConnection())
|
||||
{
|
||||
// 讀取目前使用的簡訊平台代碼
|
||||
var dacBwex = new DAC_BWEX(conn);
|
||||
string currentPlatform = dacBwex.GetKeyValue("CurrentMMSPlatform");
|
||||
if (!string.IsNullOrEmpty(currentPlatform))
|
||||
PublicVariable.CurrentMMSPlatform = currentPlatform;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("[Global] 讀取系統設定失敗:" + ex.Message);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 啟動簡訊發送服務
|
||||
InitializeSMSSendingService();
|
||||
#endregion
|
||||
}
|
||||
|
||||
protected void Session_Start(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected void Application_BeginRequest(object sender, EventArgs e)
|
||||
{
|
||||
HttpRuntimeSection section = (HttpRuntimeSection)ConfigurationManager.GetSection("system.web/httpRuntime");
|
||||
int maxFileSize = section.MaxRequestLength * 1024;
|
||||
if (Request.ContentLength > maxFileSize)
|
||||
{
|
||||
Server.ClearError();
|
||||
Response.Redirect("~/Forms/Common/FileTooLarge.aspx");
|
||||
}
|
||||
}
|
||||
|
||||
protected void Application_AuthenticateRequest(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private string Message1 = @"錯誤網頁:{0}
|
||||
錯誤類別:{1}
|
||||
錯誤來源:{2}
|
||||
錯誤訊息:{3}
|
||||
堆疊內容:
|
||||
{4}
|
||||
|
||||
";
|
||||
|
||||
private string Message2 = @"以上的錯誤發生的原因是:
|
||||
錯誤類別:{0}
|
||||
錯誤來源:{1}
|
||||
錯誤訊息:{2}
|
||||
堆疊內容:
|
||||
{3}
|
||||
|
||||
";
|
||||
protected void Application_Error(object sender, EventArgs e)
|
||||
{
|
||||
// 發生未處理錯誤時執行的程式碼
|
||||
string Message = "";
|
||||
Exception ex = Server.GetLastError();
|
||||
|
||||
if (ex == null)
|
||||
{
|
||||
Server.ClearError();
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果是 404,重導到 404 頁面
|
||||
if (ex is HttpException && (ex as HttpException).GetHttpCode() == 404)
|
||||
{
|
||||
Server.ClearError();
|
||||
Response.Redirect("~/Forms/404.aspx");
|
||||
return;
|
||||
}
|
||||
|
||||
Message += String.Format(Message1,
|
||||
Request.Path,
|
||||
ex.GetType().FullName,
|
||||
ex.Source,
|
||||
ex.Message,
|
||||
ex.StackTrace);
|
||||
|
||||
ex = ex.InnerException;
|
||||
while (ex != null)
|
||||
{
|
||||
Message += String.Format(Message2,
|
||||
ex.GetType().FullName,
|
||||
ex.Source,
|
||||
ex.Message,
|
||||
ex.StackTrace);
|
||||
ex = ex.InnerException;
|
||||
}
|
||||
|
||||
// 建立Log目錄
|
||||
if (!System.IO.Directory.Exists(Server.MapPath("~/Log")))
|
||||
{
|
||||
System.IO.Directory.CreateDirectory(Server.MapPath("~/Log"));
|
||||
}
|
||||
|
||||
// 寫入文字檔
|
||||
string fileName = DateTime.UtcNow.AddHours(8).ToString("yyyyMMdd_HHmmss");
|
||||
System.IO.File.AppendAllText(Server.MapPath(string.Format(@"~/Log/{0}.txt", fileName)), Message);
|
||||
|
||||
// 清除Error
|
||||
Server.ClearError();
|
||||
|
||||
Response.Redirect(String.Format("~/Forms/500.aspx?errcode={0}", fileName));
|
||||
}
|
||||
|
||||
protected void Session_End(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected void Application_End(object sender, EventArgs e)
|
||||
{
|
||||
// 停止簡訊發送 Timer
|
||||
if (_smsSendingTimer != null)
|
||||
{
|
||||
_smsSendingTimer.Stop();
|
||||
_smsSendingTimer.Dispose();
|
||||
_smsSendingTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化簡訊發送服務
|
||||
/// </summary>
|
||||
private void InitializeSMSSendingService()
|
||||
{
|
||||
try
|
||||
{
|
||||
// 從 Web.Config 讀取發送間隔設定(預設為 1 分鐘)
|
||||
if (ConfigurationManager.AppSettings["SMSSendingIntervalMinutes"] != null)
|
||||
{
|
||||
if (int.TryParse(ConfigurationManager.AppSettings["SMSSendingIntervalMinutes"], out int interval))
|
||||
{
|
||||
_smsSendingIntervalMinutes = interval;
|
||||
}
|
||||
}
|
||||
|
||||
// 建立 Timer
|
||||
if (_smsSendingTimer == null)
|
||||
_smsSendingTimer = new Timer();
|
||||
_smsSendingTimer.Interval = _smsSendingIntervalMinutes * 60 * 1000; // 轉換為毫秒
|
||||
_smsSendingTimer.Elapsed += SMSSendingTimer_Elapsed;
|
||||
_smsSendingTimer.AutoReset = true;
|
||||
_smsSendingTimer.Enabled = true;
|
||||
|
||||
System.Diagnostics.Debug.WriteLine(
|
||||
string.Format("[SMSSendingService] 已啟動,發送間隔:{0} 分鐘", _smsSendingIntervalMinutes));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine(
|
||||
string.Format("[SMSSendingService] 初始化失敗:{0}", ex.Message));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Timer Elapsed 事件處理器
|
||||
/// </summary>
|
||||
private void SMSSendingTimer_Elapsed(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 呼叫簡訊發送服務
|
||||
SMSSendingService.Instance.Start();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine(
|
||||
string.Format("[SMSSendingService] 執行失敗:{0}", ex.Message));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user