简体   繁体   English

Asp.net Web服务以及如何重用WMI或DB连接?

[英]Asp.net web services and how to reuse a WMI or DB connections?

I have simple asp.net web service, for monitoring and managing about 10 computers, with 4 webMethods and all of this methods are quite simple. 我有简单的asp.net Web服务,用于监视和管理约10台计算机,并带有4个webMethods,所有这些方法都非常简单。 In general they look sometning like: (1)make WMI connections to certain machine, (2)do some simple task, (3)return result. 通常,它们看起来像是:(1)建立到特定计算机的WMI连接,(2)执行一些简单的任务,(3)返回结果。

Problem is that WMI connections to remote computers takes about 15s and I offten need to call 2 or 3 methods successively for the same machine. 问题是与远程计算机的WMI连接大约需要15秒钟,因此我不必为同一台计算机连续调用2或3个方法。

From what I know, there is new instance of my service class (public class MonSvr : System.Web.Services.WebService ) created every time webMethod is called. 据我所知,每次调用webMethod时都会创建我的服务类的新实例(公共类MonSvr:System.Web.Services.WebService)。

  • So how can I share WMI or DB connection betwen all instances of my service that I could reuse this connection ? 那么,如何在可以重用此连接的所有服务实例之间共享WMI或DB连接?

  • When there all multiple calls to webMethods of my service, does then each instance of web service runs in separate thread ? 当对我的服务的webMethods进行所有多个调用时,那么每个Web服务实例是否在单独的线程中运行?

You could create a static Dictionary containing the name of the remote machine and the WMI connection. 您可以创建一个静态Dictionary其中包含远程计算机的名称和WMI连接。 This could then be shared between different calls. 然后可以在不同的呼叫之间共享。 You may have issues with multiple threads using the WMI classes at once, so be sure to check all calls are thread safe. 您可能一次使用WMI类遇到多个线程问题,因此请确保检查所有调用都是线程安全的。

Isn't you want to save the cost of object creation? 您不是要节省对象创建成本吗? If it is so then why not use Singleton pattern? 如果是这样,那为什么不使用Singleton模式呢? and for multi-threaded Singleton you can check http://msdn.microsoft.com/en-us/library/ff650316.aspx 对于多线程Singleton,可以检查http://msdn.microsoft.com/zh-cn/library/ff650316.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM