我正在尝试编写一个小控制台应用程序来显示 SharePoint 2010 网站上正在运行的服务应用程序的列表。我使用过 Microsoft.SharePoint 和 Microsoft.SharePoint.Administration,但到目前为止我运气不佳。下面是我一直在摆弄的东西。谁能给我一些关于如何正确使用 SPServiceApplicationCollection 的指示?
提前致谢!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ServiceProcess;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SPServiceApplicationCollection services = new SPServiceApplicationCollection(String, SPFarm.Local.Services);
foreach (SPServiceApplication service in services)
{
Console.WriteLine(service.Name);
if (service is SPWebService)
{
SPWebService webService = (SPWebService)service;
foreach (SPWebApplication webApp in webService.WebApplications)
{
Console.WriteLine(webApp.Name);
Console.ReadLine();
}
}
}
}
}
}
编辑 经过一番挖掘/询问后,我想出了一个粗略的解决方案来解决我想要的问题。为了将来参考/希望做这种事情的任何其他人,我可以通过执行以下操作获取已部署服务器的列表以及应用程序名称:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ServiceProcess;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Administration.Health;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var solution = SPFarm.Local.Solutions["Your Service Application Name.wsp"];
string serverName = string.Empty;
foreach (SPServer server in solution.DeployedServers)
{
serverName += server.Name;
Console.WriteLine(server.Name);
}
if (solution != null)
{
if (solution.Deployed)
{
Console.WriteLine("{0} is currently deployed on: {1}", solution.Name, serverName);
Console.ReadLine();
}
else
{
Console.WriteLine("Error! Solution not deployed!");
Console.ReadLine();
}
}
}
}
}