Windows服务的静态属性

时间:2012-07-04 20:19:13

标签: c# windows-services static-classes

在我的解决方案资源管理器中,我已经为Windows服务BridgeWS拖了一个项目,而另一个项目Vytru.Platform.Bridge.Configuration有一个静态类SharedData.cs

我的问题:我想使用此静态属性SharedData.DeviceList来获取BridgeWS服务中的Device对象列表,但它总是等于null?

这是我的解决方案

enter image description here

我的静态类

中的一些代码
public static  class SharedData
    {
        public const string CONFIGURATION_XML_PATH = @"\Configuration.xml";
        private static List<Device> _deviceList;

        public static void Initialize()
        {
            APPLICATION_LOCAL_PATH = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            if (!string.IsNullOrEmpty(APPLICATION_LOCAL_PATH)) CONFIGURATION_FULL_PATH = APPLICATION_LOCAL_PATH + CONFIGURATION_XML_PATH;

            _deviceList = new List<Device>();
            _tempDeviceList = new List<Device>();
            _deviceAgent = new DeviceManager();
            _deviceAgent.Initialize();
        }           


        public static bool AddToTempDeviceList(Device device)
        {
            if (_tempDeviceList != null)
            {
                if (!_deviceAgent.IsExist(device, true))
                {
                    _tempDeviceList.Add(device);
                    return true;
                }
            }
            return false;
        }

        public static bool UpdateFile()
        {
            _deviceList = _tempDeviceList;
            return _deviceAgent.Save();
        }

          public static List<Device> DeviceList
        {
            get { return _deviceList; }
            set { _deviceList = value; }
        }

        public static List<Device> TempDeviceList
        {
            get { return _tempDeviceList; }
            set { _tempDeviceList = value; }
        }

        public static DeviceManager DeviceAgent
        {
            get { return _deviceAgent; }
            set { _deviceAgent = value; }
        }
    }

感谢并抱歉我的英语不好。

1 个答案:

答案 0 :(得分:1)

您可以先调用Initialize方法。如果不这样做,由于这部分代码

,属性将为null
_deviceList = new List<Device>();
            _tempDeviceList = new List<Device>();
            _deviceAgent = new DeviceManager();
            _deviceAgent.Initialize();

在BridgeWS中

SharedData.Initialize();
SharedData.TempDeviceList; // not null
SharedData.DeviceList; // not null