【发布时间】:2020-02-11 18:49:37
【问题描述】:
我正在尝试使用 signalR 从 .net 核心服务器(c#)向我的客户端(Angular)发送通知。触发事件时通知客户端。但我收到一个错误。 错误:错误:由于服务器错误,无法调用“发送”
Appcomponent.ts
ngOnInit() {
this._hubConnection = new signalR.HubConnectionBuilder()
.configureLogging(signalR.LogLevel.Information)
.withUrl("http://localhost:5000/message/Send", this.transportType)
.build();
}
form(){
this._hubConnection.on("Send", (data) => {
const received = `message: ${data}`;
console.log(received);
that.disabled = false;
});
this._hubConnection.start()
.then(() =>
this._hubConnection.invoke('Send', "hey")
.catch(function (err) {
console.error(err.toString());
})
)
.then(() =>
console.log('connected'))
.then(() => this._hubConnection.stop())
.then(() => console.log('Disconnected'))
.catch(e => console.error(e.message));
}
SignalR 集线器
public class DataHub : Hub
{
public async Task Send1(string message1, string Root)
{
await Clients.All.SendAsync("Send1", message1);
}
}
控制器
public class DataController : Controller
{
private IHubContext<DrawingHub> _hubContext;
public DataController(IHubContext<DataHub> hubContext)
{
_hubContext = hubContext;
}
private void OnProvideData(DataAndHeader data)
{
// server side code
_hubContext.Clients.All.SendAsync("Send", data);
}
}
Startup.cs
services.AddSignalR();
app.UseSignalR(routes =>
{
routes.MapHub<DrawingHub>("/message/Send");
});
错误记录
失败:Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher[8] 未能调用集线器方法“发送”。 System.IO.InvalidDataException:调用提供 1 个参数,但目标需要 0。 在 Microsoft.AspNetCore.SignalR.Protocol.JsonHubProtocol.BindArguments(JArray args,IReadOnlyList`1 paramTypes) 在 Microsoft.AspNetCore.SignalR.Protocol.JsonHubProtocol.ParseMessage(Utf8BufferTextReader textReader, IInvocationBinder binder)
我想在事件中执行服务器端代码后调用信号器连接以通知客户端。有人可以指导我。提前致谢。
【问题讨论】:
-
你的 hub 方法被称为
Send1但你正在调用Send -
我想从 OnProvideData 方法调用 Send。
-
这是您在客户端上监听的事件。但在客户端上,您还尝试调用集线器上不存在的“发送”。看我的回答。
标签: angular asp.net-core signalr