66 lines
2.2 KiB
C#
66 lines
2.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using System.Web.Http;
|
|
using System.Web.Mvc;
|
|
using System.Web.Optimization;
|
|
using System.Web.Routing;
|
|
|
|
namespace ChatRoomServer.Www
|
|
{
|
|
using System.IO;
|
|
using System.Timers;
|
|
using ChatRoomServer.Www.Areas.Web.Models;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Serialization;
|
|
|
|
public class MvcApplication : System.Web.HttpApplication
|
|
{
|
|
Timer _cacheUpdateCacheTimer;
|
|
public static DateTime CacheUpdateStartTime;
|
|
public static DateTime LastCacheUpdateTime;
|
|
static string _hostFile;
|
|
public static HostResolveCache HostResolveCache { get; private set; }
|
|
|
|
static MvcApplication()
|
|
{
|
|
_hostFile = System.Web.Hosting.HostingEnvironment.MapPath("~/data/12306host.config");
|
|
if (File.Exists(_hostFile))
|
|
HostResolveCache = Newtonsoft.Json.JsonConvert.DeserializeObject<HostResolveCache>(System.IO.File.ReadAllText(_hostFile));
|
|
else HostResolveCache = new HostResolveCache();
|
|
}
|
|
|
|
protected void Application_Start()
|
|
{
|
|
AreaRegistration.RegisterAllAreas();
|
|
GlobalConfiguration.Configure(WebApiConfig.Register);
|
|
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
|
|
RouteConfig.RegisterRoutes(RouteTable.Routes);
|
|
BundleConfig.RegisterBundles(BundleTable.Bundles);
|
|
|
|
//set json naming method
|
|
var cfg = GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings;
|
|
cfg.Formatting = Formatting.None;
|
|
cfg.ContractResolver = new CamelCasePropertyNamesContractResolver();
|
|
RouteTable.Routes.RouteExistingFiles = false;
|
|
RouteTable.Routes.AppendTrailingSlash = true;
|
|
RouteTable.Routes.LowercaseUrls = true;
|
|
|
|
//定时器
|
|
_cacheUpdateCacheTimer = new Timer(1000 * 60 * 10);
|
|
_cacheUpdateCacheTimer.AutoReset = true;
|
|
_cacheUpdateCacheTimer.Elapsed += UpdateCacheToDb;
|
|
_cacheUpdateCacheTimer.Start();
|
|
CacheUpdateStartTime = DateTime.Now;
|
|
}
|
|
|
|
void UpdateCacheToDb(object sender, EventArgs e)
|
|
{
|
|
LastCacheUpdateTime = DateTime.Now;
|
|
System.IO.File.Copy(_hostFile, _hostFile + DateTime.Now.ToString("yyyy-MM-dd") + ".bak", true);
|
|
System.IO.File.WriteAllText(_hostFile, JsonConvert.SerializeObject(HostResolveCache));
|
|
}
|
|
}
|
|
}
|