문제

protected void getNews()
{   
 using (SPWeb web = getWeb("InternalNews"))

            {
                fetchNewsFromWeb(ref dtNews,true,"English",new string[] { "Internal news page" },web,startDate,endDate,false,true);
            }

}
protected SPWeb getWeb(string contentTypeUrlKey)
{
    try
    {
        List<string> urls = CTUrlWrapper.GetContentTypeUrl(contentTypeUrlKey, this.Page.Request.Url.ToString());
        return SPContext.Current.Site.OpenWeb(urls[0].ToLowerInvariant().Replace(SPContext.Current.Site.Url.ToLowerInvariant(), "").TrimStart('/'));
    }
    catch
    {
        throw new Exception("Can not fetch value from CTUrl list, key: \"" + contentTypeUrlKey + "\"");
    }
}

Do I really need to dispose the web in fetchNewsFromWeb method?

  protected DataTable fetchNewsFromWeb(ref DataTable dtAllData, bool useCriticalField, string pageLanguage, string[] contentTypes, SPWeb web, DateTime? fromDate, DateTime? toDate, bool otherUnitNews, bool useHeaderPrefix)
    {

        SPSiteDataQuery sdq = GetQuery(useCriticalField);
        StringBuilder sbQuery = new StringBuilder();
        sbQuery.Append(getWhereClause(pageLanguage, fromDate, toDate, contentTypes, otherUnitNews));
        sbQuery.Append(getOrderByClause(useCriticalField));
        sdq.Query = sbQuery.ToString();

        try
        {

            DataTable foundItems = web.GetSiteData(sdq);              
            if (foundItems.Rows.Count > 0)
            {
                foreach (DataRow row in foundItems.Rows)
                {
                    try
                    {
                        object[] dtAlldataTemp = extractNewsFields(row, useHeaderPrefix);
                        dtAllData.Rows.Add(dtAlldataTemp);

                    }
                    catch (Exception ex)
                    {

                    }
                }
            }
        }
        catch (Exception ex)
        {
        }

        finally {web.Dispose();}// do we really need this here?

        return dtAllData;
    }

protected SPWeb getWeb(string contentTypeUrlKey)
    {
        try
        {
            List<string> urls = CTUrlWrapper.GetContentTypeUrl(contentTypeUrlKey, this.Page.Request.Url.ToString());
            return SPContext.Current.Site.OpenWeb(urls[0].ToLowerInvariant().Replace(SPContext.Current.Site.Url.ToLowerInvariant(), "").TrimStart('/'));
        }
        catch
        {
            throw new Exception("Can not fetch value from CTUrl list, key: \"" + contentTypeUrlKey + "\"");
        }
    }
도움이 되었습니까?

해결책

No you don't.

The using will take care of disposing it already.

다른 팁

Did you ask SPDisposeCheck?

It depends on how getWeb function is returning the SPWeb.

If its just a SPContext.Current.Web - > Answer is No.

If its returned by opening a new web using OpenWeb() call. Answer is Yes.

Can you not pass the web by ref and have the outer dispose take are of it?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top