如何使用c#启动未通过asp.net安装在计算机上的Windows服务? [英] How to start a windows service not installed on computer via asp.net with c#?I

查看:93
本文介绍了如何使用c#启动未通过asp.net安装在计算机上的Windows服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过带有c#的asp.net启动未安装在计算机上的Windows服务?我可以启动系统中安装的服务。

我的代码如下:

在web.config下



How to start a windows service not installed on computer via asp.net with c#?I am able to start the service installed in system.
My code as follows:
Under web.config

userName="Domain/Username" password="password" />
 

Under aspx.cs page
private void StartService(string serviceName, int timeoutMilliseconds)
{
Configuration objConfigFile;
//getting an instance of the configuration file
objConfigFile = WebConfigurationManager.OpenWebConfiguration(
HttpContext.Current.Request.ApplicationPath);
 
//getting an instance of the "identity" section of the cofiguration file
IdentitySection objIdentitySection =
(IdentitySection)objConfigFile.GetSection("system.web/identity");
 
if (objIdentitySection != null)
{
string username = objIdentitySection.UserName;
string password = objIdentitySection.Password;
bool impersonate = objIdentitySection.Impersonate;
//Configuration currentConfiguration = objIdentitySection.C.CurrentConfiguration;
 
//Obviously you won't be doing this. The lines below are just for testing purpose

 
ServiceController service = new ServiceController(serviceName);
try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
 
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
catch(Exception ex)
{
throw (ex);
}
}
 

}

推荐答案

请不要发表同样的问题:如何启动一个窗口服务没有通过带有c#的asp.net安装在计算机上? [ ^ ]。
Please do not post the same question : How to start a windows service not installed on computer via asp.net with c#?[^], using different signons.


请参考以下可能对您有用的链接



如何在不安装Windows服务的情况下调试或测试Windows服务...... [ ^ ]



使用ASP.NET模拟Windows服务来运行预定作业 [ ^ ]
Refer the below links which may help you

How to Debug or Test your Windows Service Without Installing it...[^]

Simulate a Windows Service using ASP.NET to run scheduled jobs[^]


这篇关于如何使用c#启动未通过asp.net安装在计算机上的Windows服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆