C# 如何用c语言在远程服务器上实际重启web服务
我找不到用于在远程服务器上启动web服务的类,该类使用的代码与Windows服务类似C# 如何用c语言在远程服务器上实际重启web服务,c#,asp.net,asp.net-mvc,c#-4.0,c#-3.0,C#,Asp.net,Asp.net Mvc,C# 4.0,C# 3.0,我找不到用于在远程服务器上启动web服务的类,该类使用的代码与Windows服务类似 var sc = new System.ServiceProcess.ServiceController("mywebsite", "remoteservername"); sc.Start(); sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running); sc.
var sc = new System.ServiceProcess.ServiceController("mywebsite", "remoteservername");
sc.Start();
sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running);
sc.Stop();
sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Stopped);
Windows服务下未列出Web服务。它正在IIS下运行,要停止/启动它,您需要停止/启动运行此服务的应用程序池。如果您计划远程执行此操作,则需要在目标服务器上启用WMI。为方便起见,请为您提供一个代码:
public void PoolAction(String servername, String AppPoolName, String action)
{
StringBuilder sb = new StringBuilder();
ConnectionOptions options = new ConnectionOptions();
options.Authentication = AuthenticationLevel.PacketPrivacy;
options.EnablePrivileges = true;
ManagementScope scope = new ManagementScope(@"\\" +
servername + "\\root\\MicrosoftIISv2", options);
// IIS WMI object IISApplicationPool to perform actions on IIS Application Pool
ObjectQuery oQueryIISApplicationPool =
new ObjectQuery("SELECT * FROM IISApplicationPool");
ManagementObjectSearcher moSearcherIISApplicationPool =
new ManagementObjectSearcher(scope, oQueryIISApplicationPool);
ManagementObjectCollection collectionIISApplicationPool =
moSearcherIISApplicationPool.Get();
foreach (ManagementObject resIISApplicationPool in collectionIISApplicationPool)
{
if (resIISApplicationPool["Name"].ToString().Split('/')[2] == AppPoolName)
{
// InvokeMethod - start, stop, recycle can be passed as parameters as needed.
resIISApplicationPool.InvokeMethod(action, null);
}
}
注:
操作可以包含“开始”、“停止”或“回收”
运行此代码的帐户必须是目标服务器上的管理员。
如何在服务器上启用WMI
静态无效字符串[]参数 {
try
{
using (ServerManager manager = new ServerManager())
{
var iisManager = ServerManager.OpenRemote("YourServerName");
Microsoft.Web.Administration.Site site = iisManager.Sites.Where(q => q.Name.Equals("YourSiteName")).FirstOrDefault();
if (site.State== site.Start())
{
site.Stop();
}
else
{
site.Start();
}
manager.CommitChanges();
}
}
catch (Exception ex)
{
}
}
有人能帮我吗?你试过我的建议吗?