【发布时间】: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>: ' + 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>
现在的问题是,将输入发送到服务器端函数不起作用。当我单击发送时,服务器端功能“发送”不会被调用。我也没有在控制台日志中看到任何错误。编译器不会达到我在服务器端函数上设置的断点。 我该如何解决这个问题?
【问题讨论】: