为什么我的WCF全局变量重置?

时间:2013-05-03 11:46:57

标签: c# wcf networking host

我有一个WCF服务,它有两个全局定义的类对象,名为dieselTank,类型为DieselTankunleadedTank类型为UnleadedTank。每个对象都有maximumLevelcurentLevelwarning值。这些值是从WCF服务的构造函数中的SQLite database读入的。

问题是什么时候

我需要通过Manager Systemservice reference获取这些值,并将它们全部重置为零。如果我在WCF服务中跨过每行代码,则会正确读取值,但是当我Manger System需要它们时,它们会休息为零,我不知道为什么。

 [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)] 
 public class Service1 : IService1{

    DieselTank dieselTank = new DieselTank();
    UnleadedTank unleadedTank = new UnleadedTank(); 

     public Service1(){       
        try{
            myConnection = new SQLiteConnection("Data source=test2.db; Version=3;");
            myConnection.Open();   
            SQLiteCommand tanksCommand = new SQLiteCommand("SELECT * FROM tanks", myConnection);
            SQLiteDataReader tanksReader = tanksCommand.ExecuteReader();
            while (tanksReader.Read()) {
                string tankType = tanksReader.GetString(0);
                double currentLevel = tanksReader.GetDouble(1);

                if (tankType == "Diesel Tank") {
                    dieselTank.MaximumLevel = 10000;
                    dieselTank.CurrentLevel = currentLevel;
                    tankLevel = currentLevel;
                    dieselTank.WarningLevel = 1000;                     
                }
                if (tankType == "Unleaded Tank") {
                    unleadedTank.MaximumLevel = 10000;
                    unleadedTank.CurrentLevel = currentLevel;
                    unleadedTank.WarningLevel = 1000;                    
                }
            } 
      } 

Manager的系统调用这些方法从Service:

获取值
 public DieselTank getDieselTankLevels() {
        return dieselTank;
 }

  public UnleadedTank getUnleadedTankLevels() {
        return unleadedTank;
  }  

谢谢。

以下是IService1中定义的柴油罐:

[DataContract]
public class DieselTank {
[DataMember]
public double MaximumLevel { get; set; }
[DataMember]
public double WarningLevel { get; set; }
[DataMember]
public double CurrentLevel { get; set; }
[DataMember]
public bool OrderFuel { get; set; }
}

如果它在服务中生成,则声明它和一个新实例:

    public static DieselTank dieselTank = new DieselTank();

0 个答案:

没有答案