【问题标题】:SignalR server side function not getting invokedSignalR 服务器端函数没有被调用
【发布时间】:2015-12-14 16:27:05
【问题描述】:

我刚开始学习 SignalR,并且编写了一个测试程序,它接受用户输入并广播它。

我从安装 SignalR 库开始,然后创建了一个 Owin 启动类,如下所示:

    public class Startup1
{
    public void Configuration(IAppBuilder app)
    {
        app.MapSignalR();
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
    }
}

我为服务器端功能添加了一个集线器类,如下所示:

    public class ChatHub : Hub
{
    public void Send(string name, string message)
    {
        // Call the broadcastMessage method to update clients.
        Clients.All.broadcastMessage(name, message);
    }
}

在客户端,代码如下:

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

当我启动项目时,我收到了这个错误:

Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

所以我添加了以下代码行来重定向 web.config 中的依赖关系,从而消除了错误:

  <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-7.5.0.0" newVersion="4.5.0.0" />
  </dependentAssembly>

现在的问题是,将输入发送到服务器端函数不起作用。当我单击发送时,服务器端功能“发送”不会被调用。我也没有在控制台日志中看到任何错误。编译器不会达到我在服务器端函数上设置的断点。 我该如何解决这个问题?

【问题讨论】:

    标签: asp.net signalr


    【解决方案1】:

    通常 Json.net 会自动将正确的绑定重定向添加到您的配置文件中。 我认为这不是 Json.net 的问题——但是,为什么要重定向到 4.5.0.0 而不是 6.0.0.0?确保您将绑定重定向到的版本是您使用 nuget 包管理器安装的版本。 也许这已经解决了问题,也许......

    【讨论】:

    • 安装 Json.net 时绑定不会自动添加。从包管理器中,我安装了 Json 的 7.0.2 版本,但在引用中添加的二进制文件显示它的版本为 4.5.0.0。重定向到任何其他版本都会出错
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多