WCF 服务通常是否应该是单例的? [英] Should WCF service typically be singleton or not?

查看:39
本文介绍了WCF 服务通常是否应该是单例的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信 Jimmy Nillson 说过他通常将他的网络服务做成单例.这是首选方法吗,并且使用 WCF?除了让服务方法静态化,还有什么可以做的吗?

I believe Jimmy Nillson said he generally made his webservices singletons. Is this the preferred method, and with WCF? Other than making the service methods static, is there something else to be done?

推荐答案

很好的回应,但我认为原问题有问题.一项技术的典型使用"是一个结构不佳的问题.没有人有典型"场景,您应该在决定实施或方法之前查看特定问题的要求.您的要求应为您的解决方案提供信息.

good responses, but I think there is a problem in the original question. "Typical use" of a technology is a poorly formed question. No one has a "typical" scenario, and you should review the requirements of your particular problem before deciding on an implementation or approach. Your requirements should inform your solution.

例如,Singletons [即Singleton 模式] 只是我们盒子里的另一个工具,和任何工具一样,在某些情况下它可以工作,而其他人则不能.具体来说,如果您需要集中业务逻辑[在独立应用程序中比远程 WCF 服务更适用],或者共享内存或资源,单例可以很好地工作.当然,如果是共享业务逻辑,状态是在调用栈中维护的,多线程是没有意义的.如果在消费者调用之间共享内存,那么多线程就是一个问题.关于WCF,你可以指定多线程行为的两种模式[实际上是三种,但第三种是第一种的特例],

For instance, Singletons [ie the Singleton pattern] is just another tool in our box, and like any tool, there are cases where it works, and others it does not. Specifically, if you need to centralize business logic [more applicable in a standalone application than a remote WCF service], or share memory or a resource, a Singleton works well. Of course, if you are sharing business logic, state is maintained in the call stack, and multi threading is moot. If sharing memory between consumer calls, then multi threading is an issue. As regards WCF, there are two modes [actually three, but the third is a special case of the first] of multi-threading behaviour you can specify,

// we are specifying that this service implementation is single-threaded
// and WCF should permit *at most* one call at a time. Any requests made
// simultaneously/concurrently are queued.
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single)]
public class SingleThreadedNonThreadSafeService : IService { ... }

// we are specifying that this service implementation is multi-threaded
// and [hopefully!] thread-safe. WCF should permit any number of threads,
// or any number of simultaneous concurrent calls.
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class MultiThreadedThreadSafeService : IService { ... }

ConcurrencyMode 的 Xml 注释基本上和上面说的一样.

The Xml comments for ConcurrencyMode basically say the same thing as above.

如果您不需要需要在消费者之间共享业务逻辑或内存,那么不要使用单例,模型"不需要 解决问题.这就像在继姐妹的脚上强行穿玻璃鞋!没有人应该看到这一点.

If you DO NOT need to share business logic or memory between consumers, then DO NOT use a Singleton, the "model" DOES NOT fit the problem. It's like forcing a glass slipper on a step-sister's foot! And no one should ever have to see that.

相反,如果调用之间没有共享状态,则为每个调用\会话托管一个实例.

Conversely, if no state is shared between calls, host an instance per-call\session.

这篇关于WCF 服务通常是否应该是单例的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆