locked
Can't connect to SignalR Hub RRS feed

  • Question

  • User-1045299099 posted

    Hi guys

    I'm struggeling to establish a connection from a uwp application over signalr  to my asp.net core application running a signalR server

    My Startup class on the SignalR server:

        public class Startup
        {
    
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddMvc();
                services.AddSignalR();
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                    app.UseBrowserLink();
    
                   
                }
                else
                {
                    app.UseExceptionHandler("/Home/Error");
                }
    
                app.UseStaticFiles();
    
                app.UseSignalR(routes =>
                {
                    routes.MapHub<MonitoringHub>("MonitoringHub");
                });
    
                app.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=DeviceMonitoring}/{action=Index}/{id?}");
                });
            }
        }

    SignalR Client project running on the same machine:

    using Microsoft.AspNet.SignalR.Client;
    using Microsoft.AspNet.SignalR.Client.Transports;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace PiFace_II
    {
        class MonitoringHubHandler
        {
            private IHubProxy monitoringHubProxy;
    
            // private HubConnection monitoringHubConnection;
            private IDevice device;
          
            public MonitoringHubHandler(IDevice device)
            {
                this.device = device;
                establishHubConnection();
    
            }
    
    
            private async Task establishHubConnection()
            {
                 var monitoringHubConnection = new HubConnection("http://localhost:59162/MonitoringHub");
                monitoringHubProxy = monitoringHubConnection.CreateHubProxy("MonitoringHub");
    monitoringHubProxy.On<PinState>("SetDeviceInput", PinState => SetDeviceInput(PinState));
    monitoringHubConnection.StateChanged += MonitoringHubConnection_StateChanged; await monitoringHubConnection.Start(); } . . . .

    The connection state from the "monitoringHubConnection" changes from disconnected to connecting that's all... I don't recive any response error back etc.

    Hope someone can help...

    Thanks!

    Wednesday, November 29, 2017 4:00 PM

All replies

  • User61956409 posted

    Hi Thomas,

    The connection state from the "monitoringHubConnection" changes from disconnected to connecting that's all

    Your connection does not establish, so your hub method would not be invoked and client does not receive notification message. Please make sure you are passing correct url when you initializes a new instance of HubConnection. And please start the connection (await monitoringHubConnection.Start(); ) before you call hub method.

    I do a test with the following sample to call hub method outside hub class, which works for me, please refer to it.

    Establish connection and invoke hub method:

    var hub = new HubConnection("http://localhost:33668/");
    
    var proxy = hub.CreateHubProxy("ChatHub");
    hub.StateChanged += Hub_StateChanged;
    hub.Start().Wait();
    
    //invoke hub method
    proxy.Invoke("sayhello");
    

    Trace connection state:

    private void Hub_StateChanged(StateChange obj)
    {
        string state = obj.NewState.ToString();
    }
    

    Debug result:

    With Regards,

    Fei Han


    Thursday, November 30, 2017 5:31 AM