WCF:不关闭 ChannelFactory 与不断重新创建 ChannelFactory 并关闭它之间的比较

发布于 2024-09-06 05:26:43 字数 548 浏览 6 评论 0原文

我有一个 WCF 服务,我在代码中使用它并生成为 ChannelFactory 类。我知道使用 WCF 的正确方法是创建 ChannelFactory(我们称之为 AwesomeClient),完成工作,然后对其调用 Close()。 这是我的代码片段:

    public static void DoSomething()
    {
        var client = new AwesomeClient();
        client.DoSomethingAwesome();
        client.Close();
    }

但是,我预计 DoSomething 会被非常频繁地调用(比如说每分钟 10 次?),所以我得到的建议是将 ChannelFactory 实例化为静态实例,并始终重用同一实例,并且永远不要必须关闭它(因为这比总是重新创建 ChannelFactory 然后关闭它“便宜”)。

我来这里寻求第二个意见,谁能告诉我为什么不调用 Close 并重用静态实例是一个好主意?或者我应该坚持重新创建 ChannelFactory 并为每次调用关闭它?

I have a WCF service that I consume in my code and generated as a ChannelFactory class. I know that the proper way to consume the WCF is to create the ChannelFactory (let's call this AwesomeClient), do the work, then call Close() on it.
Here's my snippet:

    public static void DoSomething()
    {
        var client = new AwesomeClient();
        client.DoSomethingAwesome();
        client.Close();
    }

However, I am expecting that DoSomething will be called quite frequently (say 10 times a minute?), so the advice I have gotten is to instantiate the ChannelFactory as a static instance, and always reuse the same instance, and never having to Close it (because this is 'cheaper' than always recreating the ChannelFactory and then closing it).

I'm here for a second opinion, can anyone tell me why not calling Close and reusing the static instance is a good idea? Or should I just stick with recreating the ChannelFactory and Close()-ing it for every call?

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

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

发布评论

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

评论(1

抹茶夏天i‖ 2024-09-13 05:26:43

一分钟10次并不常见。每秒 10 次,我肯定至少会考虑重用该通道。

您的情况有很多未知因素,无法做出正确的决定。有多少客户端将连接到该服务?这是什么类型的连接(是否有可能会在几分之一秒内断开),是否有负载平衡器?代理人?

顺便说一句,如果您确实决定每次打开和关闭通道,则无需重新创建 ChannelFactory。保持静态,每次打开和关闭一个新通道即可。大多数时候创建工厂会消耗更多资源。

10 times a minute is not that often. 10 times a second i would definitely at least consider reusing the channel.

There is a lot of unknowns in your case to make a good decision. How many clients are going to be connecting to the service? What kind of connection is this (is there a chance it will go down for a fraction of a second), is there a load balancer? Proxy?

and by the way, if you do decide to open and close the channel every time, there is no need to recreate the ChannelFactory. Keep that static, just open and close a new channel every time. Most of the time creating a factory consumes more resources.

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