locked
Microsoft.AspNet.SignalR.Hubs.HubBase.Context.get returned null. RRS feed

  • Question

  • User-1167268708 posted

    Hello everyone,

    I implemented Signal R to my project.

    Startup.cs

    using Microsoft.Owin;
    using Owin;
    
    [assembly: OwinStartup(typeof(SignalR.Startup))]
    namespace SignalR
    {
        public class Startup
        {
            public void Configuration(IAppBuilder app)
            {
                // Any connection or hub wire up and configuration should go here
                app.MapSignalR();
            }
        }
    }

    ChatHub.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Microsoft.AspNet.SignalR;
    using SignalR.Models;
    
    namespace SignalR
    {
        public class ChatHub : Hub
        {
            static List<ChatUserDetail> ConnectedUsers = new List<ChatUserDetail>();
            static List<ChatMessageDetail> CurrentMessage = new List<ChatMessageDetail>();
    
    
            public void Connect(string UserName, int UserID)
            {            
                var id = Context.ConnectionId;
    
                if (ConnectedUsers.Count(x => x.ConnectionId == id) == 0)
                {
                    ConnectedUsers.Add(new ChatUserDetail { ConnectionId = id, UserName = UserName + "-" + UserID, UserID = UserID });
                }
    
                ChatUserDetail CurrentUser = ConnectedUsers.Where(u => u.ConnectionId == id).FirstOrDefault();
                Clients.Caller.onConnected(CurrentUser.UserID.ToString(), CurrentUser.UserName, ConnectedUsers, CurrentMessage, CurrentUser.UserID);
                Clients.AllExcept(CurrentUser.ConnectionId).onNewUserConnected(CurrentUser.UserID.ToString(), CurrentUser.UserName, CurrentUser.UserID);
            }
         }
    }

    SignalRController

    <div>

     public ActionResult Chatting()
            {
                ChatHub chatHub = new ChatHub();
                chatHub.Connect("chatUser", 1);
    
                return View();
            }

    I got error  this line 

    var id = Context.ConnectionId;

    Monday, December 10, 2018 1:31 PM

All replies

  • User283571144 posted

    Hi TokenHash,

    According to your description and codes, I found you direclty call the method in your controller.

    This means your server directly broadcast the message to all the client user.

    If you use this way, there are no connectionid genreated.

    If you still want to get the connectionid  when using server  broadcast the message  way.

    I suggest you could try to install the Microsoft.AspNet.SignalR.Client pakcage and use this package to invoke the hub's method.

    Then it will generate the connectionid.

    More details, you could refer to below article:

    https://stackoverflow.com/a/16170953 

    Best Regards,

    Brando

    Tuesday, December 11, 2018 9:57 AM
  • User-1167268708 posted

    Thanks your answer. I solved my problem :)

    Friday, December 14, 2018 6:33 AM
  • User1455766605 posted

    Hi, I am facing same issue, so please confirm how to solve it

    Friday, July 3, 2020 8:21 AM