如何:创建用于注册运行状况规则的功能
上次修改时间: 2009年9月23日
适用范围: SharePoint Foundation 2010
可以通过在 SharePoint 运行状况分析器 中注册规则来安装新的运行状况规则。为此,必须先将包含规则的程序集放置在每台计算机上的全局程序集缓存 (GAC) 中,然后调用 SPHealthAnalyzer 类的 RegisterRules(Assembly) 方法。
在开发环境中工作时,可以使用全局程序集缓存工具 (Gacutil.exe) 将程序集放置到 GAC 中,然后可以编写自定义代码来加载该程序集并注册规则。在生产环境中,应使用更可靠的过程。在这种情况下,规则可能会由其他某个人(如服务器场管理员)安装,并且很可能安装在多台服务器上,而不是只安装在您用于开发的服务器上。
在生产环境中向 SharePoint 运行状况分析器 注册规则的最佳方法是创建一个用于该用途的 SharePoint 功能。该方法的关键是在规则程序集中包含 FeatureActivated 和 FeatureDeactivating 事件的事件处理程序。当服务器场管理员安装该功能时,您的事件处理程序将会注册到系统中。当场管理员激活该功能时,您的 FeatureActivated 事件处理程序中的代码就可以调用 RegisterRules(Assembly) 方法来注册该规则。当服务器场管理员停用该功能时,您的 FeatureDeactivating 事件处理程序中的代码就可以调用 UnregisterRules(Assembly) 方法来删除该规则。
本主题介绍如何创建用于注册规则和撤消规则注册的事件处理程序,以及如何在服务器场级别的功能中包括事件处理程序和规则程序集。有关部署功能以及在 GAC 中安装规则程序集的信息,请参阅如何:使用解决方案包部署运行状况规则。
创建用于注册运行状况规则和撤消运行状况规则注册的事件处理程序
右键单击"开始"菜单中的 Visual Studio,然后选择"以管理员身份运行",以管理员身份打开该程序。
打开包含运行状况规则代码的项目。
将新类添加到该项目中。
在"解决方案资源管理器"中,依次选择项目名称、"添加"和"新建项目…"。在"添加新项"对话框中,选择"类"模板。将类命名为 RuleFeatureReceiver。单击"添加"。
为以下示例中显示的命名空间添加 using 语句(在 Visual Basic 中为 Imports)。
using System; using System.Collections.Generic; using System.Reflection; using Microsoft.SharePoint; using Microsoft.SharePoint.Administration.Health;
Imports System Imports System.Collections.Generic Imports System.Reflection Imports Microsoft.SharePoint Imports Microsoft.SharePoint.Administration.Health
修改类声明以使类继承自 SPFeatureReceiver 类。
class RuleFeatureReceiver : SPFeatureReceiver
Public Class RuleFeatureReceiver Inherits SPFeatureReceiver
重写并实现 FeatureActivated(SPFeatureReceiverProperties) 方法,如以下示例所示。
public override void FeatureActivated(SPFeatureReceiverProperties properties) { Assembly a = Assembly.GetExecutingAssembly(); IDictionary<Type, Exception> exceptions = SPHealthAnalyzer.RegisterRules(a); if (exceptions != null) { string logEntry = a.FullName; if (exceptions.Count == 0) { logEntry += " All rules were registered."; } else { foreach (KeyValuePair<Type, Exception> pair in exceptions) { logEntry += string.Format(" Registration failed for type {0}. {1}", pair.Key, pair.Value.Message); } } System.Diagnostics.Trace.WriteLine(logEntry); } }
Public Overrides Sub FeatureActivated(ByVal properties As Microsoft.SharePoint.SPFeatureReceiverProperties) Dim a As Assembly = Assembly.GetExecutingAssembly() Dim exceptions As IDictionary(Of Type, Exception) = SPHealthAnalyzer.RegisterRules(a) If Not exceptions Is Nothing Then Dim logEntry As String = a.FullName If exceptions.Count = 0 Then logEntry += " All rules were registered." Else Dim pair As KeyValuePair(Of Type, Exception) For Each pair In exceptions logEntry += String.Format(" Registration failed for type {0}. {1}", _ pair.Key, pair.Value.Message) Next End If System.Diagnostics.Trace.WriteLine(logEntry) End If End Sub
重写并实现 FeatureDeactivating(SPFeatureReceiverProperties) 方法,如以下示例所示。
public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { Assembly a = Assembly.GetExecutingAssembly(); IDictionary<Type, Exception> exceptions = SPHealthAnalyzer.UnregisterRules(a); if (exceptions != null) { string logEntry = a.FullName; if (exceptions.Count == 0) { logEntry += " All rules were unregistered."; } else { foreach (KeyValuePair<Type, Exception> pair in exceptions) { logEntry += string.Format(" Unregistration failed for type {0}. {1}", pair.Key, pair.Value.Message); } } System.Diagnostics.Trace.WriteLine(logEntry); } }
Public Overrides Sub FeatureDeactivating(ByVal properties As Microsoft.SharePoint.SPFeatureReceiverProperties) Dim a As Assembly = Assembly.GetExecutingAssembly() Dim exceptions As IDictionary(Of Type, Exception) = SPHealthAnalyzer.UnregisterRules(a) If Not exceptions Is Nothing Then Dim logEntry As String = a.FullName If exceptions.Count = 0 Then logEntry += " All rules were unregistered." Else Dim pair As KeyValuePair(Of Type, Exception) For Each pair In exceptions logEntry += String.Format(" Unregistration failed for type {0}. {1}", _ pair.Key, pair.Value.Message) Next End If System.Diagnostics.Trace.WriteLine(logEntry) End If End Sub
生成项目。
创建用于安装运行状况规则的功能
创建一个 Features 文件夹。
在"解决方案资源管理器"中,右键单击项目名称,选择"添加",再选择"新建文件夹"。键入 Features。
为功能创建子文件夹。
右键单击"Features"文件夹,选择"添加",然后选择"新建文件夹"。键入功能的名称(例如 CompanyName.HealthRules)。
稍后,当您创建解决方案包以部署该功能时,将在服务器场中的每台服务器的 %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\FEATURES 文件夹中创建一个同名文件夹。为该文件夹指定的名称可以帮助管理员找到功能的定义文件。
创建功能定义文件。
右键单击功能的子文件夹,选择"添加",然后选择"新建项目..."。在"添加新项"对话框中,选择"XML 文件"模板。将文件命名为 Feature.xml。然后单击"添加"。
在编辑器中打开 Feature.xml。
删除该文件中的第一(唯一)行。在该行的位置,粘贴以下代码。
<Feature xmlns="https://schemas.microsoft.com/sharepoint/" Scope="Farm" Hidden="FALSE" Title="Your Title" Description="Your description" Id="00000000-0000-0000-0000-000000000000" ReceiverAssembly="<assembly name>, Version=<number>, Culture=<culture>, PublicKeyToken=<token>" ReceiverClass="MyNamespace.RuleFeatureReceiver" AlwaysForceInstall="TRUE" ImageUrl=""/>
在 Feature.xml 中,将 Title 和 Description 属性的值替换为您自己的文本。
将 Id 属性的值替换为新生成的 GUID(不带括号)。
可以使用 GuidGen 工具 (guidgen.exe) 来获得新 GUID。在 Visual Studio 中的"工具"菜单上,选择"创建 GUID"。在"创建 GUID"对话框中,选择"4. 注册表格式",然后单击"复制"。将剪贴板的内容粘贴到 Id 属性后面的引号之间。删除 GUID 中的括号。
将 ReceiverAssembly 属性的值替换为程序集的完整四部分名称。
有关如何获取程序集全名的信息,请参阅如何:创建用于获取程序集全名的工具。
将 ReceiverClass 属性的值替换为 SPFeatureReceiver 子类的命名空间限定的名称。
保存该文件。
有关如何部署功能的信息,请参阅如何:使用解决方案包部署运行状况规则。