jgauffin / griffin.mvccontrib

A contribution project for ASP.NET MVC3
http://blog.gauffin.org/tag/griffin-mvccontrib/
GNU Lesser General Public License v3.0
83 stars 40 forks source link

GriffinCaching : Closed Connection #47

Open KingR1 opened 10 years ago

KingR1 commented 10 years ago

I am using MS Sql 2012. Some times i get error "ExecuteScalar requires an open and available Connection. The connection's current state is closed." This error i get in subclass of ViewLocalizer and RepositoryStringProvider :

// Caching view texts
public class CachedViewLocalizer : ViewLocalizer
{
    private readonly ILocalizedViewsCache _cache;

    public CachedViewLocalizer(IViewLocalizationRepository repository, ILocalizedViewsCache cache) : base(repository) 
    {
        _cache = cache;
    }

    public override string Translate(string viewPath, RouteData routeData, string text)
    {
        string prompt;
        if (_cache.TryGetValue(viewPath, routeData, text, out prompt))
            return prompt;

        if (prompt != null)
        {
            prompt = base.Translate(viewPath, routeData, text);// THIS LINE THROW ERROR
        }

        _cache.Insert(viewPath, routeData, text, prompt);
        return prompt;
    }
}

// caching type translations
public class CachedTypeLocalizer : RepositoryStringProvider
{
    private readonly ILocalizedTypesCache _cache;

    public CachedTypeLocalizer(ILocalizedTypesRepository repository, ILocalizedTypesCache cache) : base(repository)
    {
        _cache = cache;
    }

    protected override string Translate(Type type, string name)
    {
        var promptName = type.FullName + "." + name;

        string prompt;
        if (_cache.TryGetValue(promptName, out prompt))
                return prompt;

        prompt = base.Translate(type, name);
        if (prompt != null)
        {
            _cache.Insert(promptName, prompt);    
        }

        return prompt;
    }
}

After re-build of project everything works again. So i think, Griffin.MvcContrib close connection.

Maybe some suggestions? Thanks, Roman