在 .Net 中测试 Windows 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5069327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:13:47  来源:igfitidea点击:

Testing Windows Service in .Net

c#.netwindowswindows-services

提问by mcamara

I'm developing a windows service in C# .Net and i'm having some dificulty to execute and test my service. I'm using a timer component to run it from time to time and execute my methods. I have to intialize the service all the time to run it. Anyone know a more practical way to test a service?

我正在 C# .Net 中开发 Windows 服务,但在执行和测试我的服务时遇到了一些困难。我正在使用计时器组件不时运行它并执行我的方法。我必须一直初始化服务才能运行它。有人知道测试服务的更实用的方法吗?

采纳答案by Tedd Hansen

You probably want to add unit tests to your service.

您可能希望将单元测试添加到您的服务中。

It sounds like you could benefit more from adding your application to the Task Schedulerinstead of running it as a service.

听起来您可以通过将应用程序添加到任务计划程序而不是将其作为服务运行来获益更多。

Other than that if you really need it as a service you need to design it so it can be tested. I usually write my services in a separate class and add a .EXE project to it so I can run it from the command line too.

除此之外,如果你真的需要它作为一项服务,你需要设计它以便它可以被测试。我通常在一个单独的类中编写我的服务并向其中添加一个 .EXE 项目,这样我也可以从命令行运行它。

回答by Fredrik M?rk

This often boils down to the question what you want to test. Do you want to test the service, or the functionality that it performs? Personally I would...

这通常归结为您想测试什么的问题。您要测试服务或其执行的功能吗?我个人会...

  • Refactor all of the service functionality into a separate class library
  • Test the library
  • Invoke the library functionality from the service
  • 将所有服务功能重构为一个单独的类库
  • 测试库
  • 从服务调用库功能

...and then keep the amount of code in the service itself to the bare minimum needed to trigger the functionality in the class library.

...然后将服务本身的代码量保持在触发类库中的功能所需的最低限度。

回答by Tim Robinson

Windows services are ordinary EXEs that happen to connect to the service control manager on startup.

Windows 服务是在启动时碰巧连接到服务控制管理器的普通 EXE。

I normally test services by giving them an optional command line argument that tells them to execute normally, inside the Mainmethod, instead of acting as a service. That way I can debug them directly within Visual Studio, or on the command line, etc.

我通常通过给它们一个可选的命令行参数来测试服务,该参数告诉它们在Main方法内部正常执行,而不是充当服务。这样我就可以直接在 Visual Studio 中或在命令行等上调试它们。

回答by Mike B

One of the ways I have run services when developing is along these lines - Run a windows service as a console. I have found it useful because you can add code to write out to the console for debug information and writing out any relevant exception data.

我在开发时运行服务的方法之一是沿着这些路线运行 Windows 服务作为控制台。我发现它很有用,因为您可以添加代码以将调试信息写出到控制台并写出任何相关的异常数据。

I would not use this method instead of unit tests but I found this to be a useful way to work and debug where needed.

我不会使用这种方法代替单元测试,但我发现这是一种在需要时进行工作和调试的有用方法。

回答by Vikash Sinha

After create window service you can set it into window service.

创建窗口服务后,您可以将其设置为窗口服务。

Install service using InstallUtil.exe To install or uninstall windows service (which was created using .NET Framework) use utility InstallUtil.exe. This tool can be found in the following path (use appropriate framework version number).

使用 InstallUtil.exe 安装服务 要安装或卸载 Windows 服务(使用 .NET Framework 创建),请使用实用程序 InstallUtil.exe。该工具可以在以下路径中找到(使用适当的框架版本号)。

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe

To install .NET service run command similar to this (specify full path to your service).

要安装 .NET 服务,请运行与此类似的命令(指定服务的完整路径)。

InstallUtil.exe "path of service"

InstallUtil.exe“服务路径”

To uninstall .NET service use the same command with parameter /u.

要卸载 .NET 服务,请使用带有参数 /u 的相同命令。

InstallUtil.exe /u "path of service"

InstallUtil.exe /u "服务路径"