&是什么QUOT;获得处置倒闭"意思? [英] What does "Access to disposed closure" mean?

查看:277
本文介绍了&是什么QUOT;获得处置倒闭"意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

public void DequeueRecipe(AuthIdentity identity, params Guid[] recipeIds)
{
   using (var session = GetSession())
   {
      var recipes = (from r in recipeIds select new Models.Recipes {RecipeId = r}).ToArray();

      var dbRecipes = session.QueryOver<Models.QueuedRecipes>()
         .Where(Expression.Eq("UserId", identity.UserId))
         .Where(Expression.InG("Recipe", recipes))
         .List<Models.QueuedRecipes>();

      using (ITransaction transaction = session.BeginTransaction())
      {
         dbRecipes.ForEach(r => session.Delete(r)); // <-- Warning
         transaction.Commit();
      }
   }
}



ReSharper的是给我警告

reSharper is giving me the warning:

访问处置倒闭

就行了:

dbRecipes.ForEach(r => session.Delete(r));



(即会议变量被强调为罪魁祸首)。

(The session variable is underlined as the culprit).

虽然它的真正的的ForEach 方法将其周围的变量<$创建一个封闭一个兰巴表达式C $ C>会议,我没有看到执行该代码时,它可能将被处理的方式。也许ReSharper的认为的ForEach 可并行执行某种任务或保存动作<> 在稍后的时间,从而在技术上而匿名函数仍然在内存访问它可能被处理。

While it's true the ForEach method takes a lamba expression which creates a closure around the variable session, I don't see a way where it would possibly be disposed when this code is executed. Perhaps reSharper thinks ForEach might execute some sort of task in parallel, or save that Action<> for a later time, thus technically it might be disposed while the anonymous function is still reachable in memory.

我是安全的忽略此警告?有没有一种方法可以让我重新格式化我的代码,以防止此警告出现?到底有没有这个码表示的危险?

Am I safe the ignore this warning? Is there a way I can reformat my code to prevent this warning from appearing? Is there indeed a danger presented by this code?

我已经习惯了被ReSharper的比我聪明,所以我想了解究竟发生了什么事。

I'm used to reSharper being smarter than me, so I'd like to understand exactly what's going on.

推荐答案

由于会议包装在using语句,和LINQ执行可能被延迟执行,直到它被一一列举。

Because session is wrapped in a using statement, and the LINQ execution could be defered until it is enumerated.

ReSharper的警告,这可能导致异常,因为受时间 dbRecipes 枚举,在会议可能已被释放。

Resharper is warning that this could result in an exception, because by the time dbRecipes is enumerated, the session could have have been disposed.

我会说实话:我不知道上面的代码可能永远失败的方式警告

I'll be honest: I'm not sure the above code could ever fail in the way warned about.

这篇关于&是什么QUOT;获得处置倒闭&QUOT;意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆