SPC110281: Dispose SPSite created with UserProfiles.PersonalSite

The Microsoft.Office.Server.UserProfiles.PersonalSite returns an SPSite object that must be disposed.

TypeName: DisposeSPSiteCreatedByUserProfilesPersonalSite
CheckId: SPC110281
Severity: CriticalWarning
Type: AssemblyFileReference
Resolution

The Microsoft.Office.Server.UserProfiles.PersonalSite returns an SPSite object that must be disposed. See sample from MSDN:

Bad Coding Practice

void PersonalSiteLeak()
{
  // Open a site collection.
  using (SPSite siteCollection = new SPSite("http://moss"))
  {
    UserProfileManager profileManager = new UserProfileManager(ServerContext.GetContext(siteCollection));
    UserProfile profile = profileManager.GetUserProfile("domain\username");
    SPSite personalSite = profile.PersonalSite;    // Will leak.
  }
}

Good Coding Practice
void PersonalSiteNoLeak()
{
  // Open a site collection.
  (SPSite siteCollection = new SPSite("http://moss"))
  {
    UserProfileManager profileManager = new UserProfileManager(ServerContext.GetContext(siteCollection));
    UserProfile profile = profileManager.GetUserProfile("domain\username");
    using (SPSite personalSite = profile.PersonalSite)
    {
      // ...
    }
  }
}

Remarks

Rule relates to SPDisposeCheckId 'SPDisposeCheckID_400'. To ignore this rule add the attribute '[SPDisposeCheckIgnore(SPDisposeCheckID.SPDisposeCheckID_400, "Caller will dispose")]' to your method.

Links

comments powered by Disqus