我应该处置这个 SPWeb 吗?
我希望有人能帮助我。我需要从 SPContext 获取当前站点的根 Web。通过以下操作可以轻松完成
SPContext.Current.Site.RootWeb
我对 SPContext.Current.Site.RootWeb 处的 SPSite 对象不应该被丢弃的想法感到满意,但是我得到的 SPWeb 对象又如何呢?来自 SPS 站点。当 SPSite 被处置时,rootweb SPWeb 也会被处置吗?还是我需要自己处理掉它?
I'm hoping someone can help me out. I need to get the root web of the current site from the SPContext. It's easily done with the following
SPContext.Current.Site.RootWeb
I'm comfortable with the idea that the SPSite object here at SPContext.Current.Site.RootWeb shouldn't be disposed of, but what about the SPWeb object I'm getting from the SPSite. Will, when the SPSite get's disposed, the rootweb SPWeb get disposed to? Or do I need to dispose of it myself?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不应释放对 SPSite.RootWeb 的调用。处置 SPSite 也会处置 RootWeb。
SPDisposeCheck 中有一个错误,如果您确实处置它,它会标记,如果您不处置(无论哪种方式都该死!)我详细介绍了我如何解决这个问题 在此博文中,因为您无法使用 SPDisposeCheckIgnore提升权限块中的属性。
Calls to SPSite.RootWeb should not be disposed. Disposing the SPSite will also dispose the RootWeb.
There is a bug in SPDisposeCheck where it flags if you do dispose it, and if you don't (damned either way!) I detailed how I solved this in this blog post, as you can't use an SPDisposeCheckIgnore attribute in elevated privileges blocks.
不,你不应该。您应该只处置您可以控制的物体。由于上下文是由 SharePoint 创建的,因此您无需处理它,因为其他对象可能依赖于它。
如果您要从该对象属性创建您自己的 SPWeb 实例,则需要将其释放。 IE。
这是一篇有关处理 SharePoint 对象的最佳实践的文章。
http://msdn.microsoft.com/en-我们/library/aa973248(v=office.12).aspx
No you should not. You should only dispose objects you are in control of. Because the context is something created by SharePoint you do not dispose of this as other objects may be dependent upon this.
If you were to create your own instance of an SPWeb from this objects properties than it would need to be disposed. I.e..
Here is an article on the best practices of disposing SharePoint objects.
http://msdn.microsoft.com/en-us/library/aa973248(v=office.12).aspx
通常应在 using 子句中使用 SPSite 和 SPWeb。
在您使用完 SPWeb 对象后,这将自动正确地释放它。
You should normally use SPSite and SPWeb in a using clause.
This automatically will correctly release the SPWeb object after you are done with it.