Click here to Skip to main content
15,895,746 members
Articles / Programming Languages / C#

Create a Web Service Method to Manage a NT Service

Rate me:
Please Sign up or sign in to vote.
4.38/5 (18 votes)
17 Nov 2006CPOL2 min read 45.8K   26  
Create a web service method to manage a NT service
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.ServiceProcess;
using System.Security.Principal;

namespace SiccoloWebServiceEventProcessor
{
	
	
	public class NTServiceOperations 
	{
		public const string Stop = "stop";
		public const string Start = "start";
		public const string Pause = "pause";
		public const string Resume = "resume";
	};

	public class ServiceProcessor : System.Web.Services.WebService
	{
		public ServiceProcessor()
		{
			//CODEGEN: This call is required by the ASP.NET Web Services Designer
			InitializeComponent();
			//
		}

		#region Component Designer generated code
		
		//Required by the Web Services Designer 
		private IContainer components = null;
				
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if(disposing && components != null)
			{
				components.Dispose();
			}
			base.Dispose(disposing);		
		}
		
		#endregion



		[WebMethod]
		public bool RestartNTService(string RemoteServerAddress , 
									string NTServiceName, 
									out string NewServiceStatus  , 
									out bool CanStop, 
									out bool CanPauseAndContinue,
									out string ErrorInfo )
		{
	
			try
			{
		
				string ToDebugSetting = System.Configuration.ConfigurationSettings.AppSettings.Get("DebugMode");
				bool ToDebug = (ToDebugSetting!="");

				ErrorInfo="";
				NewServiceStatus ="";
				CanStop = false;
				CanPauseAndContinue = false;

				System.Security.Principal.WindowsImpersonationContext impersonationContext;
				impersonationContext = 
					((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();
			
				NTServiceInfo si = new NTServiceInfo(NTServiceName, RemoteServerAddress);

				if ( ! si.RestartServervice ((WindowsPrincipal) this.User,ToDebug,  out ErrorInfo))
				{return false;}

				NewServiceStatus =  si.ServiceStatus();
				CanStop = si.CanStop();
				CanPauseAndContinue = si.CanPauseAndContinue();

				impersonationContext.Undo();

				ErrorInfo = "";
				return true;
			}
			catch (Exception ex_get_service_info)
			{
				NewServiceStatus ="";
				CanStop = false;
				CanPauseAndContinue = false;
				ErrorInfo = ex_get_service_info.Message;
				return false;
			}

		}

		[WebMethod]
		public bool RestartSQLServerService(string RemoteServerAddress , 
											out string NewServiceStatus  , 
											out string ErrorInfo )
		{
	
			try
			{
		
				string ToDebugSetting = System.Configuration.ConfigurationSettings.AppSettings.Get("DebugMode");
				bool ToDebug = (ToDebugSetting!="");

				string NTServiceName = "MSSQLSERVER";

				ErrorInfo="";
				NewServiceStatus ="";

				System.Security.Principal.WindowsImpersonationContext impersonationContext;
				impersonationContext = 
					((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();
			
				NTServiceInfo si = new NTServiceInfo(NTServiceName, RemoteServerAddress);

				if ( ! si.RestartServervice ((WindowsPrincipal) this.User,ToDebug,  out ErrorInfo))
				{return false;}

				NewServiceStatus =  si.ServiceStatus();

				impersonationContext.Undo();

				ErrorInfo = "";
				return true;
			}
			catch (Exception ex_get_service_info)
			{
				NewServiceStatus ="";
				ErrorInfo = ex_get_service_info.Message;
				return false;
			}

		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions