Hi,
I attempted to set up a background service that listens to MetadataLiveSource, but it does not function when I use it within the EnvironmentType.Service. However, it works when I use EnvironmentType.Administration. Is there a workaround for this issue, or am I overlooking something?
Not sure if this is a timing issue since EnvironmentType.Service is within the Event Server. Is there a way to determine when the event server has fully finished loading?
public override void Init()
{
_stop = false;
_thread = new Thread(new ThreadStart(Run));
_thread.Name = "MetadataPlugin Background Thread";
_thread.Start();
}
void _messageCommunication_ConnectionStateChangedEvent(object sender, EventArgs e)
{
if (_messageCommunication.IsConnected && !commReady)
{
try
{
if (_alarmClient == null)
{
_alarmClient = _alarmClientManager.GetAlarmClient(EnvironmentManager.Instance.MasterSite.ServerId);
}
if (_metadataLiveSources.Count == 0)
{
IList<Item> metadataItems = _metadataService.GetMetadataItems();
if (metadataItems != null && metadataItems.Count > 0)
{
foreach (Item item in metadataItems)
{
MetadataLiveSource metadataLiveSource = new MetadataLiveSource(item);
try
{
metadataLiveSource.LiveModeStart = true;
metadataLiveSource.Init();
metadataLiveSource.LiveContentEvent += OnLiveContentEvent;
_metadataLiveSources.Add(metadataLiveSource);
_liveSourceToItem[metadataLiveSource] = item;
}
catch (Exception ex)
{
}
}
}
}
}
catch (Exception ex)
{
EnvironmentManager.Instance.Log(false, "MetadataPlugin", $"Error initializing AlarmClient or MetadataLiveSources: {ex}", null);
}
finally
{
commReady = true;
EnvironmentManager.Instance.Log(false, "MetadataPlugin", "Communication is ready", null);
}
}
}
private void Run()
{
EnvironmentManager.Instance.Log(false, "MetadataPlugin background thread", "Now starting...", null);
MessageCommunicationManager.Start(EnvironmentManager.Instance.MasterSite.ServerId);
_messageCommunication = MessageCommunicationManager.Get(EnvironmentManager.Instance.MasterSite.ServerId);
_messageCommunication.ConnectionStateChangedEvent += new
EventHandler(_messageCommunication_ConnectionStateChangedEvent);
while (!_stop)
{
Thread.Sleep(2000);
}
EnvironmentManager.Instance.Log(false, "MetadataPlugin background thread", "Now stopping...", null);
_thread = null;
}
/// <summary>
/// Define in what Environments the current background task should be started.
/// </summary>
public override List<EnvironmentType> TargetEnvironments
{
get { return new List<EnvironmentType>() { EnvironmentType.Administration }; }
}


