如何在使用块中处置SharePoint对象 [英] How to Dispose SharePoint object within Using Block

查看:119
本文介绍了如何在使用块中处置SharePoint对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SharePoint中有一个使用SharePoint类库方法的方法。以下是代码片段

I have a method in SharePoint which is using the SharePoint class library methods. Following is the code snippet

try
{
using (SPSite mySite=new SPSite(strURL))
{
   using (SPWeb myWeb=mySite.OpenWeb())
     {
              // Some Functionality
     }
}
}
catch
{

}





现在如果在* 某些功能中发生任何异常 *然后SPSite和SPWeb对象都没有被处理掉。因此,每当我试图再次实例化SPSite和SPWeb对象时(我必须再次实例化它们,这是一个要求)它会抛出一个错误。如何解决这个问题?如何在异常块中处理对象?



now if any exception occurs within *Some Functionality* then both the SPSite and SPWeb objects are not getting disposed. Hence whenever I am trying to instantiate the SPSite and SPWeb object again (I have to instantiate those again, it's a requirement) it's throwing an error. How to resolve the issue? How to dispose the objects within exception block?

推荐答案

应该处理对象。唯一的问题是你不知道什么时候会发生这种情况。因为垃圾收集器决定何时处理对象。也许你应该考虑在没有使用块的情况下安装对象。



尝试这样的事情:

The objects should be disposed. The only problem is that you do not know when excatly this will happen. Because the garbage collector decides when the objects will be disposed. Maybe you should consider to instaciate the objects without the using blocks.

Try something like this:
SPSite mySite = null;
SPWeb myWeb = null;

try
{
using (mySite=new SPSite(strURL))
{
   using (myWeb=mySite.OpenWeb())
     {
              // Some Functionality
     }
}
}
catch
{
   if(mySite != null)
      mySite.Dispose();

   if(myWeb != null)
      myWeb.Dispose();

   // ensure they are really null
   mySite = null;
   myWeb = null;
}


这篇关于如何在使用块中处置SharePoint对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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