Castle Windsor Singleton在每次呼叫解析时实例化

时间:2011-07-26 23:01:07

标签: castle-windsor

我在ASP.NET 4.0 Web应用程序(不是ASP.NET MVC)中使用Castle Windsor 2.5.3

我有一个拦截器,用于拦截对数据访问组件的调用。拦截器依赖于缓存管理器。如果缓存管理器具有所需的数据,则拦截器使用缓存管理器来避免调用数据访问组件。

即使缓存管理器注册为Singleton,它也会被多次实例化。我可以在其默认构造函数中使用调试消息或命中计数断点来证明这一点。

新的要求是缓存可以按需清除,因此我认为解决缓存管理器和调用EmptyCache是​​一件简单的事情。发生的事情是容器正在创建一个新的Cache Manager实例,对EmptyCache的调用没有效果(因为新的缓存管理器没有缓存数据)。以下是清除缓存的网页代码:

protected void flushButton_Click(object sender, EventArgs e)
{
    ICacheManager cacheManager = null;

    try
    {
        cacheManager = Global.Container.Resolve<ICacheManager>();
        cacheManager.EmptyCache();
        resultLabel.Text = "Cache has been flushed";
    }
    catch (Exception ex)
    {
        resultLabel.Text = "An error occurred. The reason given was: " + ex.Message;
    }
    finally
    {
        if (cacheManager != null)
            Global.Container.Release(cacheManager);
    }
}

当我将鼠标悬停在Visual Studio中的Container上并钻入组件时,CacheManager被标记为Singleton。怎么会发生这种情况?

我的缓存管理器注册如下:

public class WindsorComponentInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(
            Component.For(typeof(Data.Common.Cache.ICacheManager))
                .ImplementedBy(typeof(Data.Common.Cache.CacheManager))
                .LifeStyle.Singleton
        );
        container.Register(
            Component.For<Data.Common.CachingInterceptor>()
        );
    }
}

缓存管理器界面如下所示:

public interface ICacheManager 
{
    object CacheItem(string cacheKey, DateTime absoluteExpiration, CacheItemPriority priority, Func<object> itemProvider);
    object CacheItem(string cacheKey, TimeSpan slidingExpiration, CacheItemPriority priority, Func<object> itemProvider);
    void EmptyCache();
}

拦截器如下所示:

public class CachingInterceptor : IInterceptor
{
    private ILogger logger = NullLogger.Instance;
    private ICacheManager cacheManager;

    public CachingInterceptor(ICacheManager cacheManager)
    {
        this.cacheManager = cacheManager;
    }

    public ILogger Logger
    {
        set
        {
            if (value != null) logger = value;
        }
    }

    public void Intercept(IInvocation invocation)
    {
        try
        {
            string cacheItemKey = MakeCacheItemKey(invocation);
            //Debug.WriteLine("Cache Key: {0}", cacheItemKey);
            TimeSpan lifespan = TimeSpan.Parse("00:20:00");
            bool cacheHit = true;

            object result = cacheManager.CacheItem(cacheItemKey, lifespan, CacheItemPriority.Low,
                () =>
                {
                    invocation.Proceed();
                    //Debug.WriteLine(String.Format("populate-the-cache callback was invoked and returned a {0}", invocation.ReturnValue ?? "null"));
                    cacheHit = false;
                    return invocation.ReturnValue;
                }
            );

            logger.DebugFormat("Interceptor {0} Cache Hit: {1}", (invocation.Method.Name ?? "null"), cacheHit.ToString());
            invocation.ReturnValue = result;
        }
        catch (Exception ex)
        {
            logger.Error("Intercept Error", ex);
        }
    }

    private string MakeCacheItemKey(IInvocation invocation)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append(invocation.InvocationTarget);
        sb.Append("|" + invocation.MethodInvocationTarget.Name);
        sb.Append("|" + invocation.MethodInvocationTarget.ReturnType);

        foreach (ParameterInfo pi in invocation.MethodInvocationTarget.GetParameters())
            sb.Append("|" + pi.ParameterType.ToString());

        foreach (var arg in invocation.Arguments)
        {
            sb.Append("|");
            sb.Append(arg ?? "null");
        }

        return sb.ToString();   
    }
}

数据组件的注册如下:

    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        string connStr = ConfigurationManager.ConnectionStrings["Database"].ConnectionString;

        container.Register(
            Component.For<IActualCostsVersusBudgetDataProvider>()
                .ImplementedBy<ActualCostsVersusBudgetDataProvider>()
                .DependsOn(Property.ForKey("connectionString").Eq(connStr))
                .LifeStyle.Transient
                .Interceptors(InterceptorReference.ForType<CachingInterceptor>())
                .Anywhere
        );
        /* Many calls to .Register omitted */
    }

依赖于数据提供者的业务对象的注册方式如下:

public void Install(IWindsorContainer container, IConfigurationStore store)
{
    container.Register(
        AllTypes.FromThisAssembly()
            .Where(t => t.Name.EndsWith("Manager"))
            .Configure(c => c.LifeStyle.Transient)
    );
}

容器在global.asax中初始化为:

public static IWindsorContainer Container { get; private set; }

public Global()
{
    Container = BootstrapContainer();
}

private IWindsorContainer BootstrapContainer()
{
    WindsorContainer container = new WindsorContainer();

    container.AddFacility<LoggingFacility>(f => f.LogUsing(LoggerImplementation.Log4net).WithAppConfig());

    container.Install(
        new Data.Common.Installers.WindsorComponentInstaller(),
        new Data.Installers.WindsorComponentInstaller(),
        new Business.Installers.WindsorComponentInstaller()
    );

    return container;
}

0 个答案:

没有答案