Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 安装使用Topshelf制作的Windows服务时添加参数_C#_.net_Windows Services_Topshelf - Fatal编程技术网

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

C# 安装使用Topshelf制作的Windows服务时添加参数,c#,.net,windows-services,topshelf,C#,.net,Windows Services,Topshelf,我一直在学习如何使用Topshelf在C#中创建windows服务。 该服务工作正常,但我在安装时添加自己的参数时遇到一些问题 安装时,我希望为该服务提供一个connectionstring(connStr),以后需要使用它。现在,该服务只需将“connectionstring是…”写入文本文件,以进行测试 下面是我的代码,我使用Topshelf将我的“ServiceClass”转换为windows服务。 “ServiceClass”在其构造函数中获取connectionstring 我已经添加

我一直在学习如何使用Topshelf在C#中创建windows服务。 该服务工作正常,但我在安装时添加自己的参数时遇到一些问题

安装时,我希望为该服务提供一个connectionstring(
connStr
),以后需要使用它。现在,该服务只需将“connectionstring是…”写入文本文件,以进行测试

下面是我的代码,我使用Topshelf将我的“ServiceClass”转换为windows服务。 “ServiceClass”在其构造函数中获取connectionstring

我已经添加了很多次行
Console.WriteLine(“调试器…”)
,这样我就可以看到安装时发生了什么

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

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

有人知道我做错了什么吗

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

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;

        }
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.