简体   繁体   中英

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.

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.

I don't know why the client isn't connected. I also tried to start the connection like this:

connection.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.

As far as I know, ASP.NET Core SignalR isn't compatible with clients or servers for ASP.NET SignalR.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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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