共用方式為


How to: Test a Health Rule During Development

Applies to: SharePoint Foundation 2010

While you are developing a rule, it can be helpful to create a simple console application to verify that the rule behaves as expected. The console application can invoke the rule’s Check() method and its Repair() method, if the rule has one, and it can test the return values of various properties.

After the rule is deployed, it runs in the context of the timer service. If you wait to test the rule after it is deployed, you will need to attach to owstimer.exe in order to step through the code, and testing will become more difficult.

To create a console application to test your rule

  1. Open Visual Studio as an administrator by right-clicking the program in the Start menu and selecting Run as administrator.

  2. Create a new console application project. In the New Project dialog, select Visual C# or Visual Basic, then select Windows and choose the Console Application template.

  3. Add a reference to Microsoft.SharePoint.dll. In Solution Explorer, right-click the project name and select Add Reference.... In the Add Reference dialog, browse to %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\ISAPI\Microsoft.SharePoint.dll. Select Microsoft.SharePoint.dll, and then click OK.

  4. Add a reference to the assembly that contains your rule.

  5. Add using statements (Imports in Visual Basic) for the System and Microsoft.SharePoint.Administration.Health namespaces and also for your rule’s namespace.

  6. Instantiate your rule by calling the default constructor. Then write code to test the behavior of the rule.

Example

This example is a console application that tests the rule that is shown as an example in the topic How to: Create a Health Rule.

using System;
using Microsoft.SharePoint.Administration.Health;
using Samples.HealthRules;

namespace RuleTester
{
    class Program
    {
        static void Main(string[] args)
        {
            DiskDriveAlmostFull rule = new DiskDriveAlmostFull();
            SPHealthCheckStatus status = rule.Check();

            Console.WriteLine(status.ToString());
            if (status == SPHealthCheckStatus.Failed)
                Console.WriteLine(rule.Explanation);

            Console.Write("\nPress ENTER to continue...");
            Console.Read();
        }
    }
}
Imports System
Imports Microsoft.SharePoint.Administration.Health
Imports Samples.HealthRules

Module Module1

    Sub Main()
        Dim rule As DiskDriveAlmostFull = New DiskDriveAlmostFull()
        Dim status As SPHealthCheckStatus = rule.Check()

        Console.WriteLine(status.ToString())
        If status = SPHealthCheckStatus.Failed Then
            Console.WriteLine(rule.Explanation)
        End If

        Console.Write(vbCrLf + "Press ENTER to continue...")
        Console.Read()
    End Sub

End Module

See Also

Tasks

How to: Create a Health Rule

How to: Create a Feature to Register a Health Rule

How to: Deploy a Health Rule with a Solution Package