我认为最好的方法是假设您开发了一个真正的 NT 服务并使用HttpClient向您的局部视图发送一个 http 请求并接收作为字符串的响应并使用它来组成您的电子邮件。但是,您可以通过HttpContext
在类RunScheduledTasks
中进行一些更改来获得方法。Scheduler
public delegate void Callback();
到
public delegate void Callback(HttpContext httpContext);
添加cache.Current_HttpContext = HttpContext.Current;
到运行方法
public static void Run(string name, int minutes, Callback callbackMethod)
{
_numberOfMinutes = minutes;
CacheItem cache = new CacheItem();
cache.Name = name;
cache.Callback = callbackMethod;
cache.Cache = HttpRuntime.Cache;
cache.LastRun = DateTime.Now;
cache.Current_HttpContext = HttpContext.Current;
AddCacheObject(cache);
}
更改CacheCallback
为
private static void CacheCallback(string key, object value, CacheItemRemovedReason reason)
{
CacheItem obj_cache = (CacheItem)value;
if (obj_cache.LastRun < DateTime.Now)
{
if (obj_cache.Callback != null)
{
obj_cache.Callback.Invoke(obj_cache.Current_HttpContext);
}
obj_cache.LastRun = DateTime.Now;
}
AddCacheObject(obj_cache);
}
编辑:
如何使用HttpClient
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync("http://localhost/controller/action/");
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();