簡體   English   中英

使用 InstallUtil 時可以將命令行參數傳遞給 Service 類嗎?

[英]Can command line arguments be passed to Service class when using InstallUtil?

問題

我正在嘗試將參數從命令行傳遞到我的服務類,該服務類在服務安裝之前繼承了 ServiceBase。 我找到了一種在使用 InstallUtil 運行我的服務安裝程序時接受參數的方法。 但是,運行 ServiceBase.Run(new Service()); 的主函數在可以訪問這些參數之前觸發。 因此,我不知道如何在運行之前將命令行參數傳遞到我的 Service() 類中。

我使用 ConfigStream 作為靜態類來讀取和存儲文本配置文件中的參數。 我的目標是在安裝程序運行之前從配置文件導入設置並將它們應用到我的服務類。 我希望能夠從命令行中的輸入指向該配置文件的位置。

嘗試的解決方案

我已經嘗試將參數應用於安裝程序的 OnBeforeInstall 函數內的 ConfigStream 類,但它仍然在主函數之后運行,因此它不會將設置應用於我的服務類。

我也嘗試遵循微軟的教程,但也沒有運氣。 參數從未傳遞給 Main。 教程: 創建 Windows 服務 - 添加可選參數

主類


    public static class MainClass
    {
        ///Function: Main
        ///File: ServiceWrapperV2.cs
        ///Author: Luke Maple
        ///Purpose: Main function of program, start up the service portion of program.
        static void Main(String[] args)
        {
            // Test if input arguments were supplied
            Console.WriteLine("Args: ");
            Console.WriteLine(args);
            if (args.Length > 0)
            {
                // set config location if provided
                // otherwise assume in working directory
                ConfigStream.setConfigstream(args[0]);
            }
            // run service
            ServiceBase.Run(new Service());
        }
    }

安裝程序嘗試 1


    [RunInstaller(true)]
    public class ServiceWrapperInstaller : Installer
    {
        private ServiceProcessInstaller processInstaller;
        private ServiceInstaller serviceInstaller;

        public ServiceWrapperInstaller()
        {
            //inilizing installer objects 
            processInstaller = new ServiceProcessInstaller();
            serviceInstaller = new ServiceInstaller();

            //Service will use the windows local system acount. Service will be run as admin
            processInstaller.Account = ServiceAccount.LocalSystem;

            //sets service start mode to automatic. service will auto boot on restarts
            serviceInstaller.StartType = ServiceStartMode.Automatic;

            // set service parameters
            // Console.WriteLine(ConfigStream.getSetting("Service Name"));
            serviceInstaller.ServiceName = ConfigStream.getSetting("Service Name");
            // Console.WriteLine("\nService Name: " + serviceInstaller.ServiceName + "\n");
            serviceInstaller.Description = ConfigStream.getSetting("Service Description");

            //passing object to be installed
            Installers.Add(serviceInstaller);
            Installers.Add(processInstaller);
        }

        private void _setConfigLocation()
        {
            if (Context.Parameters.ContainsKey("config"))
            {
                ConfigStream.setConfigstream(Context.Parameters["config"]);
            }
        }

        protected override void OnBeforeInstall(IDictionary savedState)
        {
            _setConfigLocation();
            base.OnBeforeInstall(savedState);
        }
    }

安裝程序嘗試 2


    [RunInstaller(true)]
    public class ServiceWrapperInstaller : Installer
    {
        private ServiceProcessInstaller processInstaller;
        private ServiceInstaller serviceInstaller;

        public ServiceWrapperInstaller()
        {
            //inilizing installer objects 
            processInstaller = new ServiceProcessInstaller();
            serviceInstaller = new ServiceInstaller();

            //Service will use the windows local system acount. Service will be run as admin
            processInstaller.Account = ServiceAccount.LocalSystem;

            //sets service start mode to automatic. service will auto boot on restarts
            serviceInstaller.StartType = ServiceStartMode.Automatic;

            // set service parameters
            // Console.WriteLine(ConfigStream.getSetting("Service Name"));
            serviceInstaller.ServiceName = ConfigStream.getSetting("Service Name");
            // Console.WriteLine("\nService Name: " + serviceInstaller.ServiceName + "\n");
            serviceInstaller.Description = ConfigStream.getSetting("Service Description");

            //passing object to be installed
            Installers.Add(serviceInstaller);
            Installers.Add(processInstaller);
        }

        protected override void OnBeforeInstall(IDictionary savedState)
        {
            string parameter = "Config";
            Context.Parameters["assemblypath"] = "\"" + Context.Parameters["assemblypath"] + "\" \"" + parameter + "\"";
            base.OnBeforeInstall(savedState);
        }
    }

配置流


    public static class ConfigStream
    {
        // set class properties of ConfigStream
        public static string config { get; private set; } = GlobalVariables.cwd + "\\" + GlobalVariables.configFileName;

        // constructor with config as an argument
        public static void setConfigstream(string config_location)
        {
            string config = config_location;
        }

        ///Function: (static) getSetting
        ///Purpose: get requested value form formatted config file
        public static string getSetting(string setting)
        {
        ...
        }
    ...
    }

你可以嘗試這樣的事情:

[RunInstaller(true)]
public partial class ServiceWrapperInstaller  : Installer
{
    private const string nameKey = "name";
    private const string displayNameKey = "displayname";
    private const string descriptionKey = "description";

    protected override void OnBeforeInstall(IDictionary savedState)
    {
        // Set installer parameters
        SetParameters();

        base.OnBeforeInstall(savedState);
    }

    private void SetParameters()
    {
        // Set service name
        _serviceInstaller.ServiceName = this.Context.Parameters[nameKey];

        // Set the display name (if provided)
        if (Context.Parameters.ContainsKey(displayNameKey))
        {
            _serviceInstaller.DisplayName = this.Context.Parameters[displayNameKey];
        }

        // Set the description (if provided)
        if (Context.Parameters.ContainsKey(descriptionKey))
        {
            _serviceInstaller.Description = this.Context.Parameters[descriptionKey];
        }

        _serviceInstaller.StartType = ServiceStartMode.Automatic;
        _serviceInstaller.DelayedAutoStart = true;
    }
}

你可以這樣使用它:

InstallUtil /u /name= /displayname=<\\"Display Name\\"> /description=<\\"服務描述\\"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM