Invalid operation. The connection is closed. ASP.NET MVC

This does not happen often, but from time to time I get an exception report sent by e-mail indicating this bit of code. I am wondering if you see something wrong with the following code. I can't make it fail locally, and tracking data with breakpoints always gives the right results, step by step.

namespace DomainModel.Concrete
{
public class ConfigRepository : IConfigRepository
{
    static mvCmsContext context { get; set; }
    public ConfigRepository() { context = new mvCmsContext(); }

    private static Func<mvCmsContext, string, Configuration> _byName =
        CompiledQuery.Compile((mvCmsContext context, string configName) =>
            (from c in context.Configs
             where c.configName == configName
             select c).SingleOrDefault());
    static public Configuration ByName(string configName)
    {
        var result = (Configuration)HttpContext.Current.Cache.Get(configName);
        if (result == null)
        {
            using (new mvCmsContext())
            {
                HttpContext.Current.Cache.Insert(configName, _byName(context, configName));
                result = (Configuration)HttpContext.Current.Cache.Get(configName);
            }
        }
        return result;
    }

}
}

Here is the service calling this method:

public class ConfigService
{
public static string siteName
{
    get { return ConfigRepository.ByName("Site_Name").configValue; }
}
public static string copyright
{
    get { return ConfigRepository.ByName("Copyright").configValue; }
}
public static string companyName
{
    get { return ConfigRepository.ByName("Company_Name").configValue; }
}
public static string homeTitle
{
    get { return ConfigRepository.ByName("Home_Title").configValue; }
}
public static string contactEmail
{
    get { return ConfigRepository.ByName("Contact_Email").configValue; }
}
public static string physicalAddress
{
    get { return ConfigRepository.ByName("Physical_Address").configValue; }
}
public static string phoneNumber
{
    get { return ConfigRepository.ByName("Phone_Number").configValue; }
}
}

Here is the report:

** Summary ** --------------- This message contains events from 1 to 1 out of a total of 1 scheduled notification events. There were 0 events left in the buffer at the beginning of this notification.

** ** --------------- :/LM/W3SVC/66/ROOT-7-129384226573152341 : Virtual :/ : D: * * ***. Com\ : WIN11

** ** --------------- : 3005 : . : 1/2/2011 12:17:44 AM (UTC): 1/2/2011 6:17:44 AM : f909c5c676bd4ca1ba21512c678ac502 : 6 : 1 : 0

:      : 26904      : w3wp.exe      : NT AUTHORITY\NETWORK SERVICE

:      : System.InvalidOperationException      : . .

:     URL- : http:// .com/article/ -ALERT      ://III-ALERT      : 68.230.129.53     :     : False      :      : NT AUTHORITY\NETWORK SERVICE

:      : 6      : NT AUTHORITY\NETWORK SERVICE      : False      : System.Data.SqlClient.SqlConnection.GetOpenConnection() System.Data.SqlClient.SqlConnection.get_HasLocalTransactionFromAPI() System.Data.SqlClient.SqlCommand.ValidateCommand(String , ) System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String, DbAsyncResult) System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String)
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior , String) System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior ) System.Data.Linq.SqlClient.SqlProvider.Execute(Expression , QueryInfo queryInfo, IObjectReaderFactory factory, Object [] parentArgs, Object [] userArgs, ICompiledSubQuery [] subQueries, Object lastResult) System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo [] queryInfos, IObjectReaderFactory factory, Object [] userArguments, ICompiledSubQuery [] subQueries) System.Data.Linq.SqlClient.SqlProvider.CompiledQuery.Execute(IProvider , Object []) System.Data.Linq.CompiledQuery.ExecuteQuery(DataContext , [] args) System.Data.Linq.CompiledQuery.Invoke [TArg0, TArg1, TResult] (TArg0 arg0, TArg1 arg1) DomainModel.Concrete.ConfigRepository.ByName(String configName) DomainModel.Services.ConfigService.get_companyName() ASP.views_shared_site_master._Render_control1 (HtmlTextWriter __w, Control parameterContainer) System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter , ICollection) System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter , ICollection) System.Web.UI.Page.Render(HtmlTextWriter ) System.Web.Mvc.ViewPage.Render(HtmlTextWriter ) System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

datacontext , , ?

- :

public class mvCmsContext : DataContext
{
    public mvCmsContext(): 
    base(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString,XmlMappingSource.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream("DomainModel.mvCmsMapping.map"))){
        Log = (StringWriter)HttpContext.Current.Items["linqToSqlLog"];
    }
public Table<DomainModel.Entities.Configuration> Configs { get { return this.GetTable<DomainModel.Entities.Configuration>(); } }
}    

, : ? .

public class ConfigRepository : IConfigRepository
{
    private mvCmsContext context { get; set; }
    public ConfigRepository() { context = new mvCmsContext(); }

    private static Func<mvCmsContext, string, Configuration> _byName =
        CompiledQuery.Compile((mvCmsContext context, string configName) =>
            (from c in context.Configs
             where c.configName == configName
             select c).SingleOrDefault());
    static public Configuration ByName(string configName)
    {
        var result = (Configuration)HttpContext.Current.Cache.Get(configName);
        if (result == null)
        {
            using (var context = new mvCmsContext())
            {
                HttpContext.Current.Cache.Insert(configName, _byName(context, configName));
                result = (Configuration)HttpContext.Current.Cache.Get(configName);
            }
        }
        return result;
    }
+3
1

, datacontext . , .

, datacontext, . ByName datacontext, , datacontext, ....... .

, datacontext .

+6

Source: https://habr.com/ru/post/1783092/


All Articles