Мой код запускается при первом обращении к нему (вы можете запустить это приложение, добавив пару строк в ваш файл global.asax).Он хранит кэшированные данные в кэше ASP.Net в течение 12 часов.По истечении 12 часов кеш будет обновлен при следующем доступе к данным.
public class UserInfo
{
public static List<UserInfo> UserInfoCache {
get{
if(HttpContext.Current.Cache["CachedUserList"] == null) {
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
PrincipalSearcher ps = new PrincipalSearcher(new UserPrincipal(ctx) { SamAccountName = "*" });
var users = ps.FindAll();
var result = from c in users
let u = (UserPrincipal)c
select new UserInfo() { Email = u.EmailAddress, FirstName = u.GivenName, LastName = u.Surname };
HttpContext.Current.Cache.Insert("CachedUserList", result, null, DateTime.Now.AddHours(12), System.Web.Caching.Cache.NoSlidingExpiration);
}
return (List<UserInfo>)HttpContext.Current.Cache["CachedUserList"];
}
}
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}