Condividi tramite


SPService class

Represents a farm-wide service.

Inheritance hierarchy

System.Object
  Microsoft.SharePoint.Administration.SPAutoSerializingObject
    Microsoft.SharePoint.Administration.SPPersistedObject
      Microsoft.SharePoint.Administration.SPPersistedUpgradableObject
        Microsoft.SharePoint.Administration.SPService
          

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

Syntax

'Declaration
<GuidAttribute("DACA2A15-B9B5-43da-BEA3-6B75FBE3A883")> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
Public Class SPService _
    Inherits SPPersistedUpgradableObject
'Usage
Dim instance As SPService
[GuidAttribute("DACA2A15-B9B5-43da-BEA3-6B75FBE3A883")]
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
public class SPService : SPPersistedUpgradableObject

Remarks

To return the parent service of a service instance, use the Service property of the SPServiceInstance class. Use the Services property of the SPFarm class to return an SPServiceCollection object that represents the collection of services on the server farm. Use an indexer to return a single service from the collection using the GUID that identifies the service. For example, if the collection is assigned to a variable named myServices, use myServices[index] in C#, or myServices(index) in Visual Basic, where index is the GUID that identifies the service.To retrieve a single service from the collection by name, use the GetValue method. For example, if the collection is assigned to a variable named myServices, use myServices.GetValue<ServiceType>(name) in C#, or myServices.GetValue(ServiceType)(name) in Visual Basic, where ServiceType is the type of the service and name is the name that identifies the service.

Examples

The following example iterates through the timer jobs history for each service in the farm and reruns any timer jobs that have failed in the past hour.

Dim oneHourAgo As DateTime = DateTime.UtcNow.AddHours(-1)
Dim rerunJobIds As List(Of Guid) = New List(Of Guid)()
For Each service As SPService In SPFarm.Local.Services
    For Each entry In service.JobHistoryEntries
        ' stop if the entry didn't occur in the last hour
        If entry.EndTime < oneHourAgo Then
            Exit For
        End If

        If entry.Status = SPRunningJobStatus.Failed And _
           Not rerunJobIds.Contains(entry.JobDefinitionId) Then
            Dim job As SPJobDefinition = SPFarm.Local.GetObject( _
                entry.JobDefinitionId)
            If Not job Is Nothing Then
                job.RunNow()

                ' don't rerun the same job twice
                rerunJobIds.Add(entry.JobDefinitionId)
            End If
        End If
    Next
Next
DateTime oneHourAgo = DateTime.UtcNow.AddHours(-1);
List<Guid> rerunJobIds = new List<Guid>();
foreach (SPService service in SPFarm.Local.Services)
{
    foreach (SPJobHistory entry in service.JobHistoryEntries)
    {
        // stop if the entry didn't occur in the last hour
        if (entry.EndTime < oneHourAgo)
            break;

        if (entry.Status == SPRunningJobStatus.Failed &&
            !rerunJobIds.Contains(entry.JobDefinitionId))
        {
            SPJobDefinition job = SPFarm.Local.GetObject(
                entry.JobDefinitionId) as SPJobDefinition;
            if (job != null)
            {
                job.RunNow();

                // don't rerun the same job twice.
                rerunJobIds.Add(entry.JobDefinitionId);
            }
        }
    }
}

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

SPService members

Microsoft.SharePoint.Administration namespace

Inheritance hierarchy

System.Object
  Microsoft.SharePoint.Administration.SPAutoSerializingObject
    Microsoft.SharePoint.Administration.SPPersistedObject
      Microsoft.SharePoint.Administration.SPPersistedUpgradableObject
        Microsoft.SharePoint.Administration.SPService
          Microsoft.SharePoint.Administration.SPDiagnosticsServiceBase
          Microsoft.SharePoint.Administration.SPIisWebService
          Microsoft.SharePoint.Administration.SPIncomingEmailService
          Microsoft.SharePoint.Administration.SPOutboundMailService
          Microsoft.SharePoint.Administration.SPRequestManagementService
          Microsoft.SharePoint.Administration.SPUsageService
          Microsoft.SharePoint.Administration.SPWebService
          Microsoft.SharePoint.Administration.SPWindowsService