'Microsoft.AspNet.SignalR.Hubs.ClientProxy' does not contain a definition for 'broadcastMessage'

StackOverflow https://stackoverflow.com/questions/23226104

  •  07-07-2023
  •  | 
  •  

Question

following the instructions on the SignalR website for a simple chat hub, I was technically able to get it up and running, and I have not made any modifications to the instructions.

However, the first time the application starts, it appears Clients.All.broadcastMessage does not dynamically bind properly.

when the client executes the javascript chat.server.send() the server Send() method executes three times (see update). The first two result in the error:

Additional information: 'Microsoft.AspNet.SignalR.Hubs.ClientProxy' does not contain a definition for 'broadcastMessage'

and the third executes as expected: the clients receive the broadcast from the server.

Subsequent executions of the client code only cause one execution of the Send() method on the server, and the broadcastMessage executes as expected.

Am I failing to initialize something properly?

UPDATE: The server code is only executing once the first time, not three times. Jamming on the continue button takes two times, then it works. And since code was requested, it's the code verbatim on the SignalR Demo

But...

<!DOCTYPE html>
<html>
<head>
    <title>SignalR Simple Chat</title>
    <style type="text/css">
        .container {
            background-color: #99CCFF;
            border: thick solid #808080;
            padding: 20px;
            margin: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <input type="text" id="message" />
        <input type="button" id="sendmessage" value="Send" />
        <input type="hidden" id="displayname" />
        <ul id="discussion">
        </ul>
    </div>
    <!--Script references. -->
    <!--Reference the jQuery library. -->
    <script src="Scripts/jquery-1.6.4.min.js" ></script>
    <!--Reference the SignalR library. -->
    <script src="Scripts/jquery.signalR-2.0.3.min.js"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script src="signalr/hubs"></script>
    <!--Add script to update the page and send messages.--> 
    <script type="text/javascript">
        $(function () {
            // Declare a proxy to reference the hub. 
            var chat = $.connection.chatHub;
            // Create a function that the hub can call to broadcast messages.
            chat.client.broadcastMessage = function (name, message) {
                // Html encode display name and message. 
                var encodedName = $('<div />').text(name).html();
                var encodedMsg = $('<div />').text(message).html();
                // Add the message to the page. 
                $('#discussion').append('<li><strong>' + encodedName
                    + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
            };
            // Get the user name and store it to prepend to messages.
            $('#displayname').val(prompt('Enter your name:', ''));
            // Set initial focus to message input box.  
            $('#message').focus();
            // Start the connection.
            $.connection.hub.start().done(function () {
                $('#sendmessage').click(function () {
                    // Call the Send method on the hub. 
                    chat.server.send($('#displayname').val(), $('#message').val());
                    // Clear text box and reset focus for next comment. 
                    $('#message').val('').focus();
                });
            });
        });
    </script>
</body>
</html>

and

using System;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace SignalRChat
{
    public class ChatHub : Hub
    {
        public void Send(string name, string message)
        {
            // Call the broadcastMessage method to update clients.
            Clients.All.broadcastMessage(name, message);
        }
    }
}
Was it helpful?

Solution

This appears to be a case of over-breaking on exceptions. The Microsoft.CSharp.RuntimeBinder.RuntimeBinderException apparently will gracefully be thrown in the background in this case and not actually be thrown up to the top.

The solution is to tell the debugger to not break on that exception.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top