次のグローバル フィルターがあります。ISiteValidation と ICacheService は Windsor コンテナーを介して注入され、Transient として設定されているため、コンテナーは依存関係を自動的に破棄しません。これにより、サイトが本番環境に入ったときにリソースの問題が発生します。では、フィルターに注入されたリソースを適切に破棄するために、人々は何をしているのでしょうか? 両方のインターフェイスは IDisposable ですが、Action Filter がスコープ外になったときに 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);
}
}