title: EventHandler does not get triggered in .NET Core worker service app
body: <p>I am trying to create OPCDA C# client with worker service template in .NET Core 3.1.</p>
  <p>My goal is to read PLC tag value when DataChange event is triggered by OPC DA. The sample code I have created does the job properly in Console App (.NET Core 3.1) but when I utilize same code in Worker Service App (.NET Core 3.1), I found that it connects to OPC DA Server properly but the DataChange event does not call the method. I tried adding this code in</p>
  <pre><code>public override Task StartAsync(CancellationToken cancellationToken) {}
  </code></pre>
  <p>as well as</p>
  <pre><code>protected override async Task ExecuteAsync(CancellationToken stoppingToken) {}
  </code></pre>
  <p>but it does not trigger the event and there is no error.</p>
  <p>This is my sample code:</p>
  <pre><code>using Microsoft.Extensions.Configuration;
  using Microsoft.Extensions.Hosting;
  using Microsoft.Extensions.Logging;
  using Opc.Da;
  using System;
  using System.Collections.Generic;
  using System.Threading;
  using System.Threading.Tasks;
  
  namespace SampleWorkerService
  {
      public class Worker : BackgroundService
      {
          private readonly ILogger&lt;Worker&gt; _logger;
          private readonly IConfiguration _config;
  
          public Opc.URL url;
          private Opc.Da.Server server;
          private OpcCom.Factory fact = new OpcCom.Factory();
          private Opc.Da.Subscription groupRead;
          private Opc.Da.SubscriptionState groupState;
          private List&lt;Item&gt; itemsList = new List&lt;Item&gt;();
          string opcTopicName = &quot;TopicName&quot;;
  
          public Worker(ILogger&lt;Worker&gt; logger, IConfiguration configuration)
          {
              _logger = logger;
              _config = configuration;
          }
  
          public override Task StartAsync(CancellationToken cancellationToken)
          {
                  url = new Opc.URL(&quot;opcda://localhost/OPC Server&quot;);
                  server = new Opc.Da.Server(fact, null);
                  server.Connect(url, new Opc.ConnectData(new System.Net.NetworkCredential())); 
                  //On Debug Server.IsConnected = true meaning server is connected properly
                  groupState = new Opc.Da.SubscriptionState();
                  groupState.Name = &quot;Group&quot;;
                  groupState.UpdateRate = 1000;
                  groupState.Active = true;
                  groupRead = (Opc.Da.Subscription)server.CreateSubscription(groupState);
                  groupRead.DataChanged += null;
  
                  Item item = new Item();
  
                  for (int i = 0; i &lt; listTagNamesFrmconfigFile.Count; i++)
                  {
                      item = new Item();
                      item.ItemName = $&quot;{opcTopicName}{listTagNamesFrmconfigFile[i].Name}&quot;;
                      itemsList.Add(item);
                  }
  
                  groupRead.AddItems(itemsList.ToArray()); // On Debug, Item list is added properly
  
            //groupRead.DataChanged += 
                         new Opc.Da.DataChangedEventHandler(GroupRead_DataChanged); //tried this
            /await Task.Run(() =&gt; groupRead.DataChanged += GroupRead_DataChanged); //tried this
            groupRead.DataChanged += GroupRead_DataChanged; 
  // This line does not trigger method GroupRead_DataChanged which reads Item Values, THIS IS THE ISSUE (Same Code Works good in Console App)
  
              return base.StartAsync(cancellationToken);
          }
  
  protected void GroupRead_DataChanged(object subscriptionHandle, object requestHandle, ItemValueResult[] values)
          {
              foreach (ItemValueResult itemValue in values)
              {
                  objPLCTagInfo readPLCTagInfo = new objPLCTagInfo();
                  readPLCTagInfo.Value = itemValue.Value;
              }
          }
  
  protected override async Task ExecuteAsync(CancellationToken stoppingToken)
          {
               // I tried below commented code, the opcMethods.OPCRead has same code above but does 
                  not trigger &quot;groupRead.DataChanged&quot;
              
               //OPCMethods opcMethods = new OPCMethods();
  
              //while (!stoppingToken.IsCancellationRequested)
              //{
              //    //_logger.LogInformation(&quot;Worker running at: {time}&quot;, DateTimeOffset.Now);
  
              //    opcMethods.OPCRead(_config);
  
              //    await Task.Delay(1000, stoppingToken).ConfigureAwait(false);
              //}
  
          // Following code also doesnot trigger &quot;groupRead.DataChanged&quot;
            
              groupRead.DataChanged += (object subscriptionHandle, object requestHandle, 
                                             ItemValueResult[] values) =&gt;
              {
                  Console.WriteLine(&quot;Event Triggered&quot;);
              };
  
              // await Task.CompletedTask;
  
              await Task.Delay(1000, stoppingToken).ConfigureAwait(false);
          }
  
  public override Task StopAsync(CancellationToken cancellationToken)
          {
              return StopAsync(cancellationToken);
          }
  
      }
  }
  </code></pre>
  <p>Above Code works good with Console App.
  The event groupRead.DataChange does not trigger the method &quot;GroupRead_DataChanged&quot; to read the data, there is no error/exception occurs:</p>
  <p>This Code Line and following method reads item values, which I am trying to make it work</p>
  <pre><code>groupRead.DataChanged += GroupRead_DataChanged;
  
  protected void GroupRead_DataChanged(object subscriptionHandle, object requestHandle, ItemValueResult[] values)
      {
          //logic here to read item values
          foreach (ItemValueResult itemValue in values)
              { ... }
      }
  </code></pre>
  <p>I hope I have explained the issue I am trying to address.</p>
  <p>I appreciate your time and solution provided in advance !</p>