WCF中的get方法返回Null

本文关键字:返回 Null 方法 get 中的 WCF | 更新日期: 2023-09-27 18:10:05

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

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

 private string CustReady; //whether the customer is ready

,我有一个get an set方法,如下所示

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

客户端A利用sendReady方法并传入一个字符串,该字符串随后存储在CustReady中。在客户端B中,当单击按钮时触发getReady方法,并检索CustReady变量中保存的字符串。当我把一个断点在我的WCF周围这2种方法客户端a存储正确的信息,但是当我按下客户端B上的按钮,它返回null。有人知道为什么吗?

谢谢

WCF中的get方法返回Null

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

  [ServiceBehavior(
    ConcurrencyMode=ConcurrencyMode.Multiple,
    InstanceContextMode=InstanceContextMode.Single
  )]
  public class BehaviorService : IBehaviorService
  {
     //Snip
  }