共用方式為


SPServiceInstance Class

Represents a single instance of a service that runs on a server.

Inheritance Hierarchy

System.Object
  Microsoft.SharePoint.Administration.SPAutoSerializingObject
    Microsoft.SharePoint.Administration.SPPersistedObject
      Microsoft.SharePoint.Administration.SPPersistedUpgradableObject
        Microsoft.SharePoint.Administration.SPServiceInstance
          Microsoft.SharePoint.Administration.SPDatabaseServiceInstance
          Microsoft.SharePoint.Administration.SPIncomingEmailServiceInstance
          Microsoft.SharePoint.Administration.SPOutboundMailServiceInstance
          Microsoft.SharePoint.Administration.SPWebServiceInstance
          Microsoft.SharePoint.Administration.SPWindowsServiceInstance

Namespace:  Microsoft.SharePoint.Administration
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

<GuidAttribute("983C4B09-FBC0-4f4b-92BC-42FABF556ED5")> _
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
Public Class SPServiceInstance _
    Inherits SPPersistedUpgradableObject

Dim instance As SPServiceInstance
[GuidAttribute("983C4B09-FBC0-4f4b-92BC-42FABF556ED5")]
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
public class SPServiceInstance : SPPersistedUpgradableObject

Remarks

The SPServiceInstance class associates services with servers. Use the Service property to get the SPService object that contains the farm-wide settings that apply to the service that this instance implements. Use the Server property to get the SPServer object on which this instance is installed.

To return an SPServiceInstanceCollection object that represents the service instances that currently run on a server, use either the ServiceInstances property of the SPServer class, or the SPServiceInstanceCollection constructor. To return an SPServiceInstanceDependencyCollection object that represents the service instances that are dependent upon a service, use the Instances property of the SPService class.

Use an indexer to return a single service instance from the collection. For example, if the collection is assigned to a variable named myServiceInstances, use myServiceInstances[index] in C#, or myServiceInstances(index) in Visual Basic, where index is either the name or the GUID that identifies the service instance.

Examples

The following example displays the server name and version numbers for all search service instances with a status of Online. The example requires using directives (Imports in Visual Basic) for both the Microsoft.SharePoint.Administration and Microsoft.SharePoint.Search.Administration namespaces.

Dim searchService As SPSearchService = SPFarm.Local.Services.GetValue<SPSearchService>"SPSearch"

If Not (searchService Is Nothing) Then
   Dim instances As SPServiceInstanceDependencyCollection = searchService.Instances

   Dim serviceInstance As SPServiceInstance

   For Each serviceInstance In  instances
      If serviceInstance.Status <> SPObjectStatus.Online Then
         GoTo ContinueForEach1
      Else
         Dim serverName As String = SPEncode.HtmlEncode(serviceInstance.Server.DisplayName)
         Response.Write(serverName + " == " + 
            serviceInstance.Version.ToString() + "<BR>")
      End If
      ContinueForEach1:
   Next serviceInstance
End If
SPSearchService searchService = SPFarm.Local.Services.GetValue<SPSearchService>("SPSearch");

if (searchService != null)
{
   SPServiceInstanceDependencyCollection instances = searchService.Instances;

   foreach (SPServiceInstance serviceInstance in instances)
   {
      if (serviceInstance.Status != SPObjectStatus.Online)
      {
         continue;
      }
      else
      {
         string serverName = 
            SPEncode.HtmlEncode(serviceInstance.Server.DisplayName);
         Response.Write(serverName + " == " + 
            serviceInstance.Version.ToString() + "<BR>");
      }
   }
}

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

SPServiceInstance Members

Microsoft.SharePoint.Administration Namespace