title: C# Windows Service Events
body: <p>So I am just messing around here nothing production just proof of concept with my first ever Windows Service.</p>
  
  <p>I am trying to essentially create a windows service that sits as the listener for a signalr connection. In essence, I will have a windows application and a windows service. The win service will handle connecting to the signalr hub and on signalr calls fire an event. The windows application will listen for these events and perform actions based on them. </p>
  
  <p>Currently I cannot get this to work. I have never worked with events, or windows services before. In my windows application my events never hit their break points, as well I log an error of null reference exception from the </p>
  
  <blockquote>
    <p>ConnectToHub()</p>
  </blockquote>
  
  <p>Alright if I comment out the OnConnected() method call I log a successful connection to the hub. I have never worked with events before so is my mistake with the events?</p>
  
  <p>I debated that this approach was a bit overkill. However, for me it was a proof of concept that I could find a use for a long running windows service, and adding some events into the mix.</p>
  
  <p>Code for service:</p>
  
  <pre><code>public delegate void MessageRecievedEventHanlder(object sender, MessageRecievedArgs e);
  public delegate void ConnectedToHubEventHandler(object sender, ConnectedArgs e);
  public partial class SignalRService : ServiceBase
  {
  
      IHubProxy _hub;
      HubConnection connection;
      string url = @"http://localhost:8080/";
      private Message LastMessage;
      public static event MessageRecievedEventHanlder NewMessage;
  
      protected virtual void OnNewMessage(MessageRecievedArgs e)
      {
          NewMessage(null, e);
      }
  
      public static event ConnectedToHubEventHandler Connected;
  
      protected virtual void OnConnected(ConnectedArgs e) {
          System.IO.File.WriteAllText(@"C:\Users\Bailey Miller\Desktop\FTP\Logg.txt", "Hit OnConnected " + e.Success +" " + Connected != null ? "Isn't null" : "Null event");
          Connected(null, e);
  
      }
  
      public SignalRService()
      {
          InitializeComponent();
      }
  
      protected override void OnStart(string[] args)
      {
          ConnectToHub().Wait();
  
      }
  
      private async Task ConnectToHub()
      {
          try
          {
              //Connecting
  
              if (connection == null)
              {
                  connection = new HubConnection(url);
              }
              if (_hub == null)
              {
                  _hub = connection.CreateHubProxy("ChatHub");
              }
  
              await connection.Start();
  
              //Connected
  
              //Configure all the incoming options
              _hub.On&lt;Message&gt;("RecieveMessage", IncomingMessage);
              System.IO.File.WriteAllText(@"C:\Users\Bailey Miller\Desktop\FTP\Succes.txt", "Connected");
              OnConnected(new ConnectedArgs(true));
  
          }
          catch (Exception ex)
          {
              //Failed
              //OnConnected(new ConnectedArgs(false));
              System.IO.File.WriteAllText(@"C:\Users\Bailey Miller\Desktop\FTP\Fail.txt", "Failed to connect " + ex.Message.ToString());
          }
  
      }
  
      private void IncomingMessage(Message state)
      {
          DateTime? lmt;
          //Determine if has lastmessagetime
          if (LastMessage == null) {
              lmt = null;
          }
          else {
              lmt = LastMessage.RecievedAt;
          }
  
          LastMessage = state;
  
          //New Message
          //OnNewMessage(new MessageRecievedArgs(state, lmt));
      }
  
      protected override void OnStop()
      {
      }
  }
  
  public class MessageRecievedArgs : EventArgs
  {
      public Message NewMessage { get; }
      public DateTime? LastMessageTime { get; }
  
      public MessageRecievedArgs(Message msg, DateTime? lmt) {
          this.NewMessage = msg;
          this.LastMessageTime = lmt;
      }
  }
  
  public class ConnectedArgs : EventArgs {
      public bool Success { get; }
  
      public ConnectedArgs(bool suc) {
          this.Success = suc;
      }
  }
  </code></pre>
  
  <p>My windows application as of now:</p>
  
  <pre><code>public MainWindow()
      {
          InitializeComponent();
          SignalRService.SignalRService.NewMessage += SignalRService_NewMessage;
          SignalRService.SignalRService.Connected += SignalRService_Connected;
      }
  
      private void SignalRService_Connected(object sender, SignalRService.ConnectedArgs e)
      {
          throw new NotImplementedException();
      }
  
      private void SignalRService_NewMessage(object sender, SignalRService.MessageRecievedArgs e)
      {
          throw new NotImplementedException();
      }
  </code></pre>