asp.net 控制windows服務
asp.net 控制windows服務 最近做一個小的專案,經理要求winform與web都可操作。於是將其做成服務,winform下呼叫很簡單。asp.net下大部分過程是一樣的。但需要涉及到使用者的許可權,需要在web.config檔案中做下配置。
1、為網站新增新的引用,加入:system.serviceproces.dll
2、在程式碼檔案頭部加如:using System.ServiceProcess;
3、服務的啟動使用如下程式碼:
ServiceController sc = new ServiceController("service1");
if (sc.Status == ServiceControllerStatus.Stopped)
{
sc.Start();
}
4、服務的關閉使用如下程式碼:
ServiceController sc = new ServiceController("service1");
if (sc.Status == ServiceControllerStatus.Running)
{
sc.Stop();
}
5、服務的控制許可權的解決辦法
windows2003等伺服器系統都提供了完善的許可權訪問機制,這就是我為什麼不用前面說過的在asp.net 中呼叫另一個程式的辦法來通過net 命令啟動和停止服務。因為首先用net命令顯然比較麻煩,而呼叫新程式的發放程式執行依然是在aspnet的使用者下(通過網頁訪問的預設使用者),當網頁關閉,會話結束時間到達就會自動釋放該使用者執行的相關程式。而net命令同樣需要相應許可權。asp.net 中可以在web.config的下新增
從而解決使用者的登入許可權問題。
以上五步就可以完成對伺服器服務的控制了!
<system.web>
<identity impersonate="true" userName="admin" password="admin"/>
1、為網站新增新的引用,加入:system.serviceproces.dll
2、在程式碼檔案頭部加如:using System.ServiceProcess;
3、服務的啟動使用如下程式碼:
ServiceController sc = new ServiceController("service1");
if (sc.Status == ServiceControllerStatus.Stopped)
{
sc.Start();
}
4、服務的關閉使用如下程式碼:
ServiceController sc = new ServiceController("service1");
if (sc.Status == ServiceControllerStatus.Running)
{
sc.Stop();
}
5、服務的控制許可權的解決辦法
windows2003等伺服器系統都提供了完善的許可權訪問機制,這就是我為什麼不用前面說過的在asp.net 中呼叫另一個程式的辦法來通過net 命令啟動和停止服務。因為首先用net命令顯然比較麻煩,而呼叫新程式的發放程式執行依然是在aspnet的使用者下(通過網頁訪問的預設使用者),當網頁關閉,會話結束時間到達就會自動釋放該使用者執行的相關程式。而net命令同樣需要相應許可權。asp.net 中可以在web.config的下新增
從而解決使用者的登入許可權問題。
以上五步就可以完成對伺服器服務的控制了!
<system.web>
<identity impersonate="true" userName="admin" password="admin"/>
相關文章
- windows 服務中託管asp.net coreWindowsASP.NET
- SC-控制Windows服務的命令薦Windows
- C#製作可控制介面的windows服務C#Windows
- Windows 服務管理Windows
- windows配置MySql服務WindowsMySql
- Windows服務詳解Windows
- windows 服務例項Windows
- win10啟動windows服務顯示服務沒有響應控制功能怎麼解決Win10Windows
- ElasticSearch註冊Windows服務ElasticsearchWindows
- c#寫windows服務C#Windows
- Windows手工建立服務方法Windows
- 使用C#建立windows服務續之使用Topshelf優化Windows服務C#Windows優化
- ASP.NET Web 服務、企業服務和 .NET Remoting 的效能ASP.NETWebREM
- windows下啟動nacos服務Windows
- windows開啟ssh服務教程Windows
- Windows關閉Microsoft Defender服務WindowsROS
- Windows服務建立及安裝Windows
- Windows服務程式圖文教程Windows
- 控制linux啟動的服務Linux
- 引導過程與服務控制
- asp.net core服務的生命週期ASP.NET
- ASP.NET如何定時呼叫WebService服務ASP.NETWeb
- Dubbo Mesh - 從服務框架到統一服務控制平臺框架
- 將windows應用程式註冊為windows服務Windows
- windows下安裝Tomcat服務WindowsTomcat
- Windows如何安裝自定義服務Windows
- 使用InstallUtil釋出windows服務Windows
- PHP 程式跑在 Windows 服務上PHPWindows
- 將IoTdb註冊為Windows服務Windows
- windows系統IIS服務安裝Windows
- gitblit服務端在windows的搭建Git服務端Windows
- windows平臺下配置cron服務Windows
- Windows Service外掛服務開源Windows
- 使用ASP.NET web API建立REST服務(二)ASP.NETWebAPIREST
- 《遠端控制》-服務端實現(一)服務端
- windows下注冊表中控制例項隨著服務啟動和停止的選項Windows
- windows 使用掛載盤提供FTP服務WindowsFTP
- Windows服務除錯小結(附Demo)Windows除錯