SPService 类
表示服务器场范围内的服务。
继承层次结构
System.Object
Microsoft.SharePoint.Administration.SPAutoSerializingObject
Microsoft.SharePoint.Administration.SPPersistedObject
Microsoft.SharePoint.Administration.SPPersistedUpgradableObject
Microsoft.SharePoint.Administration.SPService
命名空间: Microsoft.SharePoint.Administration
程序集: Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)
语法
声明
<GuidAttribute("DACA2A15-B9B5-43da-BEA3-6B75FBE3A883")> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
Public Class SPService _
Inherits SPPersistedUpgradableObject
用法
Dim instance As SPService
[GuidAttribute("DACA2A15-B9B5-43da-BEA3-6B75FBE3A883")]
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
public class SPService : SPPersistedUpgradableObject
备注
若要返回一个服务实例的父服务,使用SPServiceInstance类的Service属性。使用SPFarm类的Services属性返回一个SPServiceCollection对象,表示集合的服务器场上的服务。使用索引器从集合使用 GUID 标识该服务返回一个服务。例如,如果该集合分配给名为myServices的变量中,使用在 C# myServices[index]或myServices(index)在 Visual Basic,其中index是 GUID 标识该服务。若要按名称从集合中检索单个服务,请使用GetValue方法。例如,如果该集合分配给名为myServices的变量中,使用在 C# myServices.GetValue<ServiceType>(name)或myServices.GetValue(ServiceType)(name)在 Visual Basic,其中ServiceType是服务和name的类型是用于标识该服务的名称。
示例
下面的示例循环访问服务器场中的每个服务的计时器作业历史记录,并重新运行失败过去小时任何计时器作业。
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);
}
}
}
}
线程安全性
该类型的任何公共 静态 (已共享 在 Visual Basic 中) 成员都是线程安全的。不保证任何实例成员都是线程安全的。
另请参阅
引用
Microsoft.SharePoint.Administration 命名空间
继承层次结构
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