0

我创建了一个 wcf 库,并有 1 个主机和 2 个客户端连接到它。

在我的 WCF 中,我有代码存储从客户端 A 发送到 WCF 的消息:

 private string CustReady; //whether the customer is ready

我有一个为此设置的方法,如下

  public string sendReady(string s_Ready)
    {   
        CustReady = s_Ready;
    }

    //gets state of customer (POS)
    public string getReady()
    {
        return CustReady;
    }

客户端 A 使用 sendReady 方法并传入一个字符串,然后将其存储在 CustReady 中。在客户端 B 中,当单击按钮并检索保存在 CustReady 变量中的字符串时,会触发 getReady 方法。当我在我的 WCF 中围绕这两种方法设置断点时,客户端 A 正确存储了信息,但是当我按下客户端 B 上的按钮时,它返回 null。我想知道是否有人知道为什么?

谢谢

4

1 回答 1

3

两个客户端使用主机的两个实例,因此它们不共享变量。您必须将变量设为静态或将服务器上的ServiceBehivorAttribute的InstanceContext设置为InstanceContextMode.Single(如果未将 ConcurrencyMode 设置为 Multiple,则一次只能处理一个与服务的连接。)

  [ServiceBehavior(
    ConcurrencyMode=ConcurrencyMode.Multiple,
    InstanceContextMode=InstanceContextMode.Single
  )]
  public class BehaviorService : IBehaviorService
  {
     //Snip
  }
于 2013-04-20T20:29:47.090 回答