Castle Windsor 和 WCF Singleton 生活方式 - 它会导致错误吗?

Castle Windsor & WCF Singleton lifestyle - could it be causing a bug?

我正在从事一个涉及金融交易的项目。

可以有任意数量的交易同时进入。

问题是,在某些情况下,交易似乎 "mixed up" 以一种不可预测的方式进行。 我不知道如果这是我们 "bad" Lifestyle choices for Castle Windsor 的结果,或者是其他代码导致的。

想在 Castle Windsor 上澄清一下。

日志显示有时特定的请求会与不同的响应混淆。

我知道...也吓到我们了!

主要使用的技术是C#4、Net 4.5、WCF、Castle Windsor

结构如下:

  1. 有传入交易,因此服务应始终正常运行
  2. 有3个主要服务
  3. 服务 A - Singleton Lifestyle - 使用 ChannelFactory 实例化服务 B 的客户端。最多可以生成 3 个线程。笔记 该容器的类型为 Castle.Windsor.IWindsorContainer
container.Register(Component.For().ImplementedBy().LifestyleSingleton());
container.Register(Component.For().ImplementedBy().LifestyleSingleton());
  1. 服务 B - Singleton Lifestyle - 使用服务引用,代理客户端到服务 C
container.Register(Component.For().ImplementedBy().LifestyleSingleton());
container.Register(Component.For().ImplementedBy().LifestyleSingleton());
  1. 服务 C - Singleton Lifestyle - 从外部连接到某些 API
container.Register(Component.For().ImplementedBy().LifestyleSingleton());

+--------------------+ +--------------------+ +------------------------+
| | | | | |
| | | | | |
| [WCF 服务 A] | | [WCF 服务 B] | | [WCF 服务 C] |
|实例化一个+------------>+ 实例化一个+----------------> |连接到外部
|客户端到 B 使用 | |客户端到 C 使用 | | RESTful API |
|频道工厂 | |服务参考 | | (单身生活)|
| (单身生活)| |到 C | | |
| | | (单身生活)| | |
| | | | | |
+--------------------+ +--------------------+ +------------------------+

更新 1

现在正在检查 related SO question,同时会仔细尝试所有建议

您的问题可能是多个线程同时使用了每个服务的单个实例。任何实例级别的状态,无论是在您的代码中还是在您的服务架构中(例如调用上下文),都将由对该服务的所有调用共享和编辑。如评论中所述,Transient 生活方式会有所帮助,但也有专门针对 WCF 的生活方式,例如 PerWcfOperation.