如何讓我的Windows服務,以通過以下方式工作...雙擊啓動Windows服務
1)自動啓動安裝
2)自動即使啓動後,我們簡單地雙擊可執行文件
換句話說,我不想使用「NET START」,「SC」命令並且不想通過服務控制檯啓動它。我只想讓我的服務自動安裝並自動啓動自身...當可執行文件被雙擊時自動啓動。
謝謝。
如何讓我的Windows服務,以通過以下方式工作...雙擊啓動Windows服務
1)自動啓動安裝
2)自動即使啓動後,我們簡單地雙擊可執行文件
換句話說,我不想使用「NET START」,「SC」命令並且不想通過服務控制檯啓動它。我只想讓我的服務自動安裝並自動啓動自身...當可執行文件被雙擊時自動啓動。
謝謝。
看看在Topshelf項目(http://topshelf-project.com),並消除所有的複雜性用.NET編寫Windows服務。它處理所有的自注冊並消除應用程序對服務代碼的所有依賴。
它也是開源的,託管在GitHub上,可以很容易地適應任何應用程序。
(全面披露,我是該項目的作者之一)
看看ServiceController class。
可以在commited
事件中使用這樣的:
[RunInstaller(true)]
public class ServiceInstaller : Installer
{
string serviceName = "MyServiceName";
public ServiceInstaller()
{
var processInstaller = new ServiceProcessInstaller();
var serviceInstaller = new ServiceInstaller();
processInstaller.Account = ...;
processInstaller.Username = ...;
processInstaller.Password = ...;
serviceInstaller.DisplayName = serviceName;
serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.ServiceName = serviceName;
this.Installers.Add(processInstaller);
this.Installers.Add(serviceInstaller);
this.Committed += new InstallEventHandler(ServiceInstaller_Committed);
}
void ServiceInstaller_Committed(object sender, InstallEventArgs e)
{
// Auto Start the Service Once Installation is Finished.
var controller = new ServiceController(serviceName);
controller.Start();
}
}
您可以添加調用安裝程序(使用ManagedInstallerClass.InstallHelper()
),和代碼來啓動服務的命令行參數......
public class DataImportService : ServiceBase
{
// ----------- Other code -----------
static void Main(string[] args)
{
if (args.Length == 0)
{
InstallService(false, argValue); break;
StartService();
}
else
{
string arg0 = args[0],
switchVal = arg0.ToUpper(),
argValue = arg0.Contains(":") ?
arg0.Substring(arg0.IndexOf(":")) : null;
switch (switchVal.Substring(0, 1))
{
//Install Service and run
case ("I"): case ("-I"): case ("/I"):
InstallService(true, argValue); break;
// Start Service
case ("S"): case ("-S"): case ("/S"):
StartService();
default: break;
// Install & Start Service
case ("IS"): case ("-IS"): case ("/IS"):
InstallService(false, argValue); break;
StartService();
// Uninstall Service
case ("U"): case ("-U"): case ("/U"):
InstallService(false, argValue); break;
default: break;
}
}
private static void InstallService(bool install, string argFileSpec)
{
string fileSpec = Assembly.GetExecutingAssembly().Location;
if (!String.IsNullOrEmpty(argFileSpec)) fileSpec = argFileSpec;
// ------------------------------------------------------------
string[] installerParams =
install? new string[] { fileSpec } :
new string[] { "/u", fileSpec };
ManagedInstallerClass.InstallHelper(installerParams);
}
private void StartService()
{
var ctlr = new ServiceController();
ctlr.ServiceName = "MyService"; // hard code the service name
// Start the service
ctlr.Start();
}
}
開始服務怎麼樣?我需要通過雙擊它來啓動服務...... – Josh 2010-07-06 07:36:38
@Josh,使用'ServiceController()'類來啓動和停止服務。你將需要決定如何編寫這個Main例程來做到這一點......這取決於你... – 2010-07-06 13:28:26
試過你的項目......但是SERVICE參數不起作用。它就像一個普通的windows服務。它說「 --------------------------- Windows服務啓動失敗 ----------------- ---------- 無法從命令行或調試程序啓動服務必須首先安裝Windows服務(使用installutil.exe),然後以ServerExplorer,Windows服務管理工具或NET START啓動命令 --------------------------- OK ----------------- ---------- 「。 – Josh 2010-07-06 06:50:37
你需要創建自己的EXE,你可以按照這裏的播放:http://topshelf-project.com/documentation/getting-started/ – 2010-07-07 02:53:32
謝謝...和下載鏈接不工作在您的網站上。 – Josh 2010-07-15 12:19:10