C# 我需要在 ASP.NET 中处理 Web 服务引用吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/429478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do I need to dispose a web service reference in ASP.NET?
提问by BeaverProj
Does the garbage collector clean up web service references or do I need to call dispose on the service reference after I'm finished calling whatever method I call?
垃圾收集器是否会清理 Web 服务引用,或者我是否需要在调用完我调用的任何方法后对服务引用调用 dispose?
采纳答案by Dan Herbert
Instead of worrying about disposing your web services, you could keep only a single instance of each web service, using a singleton pattern. Web services are stateless, so they can safely be shared between connections and threads on a web server.
不用担心处理您的 Web 服务,您可以使用单例模式只保留每个 Web 服务的一个实例。Web 服务是无状态的,因此它们可以在 Web 服务器上的连接和线程之间安全地共享。
Here is an example of a Web Service class you can use to hold references to your web service instances. This singleton is lazy and thread-safe. It is advised that if you make your singletons lazy, they are also kept thread safe by following the same logic. To learn more about how to do this, read the C# In Depth article on Implementing Singletons.
下面是一个 Web 服务类的示例,您可以使用它来保存对 Web 服务实例的引用。这个单例是惰性且线程安全的。建议如果你让你的单例懒惰,它们也可以通过遵循相同的逻辑来保持线程安全。要了解有关如何执行此操作的更多信息,请阅读有关实现单例的 C# 深入文章。
Also keep in mind that you may run into issues with WCF web services. I'd recommend reading up on WCF's instance management techniques article, specifically the singleton section, for more details.
另请记住,您可能会遇到 WCF Web 服务的问题。我建议阅读WCF 的实例管理技术文章,特别是单例部分,以了解更多详细信息。
public static class WS
{
private static object sync = new object();
private static MyWebService _MyWebServiceInstance;
public static MyWebService MyWebServiceInstance
{
get
{
if (_MyWebServiceInstance == null)
{
lock (sync)
{
if (_MyWebServiceInstance == null)
{
_MyWebServiceInstance= new MyWebService();
}
}
}
return _MyWebServiceInstance;
}
}
}
And then when you need to access your web service, you can do this:
然后当你需要访问你的网络服务时,你可以这样做:
WS.MyWebServiceInstance.MyMethod(...)
or
或者
var ws = WS.MyWebServiceInstance;
ws.MyMethod(...)
I've successfully used this pattern on several projects and it has worked well, but as tvanfosson mentions in the comments below, an even better strategy would be to use a DI framework to manage your web service instances.
我已经在几个项目中成功地使用了这种模式并且效果很好,但是正如 tvanfosson 在下面的评论中提到的那样,更好的策略是使用 DI 框架来管理您的 Web 服务实例。
回答by BeaverProj
I think the DataService inherits Dispose from Component.
我认为 DataService 从 Component 继承了 Dispose。
回答by Jobo
what are you trying to accomplish here?
你想在这里完成什么?
If your worried about performance, then I would worry more about the responsiveness of the server hosting the webservice and the network speed, as they can dramatically affect the length of time you have to wait for the webservice call to complete (unless its asynchronous).
如果您担心性能,那么我会更担心托管 Web 服务的服务器的响应能力和网络速度,因为它们会极大地影响您必须等待 Web 服务调用完成的时间长度(除非它是异步的)。
The examples on MSDN dont call 'Dispose' and its quite obvious that the garbage collector will do its job, so unless your working on a realtime system that needs to process over 100,000 records in memory every second, then maybe you dont need to come up with a way to dispose resources or manage memory.
MSDN 上的示例不调用“Dispose”,很明显垃圾收集器会完成它的工作,因此除非您在需要每秒处理内存中超过 100,000 条记录的实时系统上工作,否则您可能不需要提出以一种方式来处理资源或管理内存。
回答by orj
Objects that implement IDispose should be disposed of manually to assist the garbage collector.
应手动处理实现 IDispose 的对象以协助垃圾收集器。
If you object is short lived use a using
block. For objects that can be retained ensure that they object that retains them disposes of them when it is also disposed.
如果您的对象是短暂的,请使用using
块。对于可以保留的对象,请确保保留它们的对象在也被处置时处置它们。