Reputation: 67
I am not able to find the class which is used to start the web service on remote server using a code like we have for window service.
var sc = new System.ServiceProcess.ServiceController("mywebsite", "remoteservername");
sc.Start();
sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running);
sc.Stop();
sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Stopped);
Upvotes: 0
Views: 2222
Reputation: 67
static void Main(string[] args) {
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)
{
}
}
Upvotes: 0
Reputation: 2900
Web service is not listed under Windows services. It is running under IIS and to stop/start it you'll need to stop / start Application Pool under which this service is running. If you are planning to do it remotely, WMI needs to be enabled on target server. For your convenience providing a code that will do this for you:
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);
}
}
Note:
How to enable WMI on the server
Upvotes: 2