0

I am working in signal r mvc .. and Connection through hub not created my hub code is

public class ChatHub : Hub
{
    private readonly static ConnectionMapping<string> _connections =
        new ConnectionMapping<string>();

    public void SendChatMessage(string who)
    {
        string name = Context.User.Identity.Name;
        Clients.User(who).addChatMessage();
    }

    public override Task OnConnected()
    {
        string name = Context.User.Identity.Name;

        _connections.Add(name, Context.ConnectionId);
         Debug.Write("Shubham Conected");
        return base.OnConnected();
    }

    public override Task OnDisconnected(bool stopCalled)
    {
        string name = Context.User.Identity.Name;

        _connections.Remove(name, Context.ConnectionId);
        Debug.Write("Shubham now Disconnected");
        return base.OnDisconnected(stopCalled);
    }

    public override Task OnReconnected()
    {
        string name = Context.User.Identity.Name;

        if (!_connections.GetConnections(name).Contains(Context.ConnectionId))
        {
            _connections.Add(name, Context.ConnectionId);
        }
        Debug.Write("Shubham Reconnected");
        return base.OnReconnected();
    }


    public void SendChatMessage(string who, string message)
    {
        string name = Context.User.Identity.Name;
        Debug.Write("Shubham inside the send");
        Clients.Group(who).addChatMessage(name + ": " + message);
    }


} 

When I execute it in debug mode in visual studio 2013, it throws exception in

Context.User.Identity.Name; null reference exception.

I don't know how to solve this problem. When I use this statement in my controller than this code returns the userId.

John Saunders
  • 157,405
  • 24
  • 229
  • 388
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Apr 10 '15 at 22:44
  • this is specific to signalr and should not be duplicate. – Anuj Pandey Mar 25 '16 at 14:10

1 Answers1

1

Please can you tell whats the authentication mode you are using for this, I mean are you setting the context programmatically or are you using windows authentication through IIS.

First check that that in your MVC Application your authentication step should be prior to the configuration of SignalR

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ConfigureAuth(app);
        app.MapSignalR();
    }
}
saurabh vats
  • 319
  • 3
  • 11