0

I have a Console Application and i am trying to subscribe to a SignalR hub. Looking at all examples on the internet i see that all use HubConnection in order to do that. The problem is that i think they all are either deprecated since the constructor in their case takes an url and in the documentation it takes IConnectionFactory and a ILoggerFactory.

Server

public class Startup {

        public void ConfigureServices(IServiceCollection services) {
            services.AddCors(o =>
            o.AddPolicy("CorsPolicty", b => b.WithOrigins("http://localhost:4200").AllowAnyHeader().AllowAnyMethod().AllowCredentials());
            services.AddSignalR();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
            app.UseDeveloperExceptionPage();
            app.UseSignalR(r => r.MapHub<MyHub>("/myhub"));
        }
    }


public class MyHub:Hub {
        public async Task SendMessage(string user,string message) {

            await this.Clients.All.SendAsync("ReceiveMessage", user, message);

        }
    }

Client

So the HubConnection does not look like in the MSDN examples , also i have looked at HubConnectionBuilder class in which case in all examples has WithUrl extension whereas in reality it has a IServiceCollection where you can add services.

class ConcreteContext : ConnectionContext {
        public override string ConnectionId { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

        public override IFeatureCollection Features => throw new NotImplementedException();

        public override IDictionary<object, object> Items { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
        public override IDuplexPipe Transport { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
    }
    class Factory : IConnectionFactory {
        public Task<ConnectionContext> ConnectAsync(TransferFormat transferFormat, CancellationToken cancellationToken = default(CancellationToken)) {
            var context = new ConcreteContext();
            return Task.FromResult(context as ConnectionContext);
        }

        public Task DisposeAsync(ConnectionContext connection) {
            throw new NotImplementedException();
        }
    }
    class Program {
        static void Main(string[] args) {
            //try 1
            var builder = new HubConnectionBuilder().Build(); //no withUrl extensions
            //try 2
            var factory = new Factory();
            var hub = new HubConnection(factory,); //needs IConnectionFactory and IHubProtocol and ILoggerFactory

        }

I cannot believe i have to implement all that bloat in order to start a connection.How can i connect to a hub? I might consider going back to raw websockets if i have to write so much just for a single thing.

Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152

1 Answers1

0

This SO question appears to be resolved but has links and references for a connection from client to hub for SignalR Core.

The client code looks much more simplistic than what you show. I would check that out and the links if needed.

Frank M
  • 1,379
  • 9
  • 18