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();
}
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
None
0 Points
2 Posts
Microsoft.AspNet.SignalR.Hubs.HubBase.Context.get returned null.
Dec 10, 2018 01:31 PM|TokenHash|LINK
Hello everyone,
I implemented Signal R to my project.
Startup.cs
ChatHub.cs
SignalRController
<div>
I got error this line
var id = Context.ConnectionId;
Star
9831 Points
3120 Posts
Re: Microsoft.AspNet.SignalR.Hubs.HubBase.Context.get returned null.
Dec 11, 2018 09:57 AM|Brando ZWZ|LINK
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
None
0 Points
2 Posts
Re: Microsoft.AspNet.SignalR.Hubs.HubBase.Context.get returned null.
Dec 14, 2018 06:33 AM|TokenHash|LINK
Thanks your answer. I solved my problem :)
None
0 Points
1 Post
Re: Microsoft.AspNet.SignalR.Hubs.HubBase.Context.get returned null.
Jul 03, 2020 08:21 AM|Parveen Gupta|LINK
Hi, I am facing same issue, so please confirm how to solve it