[英]How to start/stop a windows service from C#
我在应用程序中具有以下三个用户角色。
我只想为具有SuperAdmin用户角色的用户启动/停止服务。
该应用程序的需要是从ASP.NET Web应用程序启动/停止服务(或运行您要启动/停止的其他任何服务)。
使用服务控制管理器来启动/停止服务,而不是SHUTDOWN
T-SQL动词。 SCM知道有关服务的依赖关系,并将正确关闭依赖的服务,例如SQL Agent。 此外,只有SCM可以启动服务。 SCM的托管API是ServiceController
类。 这解决了启动/停止服务的问题。
现在,关于假冒。 为什么要照顾? 确定用户是否为“超级管理员”(无论意味着什么)是您应用程序的特定逻辑。 在您的ASP.Net逻辑中进行身份验证和授权,然后向应用程序池主体授予启动和停止服务所需的特权。 请参阅服务安全性和访问权限 。 请注意,本地身份验证用户如何具有连接SCM和启动/停止服务的所有必需特权。
如果您确实选择模拟,则将在模拟上下文中拥有必要的特权。 Windows中没有“超级管理员”这样的概念,您将模拟一个Active Directory帐户,并且该帐户将具有或没有连接到SCM和启动/停止服务的权限。
在上述情况下,我必须使用c#从Asp.Net Web应用程序开始停止我的Monitoring Server服务。
添加System.ServiceProcess
作为默认的Start();
Stop();
Refresh();
职能
using System.ServiceProcess;
private readonly ServiceController _monitoringServer = new ServiceController("MONITORING$SERVER");
// on login screen, I just saved the userRole of a Loggedin user in Session["userRole"]
// On pageload, refresh the sql service and gets its current status.
protected void Page_Load(object sender, EventArgs e)
{
_monitoringServer.Refresh();
GetServiceCurrentStatus();
// if userRole of loggedIn user is SuperAdmin, then it will make the buttons visible to the user
// else it will hide the buttons and show only status of SQL SERVER to admin user and standard/limited user
if(Session["userRole"] == "SuperAdmin"){
this.btnStartServer.Visible = true;
this.btnStopServer.Visible = true;
}
else{
this.btnStartServer.Visible = false;
this.btnStopServer.Visible = false;
}
// You can further login to show hide Start Stop buttons
}
// Start Server Function
protected void btnStartServer_Click(object sender, EventArgs e)
{
_monitoringServer.Refresh();
if (_monitoringServer.Status == ServiceControllerStatus.Stopped)
{
_monitoringServer.Start();
_monitoringServer.Refresh();
}
GetServiceCurrentStatus();
}
// Stop Server Function
protected void btnStopServer_Click(object sender, EventArgs e)
{
_monitoringServer.Refresh();
if (_monitoringServer.Status == ServiceControllerStatus.Running)
{
_monitoringServer.Stop();
_monitoringServer.Refresh();
}
GetServiceCurrentStatus();
}
// Get Current Status of SQL Service Function
private void GetServiceCurrentStatus()
{
try
{
_monitoringServer.Refresh();
if (_monitoringServer.Status == ServiceControllerStatus.Running)
this.txtServerStatus.Text = "Monitoring Server: Running";
else if (_monitoringServer.Status == ServiceControllerStatus.Stopped)
this.txtServerStatus.Text = "Monitoring Server: Stopped";
else if (_monitoringServer.Status == ServiceControllerStatus.StartPending)
this.txtServerStatus.Text = "Monitoring Server: Starting...";
else if (_monitoringServer.Status == ServiceControllerStatus.StopPending)
this.txtServerStatus.Text = "Monitoring Server: Stopping...";
else if (_monitoringServer.Status == ServiceControllerStatus.Paused || _monitoringServer.Status == ServiceControllerStatus.PausePending)
this.txtServerStatus.Text = "Monitoring Server: Pause";
else
this.txtServerStatus.Text = "Monitoring Server: Processing";
}
catch (Exception)
{
this.txtServerStatus.Text = "Monitoring Server: Not Installed";
}
}
// Timer Function
protected void ServerTimer_OnTick(object sender, EventArgs e)
{
_monitoringServer.Refresh();
}
// On client side, I just used a timer and sets its interval to 2 seconds (2000ms)
// after every 2 seconds it executes the ServerTimer_OnTick function which refreshes the status
<asp:Timer ID="ServerTimer" runat="server" Interval="2000" OnTick="ServerTimer_OnTick"></asp:Timer>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.