WCF 服务工作线程与 ServiceHost 线程通信

发布于 2024-08-25 22:42:01 字数 241 浏览 5 评论 0原文

我有一个打开 ServiceHost 对象的 Windows NT 服务。服务主机上下文是每个会话的,因此为每个客户端创建一个新的工作线程。我想做的是让每个工作线程调用启动服务主机的线程。 NT 服务需要打开 VPN 连接并从远程网络上的设备轮询信息。该信息存储在 SQL 数据库中供工作线程读取。我只想在有客户端连接时轮询设备,这将减少网络流量。我希望工作线程告诉服务主机线程它们正在请求信息并开始轮询和更新数据库。如果设备始终被轮询并且数据库正在更新,则一切正常。

I have a windows NT Service that opens a ServiceHost object. The service host context is per-session so for each client a new worker thread is created. What I am trying to do is have each worker thread make calls to the thread that started the service host.
The NT Service needs to open a VPN connection and poll information from a device on the remote network. The information is stored in a SQL database for the worker threads to read. I only want to poll the device if there is a client connected, which will reduce network trafic. I would like the worker threads to tell the service host thread that they are requesting information and start the polling and updating the database. Everything is working if the device is alway being polled and the database being updated.

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

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

发布评论

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

评论(2

习ぎ惯性依靠 2024-09-01 22:42:01

为什么不实现单例并在服务创建后初始化此属性。之后您可以随时参考它。

private static MyService m_ServiceInstance;

public static MyService ServiceInstance
{
    get { return m_ServiceInstance; }
}

Why not implement singleton and init this property after service creation. After that you can always refer to it.

private static MyService m_ServiceInstance;

public static MyService ServiceInstance
{
    get { return m_ServiceInstance; }
}
像你 2024-09-01 22:42:01

我建议将打开 VPN 连接和轮询信息的代码转变为自己的单例服务,并使用相同(或不同)的 Windows NT 服务托管它。面向客户端的服务使用 WCF 调用 VPN 服务。 VPN 服务仅在面向客户端的服务“监听”时才会进行轮询。

这有几个优点:

  • WCF 将处理创建服务实例和管理线程的复杂性。 (在单例中,您可能仍然需要实现锁定,但仅此而已。)
  • VPN 轮询服务不再与面向客户端的服务紧密耦合。这为您提供了部署灵活性以及支持未来新用例的能力。

I suggest turning the code that opens a VPN connection and polls for information into its own singleton service and hosting it withing the same (or different) Windows NT Service. The client facing service calls the VPN service using WCF. The VPN service would only poll when client facing services are "listening".

This has a couple advantages:

  • WCF will take care of the complexities of creating service instances and managing threads. (Within the singleton you will likely still have to implement locking, but that's all.)
  • The VPN polling service is no longer tightly coupled to the client facing service. This gives you flexibility in deployment and the ability to support new use cases in the future.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文