Castle Windsor& WCF Singleton生活方式 - 它可能导致错误吗?

时间:2016-11-20 09:54:15

标签: c# web-services wcf castle-windsor

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

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

问题是,似乎在少数情况下,交易以不可预测的方式“混淆”。我不知道这是否是我们的结果“不好” Lifestyle choices 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
  4. container.Register(Component.For<IServiceA>().ImplementedBy<ServiceA>().LifestyleSingleton());
    container.Register(Component.For<IServiceBFactory>().ImplementedBy<ServiceBFactory>().LifestyleSingleton());
    
    1. 服务B - Singleton Lifestyle - 使用服务引用,服务C的代理客户端
    2. container.Register(Component.For<IServiceB>().ImplementedBy<ServiceB>().LifestyleSingleton());
      container.Register(Component.For<IServiceC>().ImplementedBy<ServiceCClient>().LifestyleSingleton());
      
      1. 服务C - Singleton Lifestyle - 外部连接到某些API
      2. container.Register(Component.For<IServiceC>().ImplementedBy<ServiceC>().LifestyleSingleton());
        
        
        +-----------------------+              +-----------------------+                  +-----------------------+
        |                       |              |                       |                  |                       |
        |                       |              |                       |                  |                       |
        | [WCF Service  A]      |              | [WCF Service  B]      |                  | [WCF Service  C]      |
        | Instantiates a        +------------->+ Instantiates a        +----------------> | Connects to an external
        | client to B using     |              | client to C using     |                  | RESTful API           |
        | ChannelFactory        |              | a serivice referece   |                  | (Singleton Lifestyle) |
        | (Singleton Lifestyle) |              | to C                  |                  |                       |
        |                       |              | (Singleton Lifestyle) |                  |                       |
        |                       |              |                       |                  |                       |
        +-----------------------+              +-----------------------+                  +-----------------------+
        
        

        更新1

        现在通过related SO question,同时会仔细尝试所有建议

1 个答案:

答案 0 :(得分:1)

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