[英]SignalR send message from client to server error
I'm trying to send a message from a c# console application to my ASP.NET Core Hub.我正在尝试将消息从 c# 控制台应用程序发送到我的 ASP.NET 核心集线器。
The server is running but when I try to send a message to the hub I get following expection:服务器正在运行,但是当我尝试向集线器发送消息时,我得到以下预期:
Exception thrown: 'System.InvalidOperationException' in Microsoft.AspNet.SignalR.Client.dll An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.AspNet.SignalR.Client.dll The connection has not been established. Exception thrown: 'System.InvalidOperationException' in Microsoft.AspNet.SignalR.Client.dll An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.AspNet.SignalR.Client.dll The connection has not been established.
I don't know why the client isn't connected.我不知道为什么客户端没有连接。 I also tried to start the connection like this:我也尝试像这样开始连接:
connection.Start().Wait();连接.Start().Wait();
But then the print "Client connected" will never be excecuted!但是打印“客户端已连接”将永远不会被执行!
Server服务器
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc();
// register middleware for SignalR
app.UseSignalR(routes =>
{
// the url most start with lower letter
routes.MapHub<TestHub>("/hub");
});
}
HubClass集线器类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
namespace CoreSignalRServer.Hubs
{
public class TestHub : Hub
{
public void HelloMethod(String line)
{
System.Diagnostics.Debug.Write(line);
}
}
}
Client客户
static void Main(string[] args)
{
Console.WriteLine("Client started!");
HubConnection connection = new HubConnection("http://localhost:44384/hub");
IHubProxy _hub = connection.CreateHubProxy("TestHub");
connection.Start();
Console.WriteLine("Client connected!");
string line = null;
while ((line = System.Console.ReadLine()) != null)
{
_hub.Invoke("HelloMethod", line).Wait();
}
}
I'm trying to send a message from a c# console application to my ASP.NET Core Hub.我正在尝试将消息从 c# 控制台应用程序发送到我的 ASP.NET 核心集线器。
As far as I know, ASP.NET Core SignalR isn't compatible with clients or servers for ASP.NET SignalR.据我所知,ASP.NET Core SignalR 与 ASP.NET Z358E3F3085C2A6E4EE53 的客户端或服务器不兼容If you'd like to connect to Hub server (built on ASP.NET Core SignalR) and send message from a console application (.NET Framework), you can install following client library to your application:如果您想连接到 Hub 服务器(基于 ASP.NET Core SignalR)并从控制台应用程序(.NET Framework)发送消息,您可以将以下客户端库安装到您的应用程序:
Microsoft.AspNetCore.SignalR.Client Microsoft.AspNetCore.SignalR.Client
and achieve it with following code.并使用以下代码实现它。
Console.WriteLine("Client started!");
HubConnection connection = new HubConnectionBuilder()
.WithUrl("https://localhost:44384/hub/TestHub")
.Build();
connection.StartAsync().Wait();
Console.WriteLine("Client connected!");
string line = null;
while ((line = System.Console.ReadLine()) != null)
{
connection.InvokeAsync("HelloMethod", line);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.