Microsoft 开发人员网络 > 论坛主页 > SharePoint - Development and Programming > Dispose SPWeb object inside Feature Receiver? Or don't?
提出问题提出问题
 

已答复Dispose SPWeb object inside Feature Receiver? Or don't?

  • 2008年9月8日 19:22PSeale 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     包含代码
    All,

    Is there an authoritative source of information telling us whether we should dispose SPWeb objects retrieved inside a feature receiver, specifically something like:

    public override void FeatureActivated(SPFeatureReceiverProperties properties) 
        //should this be in a using block? 
        SPWeb web = properties.Feature.Parent as SPWeb; 

    I have my opinion, and I'm sure you all have your opinion, but...does anyone really know?

    Also before you start, I'll pre-empt you and link to Roger Lamb's post on the subject. He doesn't cover Feature Receivers specifically:
    http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx

答案

  • 2008年9月8日 20:18Tony Testa MVPMVP用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    I just looked at some old event handlers i wrote and I always cleaned up the SPWeb object.

    BUT if your using properties.feature.parent, you would NOT want to kill the object because you would be killing the object that is running your code, same as in a webpart, you won't want to kill the SPContext.Current.Web object because its running your webpart.

    Typically in my event handlers I need to run code elevated, so i create the SPWeb or SPSite objects inside the elevated code and use the ID or URL from the properties collection to create the objects, that way I am creating new objects and can dispose of them knowing that I am not killing the object that is running my code.

全部回复