.NET Web 服务代理类可以安全地用作单例吗?
我正在使用 Visual Studio 生成的代理类与 SOAP Web 服务进行通信。生成的类派生自 System.Web.Services.Protocols.SoapHttpClientProtocol。我发现实例化该类的成本很高,因此我正在考虑修改我的工厂方法以返回该类的 Singleton 实例。根据 文档< /a>,该类对于多线程是安全的。
有人有重用这些类的实例的经验吗?这样做有什么负面影响吗(即连接保持打开状态等)?
.NET框架版本:2.0
I am using a Visual Studio generated proxy class for communicating with a SOAP web service. The generated class derives from System.Web.Services.Protocols.SoapHttpClientProtocol. I find the class is expensive to instantiate, so I am considering modifying my factory method to return a Singleton instance of the class. According to the documentation, the class is safe for multithreading.
Does anyone have experience with reusing instances of these classes? Are there any negatives to doing so (i.e. connections left open, etc.)?
.NET Framework Version: 2.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了开销之外,我可以想象实例化单例代理的多个原因,我同意开销不是很大。最大的原因是维持服务调用之间的会话。想象一个具有多个页面的应用程序,所有页面都使用相同的 Web 服务方法。 Web 服务调用的会话通常是通过在代理上设置 cookie 参数来执行的:
如果为每个页面操作创建新的代理,则会话将在页面之间丢失。创建一个所有页面都使用的单例代理可以解决这个问题。
I can imagine multiple reasons for instantiating a singleton of the proxy apart from the overhead, which I agree is not very much. The biggest reason would be to maintain session between service calls. Imagine an application that has multiple pages all of which use the same web service methods. Session for web service calls is usually carried out by setting a cookie parameter on the proxy:
If you are creating a new proxy for each page operation, the session will be lost from page to page. Creating a singleton proxy used by all the pages would solve this problem.
在单例模式中使用这些类应该没有理由会导致问题,但是我很难理解您要解决的问题。当您说实例化这些类的成本很高时,您将它们与什么进行比较?与向 Web 服务发出 http 请求的成本相比,实例化代理类的成本可以忽略不计。您能否解释一下情况/发布一些代码来证明您的性能瓶颈是由实例化代理类引起的。
There should be no reason that using these classes in a singleton pattern should cause a problem, however i am stuggling to unserstand what problem you are trying to solve. When you say these classes are expensive to instantiate what are you comparing them with? The cost of instantiating a proxy class should be neglible in comparison to the cost of making an http request to a web service. Could you explain the circumstances/post some code that demonstrates your performance bottle neck is being caused by instantiating the proxy classes.