C# 替代“允许服务与桌面交互”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2345620/
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
Alternative to "Allow service to interact with desktop"?
提问by jdecuyper
I have a windows service (C#) installed on a server that launches every 10 minutes an executable file (C#) to process some images from one directory to another. No interaction is required with any user. Nevertheless, since the executable file as an output window, to make the service run I have to enable the "Allow service to interact with desktop" checkbox which is considered as an insecure and bad practice. How would I go about this problem? I like to have the executable separated from my windows service because
我在服务器上安装了一个 Windows 服务 (C#),它每 10 分钟启动一个可执行文件 (C#) 来处理从一个目录到另一个目录的一些图像。不需要与任何用户交互。尽管如此,由于可执行文件作为输出窗口,为了使服务运行,我必须启用“允许服务与桌面交互”复选框,这被认为是不安全和不好的做法。我将如何解决这个问题?我喜欢将可执行文件与我的 Windows 服务分开,因为
- it makes it easier to debug and doesn't require a full windows service redeploy.
- sometimes I use the same windows service to launch several executable files at different intervals (but all related to the same project).
- 它使调试更容易,并且不需要重新部署完整的 Windows 服务。
- 有时我使用相同的 Windows 服务以不同的时间间隔启动多个可执行文件(但都与同一个项目相关)。
EDIT:
编辑:
When the interaction with the desktop is not enabled, the console application does not get executed correctly and the following error appears inside the Windows Logs:
未启用与桌面的交互时,控制台应用程序无法正确执行,Windows 日志中会出现以下错误:
Faulting application myapp.exe, version 1.0.0.0, time stamp 0x4b8304c3,
faulting module KERNEL32.dll, version 6.0.6002.18005, time stamp 0x49e03821,
exception code 0xc0000142, fault offset 0x00009eed, process id 0x10ec,
application start time 0x01cab736950a64b5.
Once the desktop interaction is enabled, application gets executed normally.
启用桌面交互后,应用程序将正常执行。
Any thoughts?
有什么想法吗?
Thanks a lot for your time.
非常感谢您的时间。
采纳答案by Weeble
If you are using Vista and later and you don't really need any interaction with the user, but have an interactive exe to execute, the Session 0 isolation featureshould help alleviate some of the concerns about the 'bad practice' on having a service interact with the desktop (which in Session 0 has no physical console).
如果您使用的是 Vista 及更高版本,并且您实际上并不需要与用户进行任何交互,但需要执行交互式 exe,则会话 0 隔离功能应该有助于减轻对使用服务的“不良做法”的一些担忧与桌面交互(在会话 0 中没有物理控制台)。
This Session 0 isolation would prevent unprivileged users from performing Shatter Attackson your service as they get their interactive desktops in different sessions. Shatter attacks are the main reason why this 'interaction with desktop' was considered bad practice and if you are using Vista or later, it should be ok if you cannot avoid it (or will have to spend too much effort to do it).
这种会话 0 隔离将防止非特权用户在您的服务上执行粉碎攻击,因为他们在不同的会话中获得他们的交互式桌面。粉碎攻击是这种“与桌面交互”被认为是不好的做法的主要原因,如果您使用的是 Vista 或更高版本,如果您无法避免它(或将不得不花费太多精力去做),那应该没问题。
So, if things are working fine as they are, you are probably ok.
所以,如果一切正常,你可能没问题。
Of course, after an OS update, things might just stop working, so it is probably better to prepare to move the dependency on interactivity out, as you don't really need it.
当然,在操作系统更新后,事情可能会停止工作,因此最好准备好消除对交互性的依赖,因为您并不真正需要它。
回答by Reed Copsey
If you can, I would recommend rewriting your executables that handle the move to not use an output window. If they are standard, console applications with no output, you can execute them from within a service without requiring "Allow service to interact with desktop". This provides you all of the benefits, without any changes to your service.
如果可以,我建议您重写处理移动的可执行文件以不使用输出窗口。如果它们是标准的、没有输出的控制台应用程序,您可以从服务中执行它们,而无需“允许服务与桌面交互”。这为您提供了所有好处,而无需对您的服务进行任何更改。
回答by Weeble
Is the subprocess just a console application? I've not written Windows Services, but I think perhaps just starting the subprocess without a window would be sufficient. Use the overload of Process.Start that takes a ProcessStartInfo and set ProcessStartInfo.CreateNoWindow to true.
子进程只是一个控制台应用程序吗?我没有编写 Windows 服务,但我认为也许只启动没有窗口的子进程就足够了。使用采用 ProcessStartInfo 并将 ProcessStartInfo.CreateNoWindow 设置为 true 的 Process.Start 的重载。
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.createnowindow.aspx
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.createnowindow.aspx
回答by Robert
I know this is a bit late but in this circumstance i would use the task scheduler and not bother with the windows service. The task scheduler has a comprehensive set of scheduling options and can run console applications without issue.
我知道这有点晚了,但在这种情况下,我会使用任务调度程序,而不用打扰 Windows 服务。任务调度程序有一套全面的调度选项,可以毫无问题地运行控制台应用程序。