开发者

WCF Service worker thread communicate with ServiceHost thread

开发者 https://www.devze.com 2022-12-23 01:07 出处:网络
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 ea

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.


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; }
}


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消