'Microsoft.AspNet.SignalR.Hubs.ClientProxy'不包含'broadcastMessage'的定义

时间:2014-04-22 17:15:00

标签: c# asp.net signalr

按照on the SignalR website的说明进行简单的聊天中心,我在技术上能够启动并运行,并且我没有对说明进行任何修改。

但是,第一次启动应用程序时,似乎Clients.All.broadcastMessage没有正确动态绑定。

当客户端执行javascript chat.server.send()时,服务器Send()方法执行三次(请参阅更新)。前两个导致错误:

  

其他信息:' Microsoft.AspNet.SignalR.Hubs.ClientProxy'   不包含' broadcastMessage'

的定义

,第三个按预期执行:客户端从服务器接收广播。

后续执行客户端代码只会导致在服务器上执行一次Send()方法,并且broadcastMessage按预期执行。

我未能正确初始化某些内容吗?

更新:服务器代码仅在第一次执行一次,而不是三次。继续按钮上的干扰需要两次,然后才能正常工作。并且由于代码是被请求的,因此SignalR Demo

上的代码是逐字的

但是...

<!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>

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);
        }
    }
}

1 个答案:

答案 0 :(得分:4)

这似乎是例外情况过度破坏的情况。显然,Microsoft.CSharp.RuntimeBinder.RuntimeBinderException将在后台中优雅地抛出,而不是实际上被抛到顶部。

解决方案是告诉调试器不要破坏该异常。