注入创建成本高昂的服务

发布于 2024-11-30 17:25:53 字数 202 浏览 0 评论 0原文

我有一个应用程序,可以在项目完成时发送电子邮件和传真通知。我的实现正在运行,但需要几秒钟才能构建(即连接到我们的服务器)。这最终会使 UI 冻结几秒钟,直到通知服务完全构建并使用为止。我已经通过在最后一刻注入工厂并创建我的服务来尽可能地解决这个问题。

对于注入需要几秒钟构建的外部服务,我有哪些选项?我正在考虑指示我的容器这些服务是单例,每次应用程序启动时只会构建一次服务。

I have an application that sends email and fax notifications when an item is complete. My implementation is working but it takes several seconds to construct (i.e. connecting to our servers). This ends up freezing the UI for several seconds until the notification services have been fully constructed and used. I'm already pushing the problem as far as it will go by injecting factories and creating my services at the last possible minute.

What options do I have for injecting external services that takes several seconds to construct? I'm thinking of instructing my container that these services are singletons, which would only construct the services once per application start-up.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白云不回头 2024-12-07 17:25:53

简单的解决方案

我会给这些服务更长的生命周期(单实例)。

但问题是,如果一段时间没有发生任何事情,TCP 连接通常会断开。这意味着您需要某种保持活动的数据包来保持它们打开。

更强大的解决方案

恕我直言,连接设置不是问题。联系服务应该不会花很长时间。您还没有指定外部服务是什么。我猜测它们是 IIS 中托管的某种 Web 服务。如果是这样,请确保应用程序池不会过于频繁地回收。在 IIS 中启动新应用程序可能需要一些时间。

另一个想法是您是否真的需要等待服务完成?为什么不将操作排队(由线程池或单独的线程处理)并让用户继续?如果需要,只需在调用服务且出现故障时使用消息框即可。

Simple solution

I would give those services a longer lifetime (singleinstance).

The problem though is that TCP connections are usually disconnected if nothing have happened for a while. Which means that you need to have some kind of keep alive packets to keep them open.

More robust solution

imho the connection setup is not the problem. Contacting a service should not take long. You haven't specified what the external services are. I'm guessing that they are some kind of web services hosted in IIS. If so, make sure that the application pools aren't recycled too often. Starting a new application in IIS can take time.

The other thought is if you really need to wait for the service to complete? Why not queue the action (to be handled by the thread pool or in a separate thread) and let the user continue? If required, simply use a messagebox when the service have been called and something failed.

君勿笑 2024-12-07 17:25:53

这些服务应该在应用程序启动时引导,然后使用单例通过 DI 进行配置,然后将其注入到在其构造函数中使用该类的任何类中。

我可以推荐 Unity 或 Spring.Net。我发现 Unity 非常易于用于简单注入,所以请看一下。

These services should be bootstrapped on application startup and then configured via DI using a singleton which is then injected to any classes that use the class in their constructor.

I can recommend Unity or Spring.Net. I've found Unity very easy to use for simple injection, so give that a look.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文