博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SPSite, SPWeb Dispose and Class Design Partter
阅读量:6085 次
发布时间:2019-06-20

本文共 3843 字,大约阅读时间需要 12 分钟。

Need to Dispose:
 
1. SPSite oSPsite =
new SPSite(
)
2. siteCollection =
new SPSite(SPContext.Current.Web.Url))
3. SPSite siteCollection = siteCollections.Add(
"sites/myNewSiteCollection",
"DOMAIN\\User",
     
)
 
4.  
using (SPWeb web =
new SPSite(SPContext.Current.Web.Url).OpenWeb())
    {
       
// SPSite leaked !
    }
 
5.     UserProfileManager profileManager =
new UserProfileManager(ServerContext.GetContext(siteCollection));
        UserProfile profile = profileManager.GetUserProfile(
"domain\\username");
        SPSite personalSite = profile.PersonalSite;   
// Will leak.
 
6. SPWeb oSPWeb = oSPSite.OpenWeb()
7. SPWeb web = siteCollection.AllWebs.Add(
"site-relative URL");
8.  using (SPWeb web = siteCollection.OpenWeb())
 {
        SPWeb addedWeb = web.Webs.Add(strWebUrl);  
// Will leak.
 }
 
9.  for(i = 0;i < oSPWeb.Webs.Count;i++)
  {
      oSPWeb2 = oSPWeb.Webs[i];
      BuildTableRow(oDisplayTable,
"Web", oSPWeb2.Title);
      oSPWeb2.Dispose();
  }
10.   Area area = AreaManager.GetArea(PortalContext.Current,
new Guid(
"{GUID}"));
   
using (SPWeb areaWeb = area.Web)
    {
       
string str = areaWeb.Title;
    }
 
11. 
using (SPSite siteCollection =
new SPSite(
"http://moss"))
      {
       
using (SPWeb web = siteCollection.OpenWeb())
        {
            SPFile page = web.GetFile(
"Source_Folder_Name/Source_Page");
            SPLimitedWebPartManager webPartManager =
                page.GetLimitedWebPartManager(PersonalizationScope.Shared);
                webPartManaber.Web.Dispose();
        }
// SPWeb object web.Dispose() automatically called.
      } 
// SPSite object siteCollection.Dispose() automatically called.

12.  using (SPWeb web = siteCollection.OpenWeb())

        {
            PublishingWeb variationPublishingWeb = null;
           
try
            {
                PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web); 
// Passing in SPWeb object, so no Close() needed.
                VariationLabel variationLabel = Variations.Current.UserAccessibleLabels[0];
                variationPublishingWeb = publishingWeb.GetVariation(variationLabel); 
// Must be Closed().
                // ...
            }
           
finally
            {
               
if(variationPublishingWeb != null)
                    variationPublishingWeb.Close();
            }
        }
//

 
Don't Need to Dispose:
 
1. SPWeb web = SPControl.GetContextWeb(HttpContext.Current)
2. SPWebCollection webCollection = siteCollection.AllWebs;
 
3.
using (SPSite siteCollection =
new SPSite(
"http://moss"))
    {
       
using (SPWeb outerWeb = siteCollection.OpenWeb())
        {
           
foreach (SPWeb innerWeb
in siteCollection.AllWebs)
            {
               
try
                {
                   
// ...
                }
               
finally
                {
                   
if(innerWeb !=
null)
                        innerWeb.Dispose();
                }
            }
        }
// SPWeb object outerWeb.Dispose() automatically called.
    }
 
4.  IPersonalPage currentMySitePage =
this.Page
as IPersonalPage;
if (currentMySitePage !=
null && !currentMySitePage.IsProfileError)
{
     SPSite personalSite = currentMySitePage.PersonalSite;
// Will not leak.
    
// ...
}
 
5. SPWeb parentWeb = list.ParentWeb;
//No explicit dispose required.
6. SPSite siteCollection = SPControl.GetContextSite(Context);
7. SPWeb web = SPControl.GetContextWeb(Context);
8. PublishingWeb outerPubWeb = PublishingWeb.GetPublishingWeb(web);
    PublishingWebCollection pubWebCollection = outerPubWeb.GetPublishingWebs();
 
For and Foreach
            
foreach (siteCollectionInner
in siteCollections)
             {
                     
try 
//Should be first statement after foreach.
                      {
                          Console.WriteLine(siteCollectionInner.Url);
                         
//Exception occurs here.
                      }
                     
finally
                      {
                         
if(siteCollectionInner !=
null)
                          siteCollectionInner.Dispose();
                 }
 
 
Class Design Pattern
public MyClass
{
     private SPSite _site;
     private SPWeb _web;
     MyClass()
    {
      _site = new SPSite(url);
       _web = _site.OpenWeb();
     }
    public
void MethodA()
    {
        _siteCollection =
new SPSite(
"http://moss");
        _web = _siteCollection.OpenWeb();
    }
   
public
void MethodB()
    {
       
if (_web !=
null)
        {
           
string title = _web.Title;
        }
    }
   
public
void MethodC()
    {
       
if (_web !=
null)
        {
           
string name = _web.Name;
        }
    }
//do stuff w/member variables
//dispose of object to prevent memory leaks
    ~MyClass()
  {
   if ( _site == null )
    {
       _site.Dispose();
      _site = null;
    }
     if ( _web == null )
    {
       _web.Dispose();
      _web = null;
     }
   }
}
 

转载于:https://www.cnblogs.com/gzh4455/archive/2011/11/02/2233299.html

你可能感兴趣的文章
用Proxmox搞定gpu穿透
查看>>
18个有趣的API供你的前端开发测试之用
查看>>
从一个职校走出来的高级程序员
查看>>
案例:低迷的产品研发团队
查看>>
Hadoop系列之一:大数据存储及处理平台产生的背景
查看>>
vector容器与find算法
查看>>
《从零开始学Swift》学习笔记(Day 19)——函数参数传递
查看>>
corosync+pacemaker高可用集群
查看>>
看完就能出去神侃,来自研发第一线的“区块链”扫盲文(一)
查看>>
比较全的 POM.xml
查看>>
7.VMware View 4.6安装与部署-connection server(View Security Server)
查看>>
Hyper-V下安装Ossim系统
查看>>
LBS营销的核心是资源共享与互换
查看>>
桌面虚拟化之GPU虚拟化
查看>>
Powershell管理系列(十六)查询最近一个月未登录的AD账号和Exchange账号
查看>>
Siri+ Wolfram Alpha……正在改变用户搜索习惯
查看>>
SDN落地需要方法论
查看>>
客户端自动升级的一个代码例子【C/S】
查看>>
对FrameBuffer的一夜hack
查看>>
Oracle数据库需要修改默认的Profiles,避免用户密码过期
查看>>