我需要在这里处理SPWeb吗...?

发布于 2024-10-07 23:05:05 字数 2573 浏览 3 评论 0原文

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 + "\"");
    }
}

我真的需要在 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 + "\"");
        }
    }
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 + "\"");
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不语却知心 2024-10-14 23:05:05

不,你不知道。

using 将负责处理它。

No you don't.

The using will take care of disposing it already.

别低头,皇冠会掉 2024-10-14 23:05:05

您是否询问过SPDisposeCheck

Did you ask SPDisposeCheck?

溺孤伤于心 2024-10-14 23:05:05

这取决于 getWeb 函数如何返回 SPWeb。

如果它只是一个 SPContext.Current.Web - >答案是否定的。

如果它是通过使用 OpenWeb() 调用打开一个新网络而返回的。答案是肯定的。

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.

美胚控场 2024-10-14 23:05:05

您不能通过引用传递网络并对其进行外部处理吗?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文