Object reference is required for the non-static field, method, or "notificationhub" property

2

Follow the code below:

void OnChange(object sender, SqlNotificationEventArgs e)
{
    //from here we will send notification message to client
    NotificationHub.SendNotification("João"); // <----- Aqui

    ....
}

Hub:

public void SendNotification(string who)
{
    string name = HttpContext.Current.User.Identity.Name;

    foreach (var connectionId in _connections.GetConnections(who))
    {
        Clients.Client(connectionId).addNotification(name + ": " + "message");
    }
}

Line error NotificationHub.SendNotification(Who); :

  

An object reference is required for the field, method, or   property "NotificationHub.SendNotification (string)" not static

Any solution?

    
asked by anonymous 02.06.2017 / 00:35

1 answer

3

Yes. Exactly what it says in the error message, it is necessary to create an instance of NotificationHub to call the SendNotification() method. This is because the method is non-static , that is, it can only be accessed by an instance of the class.

Depending on the case, you will need a specific instance, but I can not talk about it because there are no details in the question.

NotificationHub notHub = new NotificationHub();
notHub.SendNotification("João");
    
02.06.2017 / 00:42