.Net Windows 服务向用户报告错误的最佳方式

发布于 2024-08-15 07:32:00 字数 144 浏览 2 评论 0原文

我正在编写一个将进行大量网络通信的 Windows 服务,并且我需要一种方法来通知用户(如果已登录)任何异常/错误。我知道允许服务与桌面交互是不好的做法,那么是否有其他方法来通知用户发生了错误?我知道事件日志记录,但我还希望能够显示系统托盘中的通知气泡或引起用户注意的内容。

I am writing a windows service that will be doing a lot of network communication, and I need a way to notify a user (if logged on) of any exceptions/errors. I know it is bad practice to allow a service to interact with the desktop, so is there an alternative way to notify a user that an error has occurred? I am aware of event logging, but I would also like to be able to show a notification bubble from the system tray or something that catches the user's attention.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

淡墨 2024-08-22 07:32:00

请考虑使用事件日志和单独的应用程序仅用于监控。您还可以考虑使用电子邮件或 MSMQ。

Please consider using Event Log and a separate application just for monitoring. You can also consider using emails or MSMQ.

遮云壑 2024-08-22 07:32:00

通常,您将拥有一个服务可以通知的辅助应用程序。例如,您可以有一个系统托盘图标(呃),当重要的事情发生时,它会弹出气球(双呃)。然后,应用程序本身也可以呈现一个用于服务设置的界面。

Typically, you'll have a secondary app that the service can notify. For example, you can have a system tray icon (ugh) that pops up balloons (double ugh) when something important occurs. The app itself can then present an interface for settings for your service as well.

吻安 2024-08-22 07:32:00

使用另一个应用程序与此应用程序交互并通知用户错误怎么样?这样你就可以给用户“选择”。可能会关闭警报、电子邮件等

What about using another app to interact with this application and notify the user about the errors? This way you can have "option" to users. May be switch off alerts, email etc

那伤。 2024-08-22 07:32:00

您可以利用一些现有的通知应用程序,例如 Growl: http://www.growlforwindows.com/gfw/< /a>.这将允许用户“选择加入”。

You could make use of some existing notification app like Growl: http://www.growlforwindows.com/gfw/. This would allow the user to "opt in".

司马昭之心 2024-08-22 07:32:00

您必须提供“与桌面交互”选项的替代方案。 Vista 和 Win7 不再支持它。授予受限用户访问在高权限帐户上运行的 UI 的安全风险太大。谷歌“会话 0 隔离”以查找有关此内容的详细信息。

您需要使用 .NET 支持的流程互操作机制之一来设置服务和 UI 应用程序之间的交互。对于简单的消息传递,请使用套接字或命名管道。管道名称需要具有“Global\”前缀才能对所有会话可见。对于更复杂的交互,您可以使用远程处理或 WCF。

请务必处理用户决定不运行或终止桌面应用程序的可能性。您应该使用 EventLog 类记录任何错误。

You will have to provide an alternative to the "interact with desktop" option. It is no longer supported in Vista and Win7. Giving a restricted user access to an UI that runs on a highly privileged account was considered too great a security risk. Google "session 0 isolation" to find details on this.

You'll need to use one of the process interop mechanisms supported by .NET to setup interaction between your service and a UI app. For simple message passing use either a socket or named pipe. The pipe name needs to have the "Global\" prefix to be visible to all sessions. For more intricate interactions you can use Remoting or WCF.

Be sure to deal with the possibility that the user decides to not run or kill off the desktop app. You should log any error with the EventLog class.

徒留西风 2024-08-22 07:32:00

为了解决这个问题,我创建了一个Windows窗体应用程序,用户可以随意启动和停止。当然,Windows服务是24/7在后台运行的。当应用程序启动时,它使用 Windows Communication Foundation (WCF) 查询服务的当前状态 - 是否处于活动状态、连接了多少客户端、已收集了多少字节等。然后将此信息呈现给用户在应用程序内部。

我尝试模仿任务管理器的行为,因此我还在应用程序的主窗体中添加了一个 System.Windows.Forms.NotifyIcon,该窗体在系统托盘中显示一个图标。当应用程序打开时,会显示系统托盘图标。用户可以将应用程序最小化到任务栏,也可以完全隐藏应用程序。仅当用户关闭应用程序时,托盘图标才会消失。托盘图标还支持双击重新打开应用程序和弹出菜单以快速访问功能。

无论如何,我想说的是,WCF 是让服务和前端应用程序进行通信的最直接的方式。人们会争论与 WCF 相关的所谓“陡峭”学习曲线,但我没有经历过这种情况。我发现WCF是开发进程间通信的一种非常有效的方式。您可以观看此视频,让您深入了解 WCF 编程范例。

To solve this problem, I've created a Windows Forms application that the user can start and stop at will. Of course, the Windows service is running in the background 24/7. When the application is started, it uses Windows Communication Foundation (WCF) to query the service for it's current status - is it active, how many clients are connected, how many bytes have been collected, etc. This information is then presented to the user inside the application.

I've tried to mimic the behavior of Task Manager, so I've also added a System.Windows.Forms.NotifyIcon to the main form of the application that displays an icon in the system tray. When the application is open, the system tray icon is shown. The user can minimize the application to the task bar or can hide the application altogether. The tray icon only goes away when the user closes the application. The tray icon also supports double-clicks to re-open the application and a pop-up menu for quick access to functionality.

In any case, the point I want to make is that WCF is the most straightforward way to have your service and front-end application communicate. People will argue about the supposed 'steep' learning curve associated with WCF, but I did not experience that. I find that WCF is a very efficient way to develop inter-process communication. You can watch this video to give you some insight to the WCF programming paradigm.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文