Sharepoint学习笔记—习题系列--70-573习题解析 -(Q4-Q7)

Question 4
You have a Web Part that contains the following code segment. (Line numbers are included for reference only.)
01 protected void Page_Load(object sender, EventArgs e)
02 {
03   SPSite site = new SPSite("http://www.contoso.com/default.aspx");
04   {
05     SPWeb web = site.OpenWeb();
06    
07   }
08 }

You deploy the Web Part to a SharePoint site.
After you deploy the Web Part, users report that the site loads slowly. You need to modify the Web Part to prevent the site from loading slowly.
What should you do?
A. Add the following line of code at line 06:
web.Close();
B. Add the following line of code at line 06:
web.Dispose();
C. Add the following line of code at line 06:
site.Close();
D. Change line 03 to the following code segment:
using (SPSite site = new SPSite("http://www.contoso.com/default.aspx"))

解析:
 很明显,此题还是考的是SPSite,SPWeb对象内存的释放。
  由前面Question2,3的分析我们已经知道,使用Close方法与使用Dispose方法释放内存的区别。
  显然答案A,B,C只”分别” 释放了SPWeb对象或SPSite对象,而且释放的时机不对。
  我们通常是通过两种方式来处理此类代码
1. Try….Catch….Finally代码结构来实现内存的释放处理,
2. 也可以通过using()语句来自动实现内存的释放(事实上,系统在运行时会自动把Using代码块处理为Try….Catch…Finally代码块)。
http://msdn.microsoft.com/en-us/library/ee557362.aspx
所以本题目正确选项应该是D


Question 5
You create an event receiver.

The ItemAdded method for the event receiver contains the following code segment. (Line numbers are included for reference only.)

01 SPWeb recWeb = properties.Web;
02 using (SPSite siteCollection = new SPSite("http://site1/hr"))
03 {
04   using (SPWeb web = siteCollection.OpenWeb())
05   {
06     PublishingWeb oWeb = PublishingWeb.GetPublishingWeb(web);
07     PublishingWebCollection pubWebs = oWeb.GetPublishingWebs();
08     foreach (PublishingWeb iWeb in pubWebs)
09     {
10       try
11       {
12         SPFile page = web.GetFile("/Pages/default.aspx");
13         SPLimitedWebPartManager wpManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared);
14       }
15       finally
16       {
17         if (iWeb != null)
18         {
19           iWeb.Close();
20         }
21       }
22     }
23   }
24 }

You need to prevent the event receiver from causing memory leaks.
Which object should you dispose of?

A. oWeb at line 06
B. recWeb at line 01
C. wpManager at line 13
D. wpManager.Web at line 13

解析:
 对于选项A. oWeb对象是通过PublishingWeb.GetPublishingWeb方法获取的,这个对象并不是SPWeb对象,它是一个包裹了SPWeb对象集的对象实例,我们可以通过此实例的GetPublishingWebs方法进一步获取这包裹于其内的SPWeb对象集。并且通过遍历这个对象集来分别释放其内的SPWeb对象,如下面代码:

using(SPWeb web = site.OpenWeb())
{
  PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
  PublishingWebCollection pubWebs = pubWeb.GetPublishingWebs());
  foreach(PublishingWeb innerPubWeb in pubWebs)
  {
    try
    {
       Process innerPubWeb
    }
    finally
    {
      innerPubWeb.Web.Dispose();
    }
  }
}

而本题目也正是通过上述方式来实现的,所以选项A不会造成内存泄漏。
对于选项B. recWeb是通过properties对象的Web属性获取的, properties是Evenreceiver的传递参数。如下例:

private static void ItemAdded(SPItemEventProperties properties)       
{
    SPWeb web = properties.Web; 
}   

由此获取的SPWeb对象并不会造成内存的泄漏,因为它是由SPSite对象创建的,而此SPSite对象会在Event事件结束后自动被释放回收。

对于选项C,它并不是SPWeb对象,因此不存在要去释放它。
选项D为什么为造成内存泄漏问题呢,因为wpManager.Web是由page.GetLimitedWebPartManager方法返回的SPLimitedWebPartManager对象来获取的,这个SPLimitedWebPartManager对象实例包含了对SPWeb对象的内部引用, 并且在引用SPWeb对象之后并不会自动释放它,所以,wpManager.Web对象是会造成内存泄漏的。换句话说:当我们使用GetLimitedWebPartManager相关方法来获取SPWeb对象时,一定要注意内存泄漏问题。
因此,本题应该选 D
 
参考 :
http://msdn.microsoft.com/zh-tw/library/ms497306.aspx
http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/8999f361-6e8e-452a-a42e-c8bf323c106e


Question 6
You create a console application to manage Personal Sites.
The application contains the following code segment. (Line numbers are included for reference only.)

01 SPSite siteCollection = new SPSite("http://moss");
02 UserProfileManager profileManager = new UserProfileManager(ServerContext.GetContext(siteCollection));
03 UserProfile profile = profileManager.GetUserProfile("domain\\username");
04 SPSite personalSite = profile.PersonalSite;
05
06 siteCollection.Dispose();

You deploy the application to a SharePoint site.
After deploying the application, users report that the site loads slowly. You need to modify the application to prevent the site from loading slowly.
What should you do?
 
A. Remove line 06.
B. Add the following line of code at line 05:
personalSite.close();
C. Add the following line of code at line 05:
personalSite.Dispose();
D. Change line 06 to the following code segment:
siteCollection.close();

解析:
  选项A, 显然是错上加错,已经是由于SPSite造成的内存泄漏问题了,还在进一步扩大这个问题。
  选项 D. 显示无此必要,因为代码中已经有关于siteCollection的内存释放了,即:siteCollection.Dispose();
  选项 B与C:Close方法在这里并不是真正的完成了释放,因为personalSite并不是你新New的一个SPSite对象实例,见Question2,3解析,所以只能通过 Dispose方法才能真正释放SPSite的内存。
所以本题目正确选项应该是C

 
Question 7
You are creating a Web Part for SharePoint Server 2010.

The Web Part contains the following code segment. (Line numbers are included for reference only.)

01 protected override void CreateChildControls()
02 {
03   base.CreateChildControls();
04   SPSecurity.RunWithElevatedPrivileges(
05     delegate()
06     {
07       Label ListCount = new Label();
08       ListCount.Text = String.Format("There are {0} Lists", SPContext.Current.Web.Lists.Count);
09       Controls.Add(ListCount);
10     });
11 }

You need to identify which line of code prevents the Web Part from being deployed as a sandboxed solution.
Which line of code should you identify?
A.03
B. 04
C. 08
D. 09

解析
本题实质是考的关于Sandbox Solution的限制问题,Sandbox之所以安全,正是因为受限,所以它不支持通过RunWithElevatedPrivileges手段来提升代码的访问权限,因为如此一来就破坏了它的设计初衷。
Sandboxed solutions也不支持操作诸如下面的元素:
• Application Pages
• Custom Action Group
• Farm-scoped features
• HideCustomAction element
• Web Application-scoped features
• Workflows with code

所以本题目正确选项应该是B

 参考:

http://msdn.microsoft.com/en-us/library/gg615454(v=office.14).aspx

http://msdn.microsoft.com/en-us/library/gg615454.aspx

http://sharing-the-experience.blogspot.com.au/2011/06/sharepoint-2010-sandboxed-solution.html

posted @ 2013-06-07 07:54  wsdj  阅读(719)  评论(0编辑  收藏  举报