title: Subscribing to an eventhandler doesn't work in abstract class using dependency injection in C# .NET 6
body: <p>I have an abstract class <code>WebsocketHandler</code> which is inherited by <code>WsUserRequestHandler</code> which implements an interface which in turn is injected as a singleton service to be used in other classes.</p>
  <p>WebsocketHandler (simplified):</p>
  <pre><code>public abstract class WebSocketHandler
  {
      private event EventHandler&lt;OnRequestReceivedEventArgs&gt; OnRequestReceived;
      private ClientWebSocket _client { get; set; } = new ClientWebSocket();
  
      protected async Task&lt;bool&gt; Connect(WsConnectionInfo connectionInfo)
      {
          this._client.ConnectAsync(...)
          
          while (true)
          {
              await this.Receive(this._client);
              if (this._cts.IsCancellationRequested)
                  break;
          }
      }
  
      private async Task Receive(ClientWebSocket client)
      {
          while (true)
          {
              var result = await client.ReceiveAsync(...);
              if (result.MessageType == WebSocketMessageType.Text)
              {
                  var jsonInput = Encoding.UTF8.GetString(...);
                  var request = JsonConvert.DeserializeObject&lt;IWebSocketRequest&gt;(jsonInput, ...);
  
                  if (request is null)
                      continue;
  
                  // Always true
                  Console.WriteLine(&quot;received request event is null: &quot; + (this.OnRequestReceived == null));
  
                  // Always null, so not being invoked
                  this.OnRequestReceived?.Invoke(this, new OnRequestReceivedEventArgs(request)); 
              }
          }
      }
  
      // this method runs as expected when called and ReceivedRequest gets 
      // successfully attached to event handler (at least in the scope of the method)
      protected async Task&lt;TRequest&gt; RequestReceived&lt;TRequest&gt;(CancellationToken token) 
          where TRequest : IWebSocketRequest
      {
          var request = default(dynamic);
  
          void ReceivedRequest(object sender, OnRequestReceivedEventArgs args)
          {
              if (args.Request.GetType() == typeof(TRequest))
                  request = args.Request;
          }
  
          this.OnRequestReceived += ReceivedRequest;
          Console.WriteLine(this.OnRequestReceived == null); // returns false
  
          while (request == null)
              await Task.Delay(25, token);
  
          this.OnRequestReceived -= ReceivedRequest;
  
          return request;
      }
  </code></pre>
  <p>The class <code>WsUserRequestHandler</code> is injected into another class which then calls the base method <code>RequestReceived</code> and awaits it. If I then, after making sure that the method is called and awaited, send a message to the websocket, the <code>Receive</code> method is called and <code>OnRequestReceived</code> turns out <code>null</code>. I have also made sure that the cancellation token that gets passed on to <code>RequestReceived</code> isn't cancelled, and also that it doesn't unsubscribe from the event.</p>