【发布时间】:2016-01-26 14:04:24
【问题描述】:
我有一个 asp.net mvc5 网站,它从 api 获取一些图像文件并将它们存储在图像文件夹中。
public async Task<bool> getPhoto64(UserTest user)
{
try
{
if (!File.Exists(Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/images/" + user.IID + ".png"))))
using (var client = new HttpClient())
{
string baseUrl = ConfigurationManager.AppSettings["BaseUrl"];
client.BaseAddress = new Uri(baseUrl);
client.DefaultRequestHeaders.Accept.Clear();
HttpResponseMessage response = await client.GetAsync("user/image?userIid=" + user.IID);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
Stream st = await response.Content.ReadAsStreamAsync();
byte[] bytes = new byte[(int)st.Length];
st.Read(bytes, 0, (int)st.Length);
if (bytes != null)
{
if (!File.Exists(Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/images/" + user.IID + ".png"))))
File.WriteAllBytes(Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/images/" + user.IID + ".png")), bytes);
}
}
}
}
catch (Exception e)
{
logger.Error(e);
return false;
}
return true;
}
现在当用户连接到网站时我会这样做,但我想知道网站是否有办法自动触发此方法,比如说每天 00:00 一次。
【问题讨论】:
-
我不认为这是关于 MVC 的,看看how to schedule a task in ASP.NET。
-
-
这是一个服务应用程序的工作
-
使用窗口执行调度任务调度程序可以使用在午夜运行一次的控制台应用程序进行管理。
标签: c# asp.net asp.net-mvc