c# - Windows 服务启动和 Exe

标签 c# wpf windows-services

我目前正在开发一个包含 WCF 服务、Windows 服务和 WPF 应用程序的项目。 Windows 服务与WCF 通信,在某种情况下,必须启动WPF 应用程序供用户接收消息。 (WCF 在远程服务器上,其余在客户端上)。我在发布时遇到了一些障碍。我有将消息写入应用程序日志的服务,这样我就可以沿途进行一些“调试”。 Windows 服务运行以下代码没有问题。

C# 代码,Windows 服务:

WriteLog.WriteString("PostOffice.MessagesWaiting: Inside, starting up.", EventLogEntryType.Warning);
// Call the WPF Application
var messagingProcess = new Process();
var messageProcessStartInfo = new ProcessStartInfo(@"""C:\GoldenEyeMessages.exe""");
messageProcessStartInfo.CreateNoWindow = false;
messageProcessStartInfo.UseShellExecute = false;
messageProcessStartInfo.FileName = @"""C:\GoldenEyeMessages.exe""";
messageProcessStartInfo.WindowStyle = ProcessWindowStyle.Normal;
messageProcessStartInfo.Verb = "runas";

messageProcessStartInfo.RedirectStandardOutput = true;
messagingProcess.StartInfo = messageProcessStartInfo;
messagingProcess.Start();
StreamReader daStreamReaderMan = messagingProcess.StandardOutput;
string newString = daStreamReaderMan.ReadLine();

WriteLog.WriteString("PostOffice.MessagesWaiting: The Eagle has landed.", EventLogEntryType.Warning);

WPF 应用程序不在当前用户的 session 中执行。相反,我得到一个弹出窗口来查看消息。这是它的图片:

enter image description here

一旦选择“查看消息”选项,它当然会将我切换到另一个 session ,然后运行 ​​WPF 应用程序。

我的问题是,我应该如何让 WPF 应用程序在当前用户 session 或“事件” session 中启动?

最佳答案

您收到此消息是因为尝试启动 WPF 可执行文件的用户没有与桌面交互的权限。

Windows 服务通常不在具有该权限的帐户下运行,这样做被认为是一个安全漏洞。

In most cases, it is recommended that you not change the Allow service to interact with desktop setting. If you allow the service to interact with the desktop, any information that the service displays on the desktop will also be displayed on an interactive user's desktop. A malicious user could then take control of the service or attack it from the interactive desktop.

http://technet.microsoft.com/en-us/library/cc782435(v=ws.10).aspx

更安全的替代方法是让 WPF 应用程序始终在系统托盘中运行,并为 Windows 服务安排一种机制,以向 WPF 应用程序发出需要显示消息的信号。一种简单的机制是将文件写入约定的位置并使用 WPF 应用程序中的文件观察器来查找该文件(并在显示后将其删除)。请注意,Windows 服务可能在用户登录之前很久就在运行(在 WPF 应用程序运行之前很久),因此无论您使用何种通知机制,都需要允许消息累积并在登录后立即显示。

关于c# - Windows 服务启动和 Exe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11548758/

相关文章:

c# - 关于获取锁的说明

.net - 如何在 WPF 应用程序中生成 "print preview"的 FlowDocument?

wpf - 单击空白处时如何处理事件?

c# - Windows服务(错误1053)事件日志触发器

c# - 连接尝试失败,因为连接方在一段时间后没有正确响应或连接的主机未能响应

c# - 反射和动态 LINQ 的性能 - 'first time' 与 'subsequent runs'

c# - 我可以模拟 SqlConnection.BeginTransaction c# 吗?

C#:使用反射区分记录中的 init 访问器和 set 访问器

wpf - 文本框与周围的网格一起扩展但不与文本一起扩展

c# - 通过 Windows 服务将 pdf 文件发送到打印机