开发者

Do I need to dispose SPWeb here...?

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
                    {
         开发者_C百科               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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜