Pregunta

Don't web.Dispose() dispose parent SPSite object?

Is it Ok if I do this in web part?

{              
    SPSite site = SPContext.Current.Site;

    using (SPWeb web = site.OpenWeb(s))
    {
         // ..
    }
}
¿Fue útil?

Solución

The code you written is Ok.

using statement will dispose the SPWeb object you have instantiated.

Parent Site - This object is not initiantiated by you, you are using SPSite that belongs to SPContext.

SPContext objects are managed by the SharePoint framework and should not be explicitly disposed in your code. This is true also for the SPSite and SPWeb objects returned by SPContext.Site, SPContext.Current.Site, SPContext.Web, and SPContext.Current.Web.

Otros consejos

Yes, it's acceptable to do what you have done. It's also ok to pass that web object to other functions.

See a detailed article on this subject from Microsoft here.

Licenciado bajo: CC-BY-SA con atribución
scroll top