微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决安装使用 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 举报,一经查实,本站将立刻删除。