SignalR从客户端向服务器发送消息错误

我正在尝试将消息从c#控制台应用程序发送到我的ASP.NET Core Hub。

服务器正在运行,但是当我尝试向集线器发送消息时,我得到了期望值:

引发的异常:microsoft.AspNet.SignalR.Client.dll中的“ System.InvalidOperationException” microsoft.AspNet.SignalR.Client.dll中发生了类型为'System.InvalidOperationException'的未处理异常 尚未建立连接。

我不知道为什么客户端没有连接。 我也试图像这样启动连接:

connection.Start()。Wait();

但是永远不会执行“客户端连接”打印!

服务器

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

客户

        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();
            }
}
Yushujun 回答:SignalR从客户端向服务器发送消息错误

  

我正在尝试将消息从c#控制台应用程序发送到我的ASP.NET Core Hub。

据我所知,ASP.NET Core SignalR与ASP.NET SignalR的客户端或服务器不兼容。如果您想连接到集线器服务器(基于ASP.NET Core SignalR构建)并从控制台应用程序(.NET Framework)发送消息,则可以将以下客户端库安装到您的应用程序中:

Microsoft.AspNetCore.SignalR.Client

并通过以下代码实现。

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);
}
本文链接:https://www.f2er.com/3124631.html

大家都在问