我有以下全局过滤器,ISiteValidation 和 ICacheService 通过 Windsor 容器注入并设置为 Transient,因此容器不会自动处理依赖项。当站点投入生产时,这将导致资源问题。那么人们如何正确处理注入过滤器的资源呢?这两个接口都是 IDisposable 但当操作过滤器超出范围时永远不会调用 Dispose 并且容器将继续保留实现。
public class SiteValidationAttribute : ActionFilterAttribute
{
public ISiteValidation SiteValidation { get; set; }
public ICacheService CacheService { get; set; }
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (filterContext.RequestContext.HttpContext.Request.Url != null)
{
string host = filterContext.RequestContext.HttpContext.Request.Url.Host;
try
{
string siteId = CacheService.Get("SiteId",
() =>
SiteValidation.GetSiteId(
host));
var siteIdCookie = new HttpCookie("_site") {Value = siteId};
filterContext.RequestContext.HttpContext.Response.Cookies.Add(siteIdCookie);
}
catch (Exception)
{
throw new HttpException(404, String.Format("This site'{0}' was not found", host));
}
}
base.OnActionExecuted(filterContext);
}
}