문제

We have code where at times we'll be returning an SPWeb object from a function. So for example:

public SPWeb getDeptWeb()
{
    SPWeb deptWeb = SpSite.OpenWeb(SpContext.Web.ID);
    ...
    return deptWeb;
}

How can we dispose of the SPWeb object in this instance? Or is it sufficient to dispose it where we're accepting the returned parameter?

도움이 되었습니까?

해결책

The best way is probably to dispose of the SPWeb in the caller, e.g. with the using statement:

public SPWeb getDeptWeb()
{
    SPWeb deptWeb = SpSite.OpenWeb(SpContext.Web.ID);
    // ...
    return deptWeb;
}

public void Foo()
{
    using (SPWeb deptWeb = getDeptWeb()) {
        // Do something with the website...
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top