繁体   English   中英

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

[英]SignalR send message from client to server error

我正在尝试将消息从 c# 控制台应用程序发送到我的 ASP.NET 核心集线器。

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

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.

我不知道为什么客户端没有连接。 我也尝试像这样开始连接:

连接.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");
            });

        }

集线器类

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

我正在尝试将消息从 c# 控制台应用程序发送到我的 ASP.NET 核心集线器。

据我所知,ASP.NET Core SignalR 与 ASP.NET Z358E3F3085C2A6E4EE53 的客户端或服务器不兼容如果您想连接到 Hub 服务器(基于 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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM