安装使用 Topshelf 制作的 Windows 服务时添加参数

问题描述

我一直在学习如何使用 Topshelf 在 C# 中创建 Windows 服务。 该服务运行良好,但在安装时我在添加自己的参数时遇到了一些麻烦。

安装时,我想为该服务提供一个稍后需要使用的连接字符串 (connStr)。现在,该服务只会将“连接字符串是……”写入文本文件,以进行测试。

以下是我的代码,我使用 Topshelf 将我的“ServiceClass”转换为 Windows 服务。 “ServiceClass”在其构造函数获取连接字符串。

我多次添加Console.WriteLine("DEBUGGER ...") 行,以便我可以查看安装时发生的情况。

当我使用命令提示符安装服务时,程序似乎“忘记”了我在“ServiceClass”的构造函数中给它的字符串。

static void Main(string[] args)
        {
            string connStr = null;  //ConnectionString
            Console.WriteLine("DEBUGGER 0");

            var exitCode = HostFactory.Run(x =>
            {
                x.AddCommandLineDeFinition("connStr",f =>
                {
                    connStr = f;
                    Console.WriteLine("DEBUGGER 1,connStr = " + f);
                });
                x.ApplyCommandLine();

                Console.WriteLine("DEBUGGER 2");

                x.Service<ServiceClass>(s =>
                {
                    s.ConstructUsing(sc => new ServiceClass(connStr));
                    s.WhenStarted(sc => sc.Start());
                    s.WhenStopped(sc => sc.Stop());
                    Console.WriteLine("DEBUGGER 3");
                });

                //Service-properties
                x.RunAsLocalSystem();
                x.SetdisplayName("Loader Service");
                x.SetServiceName("LoaderService");
                x.SetDescription("Some description");
                x.StartAutomatically();

                Console.WriteLine("DEBUGGER 4");

                x.BeforeInstall(() => { });
                x.AfterInstall(() => { });
                x.BeforeUninstall(() => { });
                x.AfterUninstall(() => { });

                Console.WriteLine("DEBUGGER 5");

            });

            int exitCodeValue = (int)Convert.ChangeType(exitCode,exitCode.GetTypeCode());
            Environment.ExitCode = exitCodeValue;

        }

当我使用命令提示符和命令 LoaderService install -connStr:"TestConnectionString" 进行安装时,我得到以下结果,我觉得这有点令人困惑。

C:\Users\behe\source\Workspaces\LoaderV5\Loader\LoaderService\bin\Debug>LoaderService install -connStr:"TestConnectionString"
DEBUGGER 0
DEBUGGER 1,connStr = "TestConnectionString"
DEBUGGER 2
DEBUGGER 3,connStr = "TestConnectionString"
DEBUGGER 4
DEBUGGER 5
DEBUGGER 1,connStr = "TestConnectionString"
Configuration Result:
[Success] Name LoaderService
[Success] displayName Loader Service
[Success] Description Some description
[Success] ServiceName LoaderService
Topshelf v4.3.0.0,.NET Framework 4.8.4341.0 (4.0.30319.42000)

Running a transacted installation.

Beginning the Install phase of the installation.
Installing Loader Service service
Installing service LoaderService...
Service LoaderService has been successfully installed.

The Install phase completed successfully,and the Commit phase is beginning.

The Commit phase completed successfully.

The transacted install has completed.

如您所见,“DEBUGGER”0-5 行都以正确的顺序出现,并且“connStr”具有正确的值。 但是,由于某种原因“DEBUGGER 1”被第二次调用,我不明白为什么。

有人知道我在这里做错了什么吗?

当我在调试模式下(使用命令行参数)运行与控制台应用程序相同的程序时,一切正常。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)