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

Asked

Viewed 7,066 times

3

Follows code:

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 non-static "Notificationhub.Sendnotification(string)" property

Some solution ?

1 answer

4


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

Depending on the case, a specific instance will be required, but I have no way to talk about it because there are no details in the question.

NotificationHub notHub = new NotificationHub();
notHub.SendNotification("João");
  • @Jubeno, only one detail, in the hub class, problem occurs Referência de objeto não definida para uma instância de um objeto.'&#xA; on the line Clients.Client(connectionId).addNotification(who); what can be ?

  • It is not because Clients.Client(connId) returns null?

  • Save in a variable this and check if there is any value

  • It is returned as null, follows the image: https://postimg.org/image/wkgocwc5d/

  • 1

    So. You need to find the cause of this

  • Great jbueno ! , I asked a new question here: https://answall.com/questions/209573/values-arrivals-nullos-com-sqldep-onchange if you know how to answer, I thank you.

  • 1

    Opa, I’ll read it now. Even if I know how to solve it may not answer yet (13% battery), but I see when you have more "prepared"

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.