gpt4 book ai didi

c# - Windows 服务启动和 Exe

转载 作者:太空狗 更新时间:2023-10-30 00:43:20 24 4
gpt4 key购买 nike

我目前正在开发一个包含 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com