Вопрос

I got many warnings from the SPDispose checker like this:

ID: SPDisposeCheckID_140 Module: xx.xx.dll Method: xx.xx.Layouts.xx.UpdateWorkspace.Page_Load(System.Object,System.EventArgs) Statement: RootWeb := RootSite.{Microsoft.SharePoint.SPSite}get_RootWeb() Source: UpdateWorkspace.aspx.cs Line: 201 Notes: Disposable type not disposed: Microsoft.SharePoint.SPWeb ***This may be a false positive depending on how the type was created or if it is disposed outside the current scope

More Information: http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx#SPDisposeCheckID_140

My code is as follows, For me its OK, its a false positive, but I wanted to know if somebody might see any issue here

protected void Page_Load(object sender, EventArgs e) {

        //Go to home site and retrieve values

        SPWeb ThisWeb = SPContext.Current.Site.RootWeb;
        currentsiteurl = ThisWeb.Url;
        SPWebApplication ThisWebApplication = SPWebApplication.Lookup(new Uri(currentsiteurl));
        WebAppUrl = ThisWebApplication.GetResponseUri(SPUrlZone.Default).AbsoluteUri;

        using (SPSite RootSite = new SPSite(WebAppUrl))
        {
                SPWeb RootWeb = RootSite.RootWeb;

        }               


    if (!IsPostBack)
    {
            SPWeb Currentweb = SPContext.Current.Web;
            SiteTitle.Text = Currentweb.Title;
            SiteDescription.Text = Currentweb.Description;
            SupportEmail.Text = Currentweb.Properties["x-SupportEmail"];
            ContactEmail.Text = Currentweb.Properties["x-ContactEmail"];

            currentsiteurl = Currentweb.Url;

            ListID = Convert.ToInt32(Currentweb.Properties["SiteListID"]);

            SPWebApplication MyWebApplication = SPWebApplication.Lookup(new Uri(currentsiteurl));
            WebAppUrl = MyWebApplication.GetResponseUri(SPUrlZone.Default).AbsoluteUri;
            IntAppUrl = MyWebApplication.GetResponseUri(SPUrlZone.Internet).AbsoluteUri;

            webapp = currentsiteurl.Replace(IntAppUrl, WebAppUrl);
            currentsiteurl = (currentsiteurl.Substring(currentsiteurl.LastIndexOf('/'))).Trim('/');




        #region Read values from central site settings list

        using (SPSite RootSite = new SPSite(WebAppUrl))
        {
            SPWeb RootWeb = RootSite.RootWeb;
                Debug.Text += "<br>RootWebUrl second loop: " + RootWeb.Url;

                string listUrl = string.Format("{0}/Lists/x", RootWeb.Url);
                SPList list = RootWeb.GetList(listUrl);
                SPListItem item = list.GetItemById(ListID);

         }

        #endregion
    }
}
Это было полезно?

Решение

Based on the warning you are experiencing, the SPDisposeCheck tool should refer to the following code block:

using (SPSite RootSite = new SPSite(WebAppUrl)) 
{ 
      SPWeb RootWeb = RootSite.RootWeb; 
      // CUT ... 
} 

Statement: RootWeb := RootSite.{Microsoft.SharePoint.SPSite}get_RootWeb()

It would seem that the tool is suggesting that you should dispose the RootWeb instance obtained from RootSite.RootWeb. If that is the case, you shouldn't worry, as RootWeb no longer requires explicit dispose - disposing will be automagically get taken care of by the parent SPSite object. There is even a specific guidance on the same blog:

public void RootWebBestPractice()
{
    // Exception to "Best Practices: Using Disposable Windows SharePoint Services 
    // Objects" White paper

    // Example 1 - new SPSite
    using (SPSite siteCollection = new SPSite("http://moss"))
    {
        SPWeb rootWeb1 = siteCollection.RootWeb;
        // No explicit rootWeb1 dispose required
    }  // siteCollection automatically disposed by implementing using()
    // rootWeb1 will be Disposed by SPSite

    // Example 2 - SPContext and SPControl
    SPWeb rootWeb2 = SPContext.Current.Site.RootWeb;
    // Also would apply to SPControl.GetContextSite(Context);
    // No explicit rootWeb2 dispose required because it's obtained from SPContext.Current.Site
}

Другие советы

SPDisposeCheck may warn you about undisposed SPWeb you get from SPSite.RootWeb. You need not worry about it.

No explicit dispose required for RootWeb.

The SPSite you create is disposed by implementing using(). The RootWeb will be disposed by SPSite.

In documentation

An earlier version of this article indicated that the calling application should dispose of the SPSite.RootWeb property just before disposing of the SPSite object that is using it. This is no longer the official guidance. The dispose cleanup is handled automatically by the SharePoint framework. Additionally, SPSite properties LockIssue, Owner, and SecondaryContact used the RootWeb property internally. Given the updated guidance for RootWeb, it is no longer advisable to call the Dispose method on the SPSite.RootWeb property whenever any of these properties are used.

This best practice addresses the issue identified by the SharePoint Dispose Checker Tool as SPDisposeCheckID_140.

See this post.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с sharepoint.stackexchange
scroll top