Pergunta

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 + "\"");
        }
    }
Foi útil?

Solução

No you don't.

The using will take care of disposing it already.

Outras dicas

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?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top