为什么某些 SharePoint 示例使用
using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
...
}
而不仅仅是简单地?
SPSite site = SPContext.Current.Web.Site;
...
Update
我想我已经将问题缩小到以下范围:
看来我不应该使用SPContent.Current
直接,除非我确定我的代码在 SharePoint 中运行。但什么时候这不是真的呢?
查看最佳实践文档在 SharePoint 2010 中处置对象 http://msdn.microsoft.com/en-us/library/ee557362%28v=office.14%29.aspx来自微软,但是有反对意见 https://sharepoint.stackexchange.com/questions/20192/using-spcontext-current-or-using-static-url.
SharePoint 项目有一些关键要点:
- 始终丢弃yourSPWeb / SPSite 对象 --> 内存泄漏
- Make use of SPContext.Current... when you are sure your code is running in a SharePoint context
- 单元测试意味着没有 Sharepoint 上下文
- 外部实用程序意味着没有 Sharepoint 上下文
- Powershell 意味着没有 SharePoint 上下文(例如,使用功能接收器激活功能可能会失败)
- 不要处置 SPContext.Current... 而是创建您自己的对象(再次
using
)
你可能有一致性问题 http://hharkema.wordpress.com/2009/09/21/spcontext-current-web-or-spcontext-current-site-openweb/与您的多个 SP.. 对象。
到底SPSite site = SPContext.Current.Web.Site;
在某些情况下很好,但你无法控制这一点site
对象 - 这可能是问题所在。如果你去new SPSite(...)
你将永远拥有your SPSite
而不是 SharePoint 为您创建和管理的东西。
就我个人而言,我几乎总是选择using
结构,以便之后所有物体都得到正确处理。或者我用SPContext.Current.Web
无需处置。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)