Devo smaltire SPWeb qui ...?
-
11-10-2019 - |
Domanda
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 + "\"");
}
}
Ho davvero bisogno di smaltire il web in modo fetchNewsFromWeb?
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 + "\"");
}
}
Soluzione
No non ne avrai.
Il using
si prenderà cura di smaltimento già.
Altri suggerimenti
Hai chiesto SPDisposeCheck ?
Dipende da come la funzione GetWeb sta tornando lo SPWeb.
Se la sua solo una SPContext.Current.Web -.> Risposta è no
Se la sua restituito con l'apertura di un nuovo Web utilizzando OpenWeb call (). La risposta è sì.
Si può non passare il web ref e hanno l'adozione Smaltire esterna sono di esso?
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow