简体   繁体   English

如何在另一个 AppDomain 中处理静态连接

[英]How to dispose static connections in another AppDomain

I got an windows service application which dynamically loads some other modules in other new appdomain.我有一个 Windows 服务应用程序,它动态加载其他新应用程序域中的一些其他模块。 The problem is they are all using the same static database connections.问题是它们都使用相同的静态数据库连接。 I can dispose the static connections in the main AppDomain when I shutdown the service.当我关闭服务时,我可以处理主 AppDomain 中的静态连接。 But how can I immediately dispose the other static connections in other AppDomain.但是我怎样才能立即处理其他 AppDomain 中的其他静态连接。 The problem is since the other connections still exist, the service application still runs in the Task Manager even I fully stop it.问题是由于其他连接仍然存在,即使我完全停止它,服务应用程序仍然在任务管理器中运行。

Thanks谢谢

The problem is they are all using the same static database connections.问题是它们都使用相同的静态数据库连接。

Yes, that is definitely a problem.是的,这绝对是个问题。 Don't do that.不要那样做。 Connections are pooled by .NET and are not expensive to create, so the proper pattern is to create them when you need them, use them, and dispose of them when you're done.连接由 .NET 池化并且创建起来并不昂贵,因此正确的模式是在您需要它们时创建它们,使用它们并在完成后处理它们。 An effective way of doing that is with using statements.一种有效的方法是using语句。

in general, whatever creates a disposable object is responsible for disposing of it.一般来说,任何创建一次性对象的东西都负责处理它。 since your disposable objects are static, there is no way to know what is responsible for disposing of it.由于您的一次性对象是静态的,因此无法知道是什么负责处理它。 So you need to have logic to see if it's already been disposed, if it's open, if it's null, etc. It's much cleaner to just keep all of the creation and disposal logic in one place.因此,您需要有逻辑来查看它是否已被处理,是否已打开,是否为空等。将所有创建和处理逻辑放在一个地方会更简洁。

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

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