Search code examples
c#signalrsignalr-hub

Method cannot be resolved in SignalR 1.0 hub in C#


I'm struggling at connecting my C# code to an existing SignalR hub. This hub is created in SignalR 1.0
I have a pretty straight forward situation. The hub is defined as follows:

using System; 
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR.Hubs;    

namespace POC.SignalR.WebHost.Hubs 
{
        [HubName("SignalRHub")]
        public class SignalRHub : Hub
        {
            /// <summary>
            /// Joins the group.
            /// </summary>
            /// <param name="groupname">The groupname.</param>
            public void JoinGroup(string groupname)
            {
                Groups.Add(Context.ConnectionId, groupname);
                Clients.OthersInGroup(groupname).newMemberMessage(Context.ConnectionId);
                Clients.Caller.JoinedGroup(groupname);  
            }

When I connect to the hub using Javascript it all works like a charm.

// Check if url exists and give it a default value if that's the case.
if (!url) { url = 'http://www.someurl.com/signalr'; }

conn = $.connection.SignalRHub;
var currentGroupName = '';
if (typeof groupName == "string") currentGroupName = groupName;

$.connection.hub.url = url;
$.connection.hub.logging = true;

// Start the connection
$.connection.hub.start().done(function () {
    verbosemsg(conn.connection.state);
    conn.server.connectionName();// init to get my connectioID   
    verbosemsg('Connection made now joining group:' + currentGroupName);
    if (currentGroupName != '') conn.server.joinGroup(currentGroupName);
});     

But when I connect to the hub in C# using the following code, I keep running into a "'JoinGroup' method could not be resolved." error. The hubConnection is in "connected" state and seems correct.

HubConnection hubConnection = new HubConnection("http://www.someurl.com/signalr", false);
IHubProxy hubProxy = hubConnection.CreateHubProxy("SignalRHub");
hubConnection.Start().Wait();
hubProxy.Invoke("JoinGroup", hubConnection.ConnectionId, "SignalRChatRoom").Wait();

As far as I can see I've implemented my code similar as found on this example: http://www.asp.net/signalr/overview/older-versions/signalr-1x-hubs-api-guide-net-client#establishconnection There must be something I've overlooked but I cannot figure it out. It would be great if someone can point me in the right direction.

Thx.


Solution

  • this link can help trouble shoot a number of SignalR issues. http://www.asp.net/signalr/overview/testing-and-debugging/troubleshooting

    “Exception: method could not be resolved” when client calls method on server

    This error can result from using data types that cannot be discovered in a JSON payload, such as Array. The workaround is to use a data type that is discoverable by JSON, such as IList. For more information, see .NET Client unable to call hub methods with array parameters.

    Also In your hub your JoinGroup is

    public void JoinGroup(string groupname)
    

    but in your client you're adding another argument, there is no JoinGroup that takes 2 arguments.

    hubProxy.Invoke("JoinGroup", hubConnection.ConnectionId, "SignalRChatRoom").Wait();
    
    // Summary:
    //     Executes a method on the server side hub asynchronously.
    //
    // Parameters:
    //   method:
    //     The name of the method.
    //
    //   args:
    //     The arguments
    //
    // Type parameters:
    //   T:
    //     The type of result returned from the hub
    //
    // Returns:
    //     A task that represents when invocation returned.
    Task<T> Invoke<T>(string method, params object[] args);