4

次の方法で Windows サービスを機能させるにはどうすればよいですか...

1.) インストール後に自動的に起動する

2.) 実行可能ファイルをダブルクリックするだけでも自動的に起動します

つまり、「NET START」、「SC」コマンドを使用したくないし、サービス コンソールから開始したくありません。サービスを自動インストールして自動起動したいだけです...さらに、実行可能ファイルをダブルクリックすると自動的に起動します。

ありがとう。

4

4 に答える 4

4

ServiceControllerクラスを見てください。

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();
    }
}
于 2010-07-03T16:05:36.030 に答える
2

Topshelf プロジェクト ( http://topshelf-project.com ) を見て、.NET で Windows サービスを作成する複雑さをすべて解消してください。すべての自己登録を処理し、アプリケーションからサービス コードへのすべての依存関係を排除します。

また、オープンソースであり、GitHub でホストされているため、あらゆるアプリケーションに簡単に適応できます。

(完全な開示、私はプロジェクトの著者の 1 人です)

于 2010-07-03T16:22:45.433 に答える
1

インストーラーを呼び出すコマンド ライン引数 ( use 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();           
     }
}
于 2010-07-03T16:14:05.193 に答える
0

私の投稿は、オプションを使用してコマンド ラインから Windows サービスをインストールする方法を示しています-install。このロジックを拡張して-startオプションを設定し、そのオプションを含むショートカットをデスクトップに作成できます。

于 2010-07-03T16:07:09.597 に答える