2 Replies - 408 Views - Last Post: 06 June 2019 - 07:21 AM Rate Topic: -----

#1 aidenkael   User is offline

  • D.I.C Regular
  • member icon

Reputation: 66
  • View blog
  • Posts: 313
  • Joined: 22-October 13

SignalR throwing "ConnectionID Required". Can't figure ou

Posted 05 June 2019 - 02:00 PM

Startup from Server:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json.Serialization;
using System.Linq;

namespace BlazorAndSignalR.Server
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().AddNewtonsoftJson();
            services.AddResponseCompression(opts =>
            {
                opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
                    new[] { "application/octet-stream" });
            });

            services.AddCors(options => options.AddPolicy("CorsPolicy", builder =>
            {
                builder.AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials()
                    .WithOrigins("http://localhost:51742");
            }));
            services.AddSignalR();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseResponseCompression();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBlazorDebugging();
            }

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapDefaultControllerRoute();
            });

            app.UseCors("CorsPolicy");
            app.UseSignalR(routes => routes.MapHub<ChatHub>("/chathub"));
            app.UseHttpsRedirection();
            app.UseBlazor<Client.Startup>();
        }
    }
}




Hub:
using Microsoft.AspNetCore.SignalR;
using System.Threading.Tasks;

namespace BlazorAndSignalR.Server
{
    public class ChatHub : Hub
    {
        public override Task OnConnectedAsync()
        {
            if(string.IsNullOrEmpty(Context.ConnectionId))
            {
                throw new System.Exception(nameof(Context.ConnectionId)); //This code is never hit
            }
            Clients.All.SendAsync("broadcastMessage", "system", $"{Context.ConnectionId} joined the conversation");
            return base.OnConnectedAsync();
        }
        public void Send(string name, string message)
        {
            Clients.All.SendAsync("broadcastMessage", name, message);
        }

        public override Task OnDisconnectedAsync(System.Exception exception)
        {
            Clients.All.SendAsync("broadcastMessage", "system", $"{Context.ConnectionId} left the conversation");
            return base.OnDisconnectedAsync(exception);
        }
    }
}



Page:
@page "/chathub"

<div class="container">
    <input type="text" id="message" class="form-control" bind-on-input="@Message" />
    <input type="button" id="sendMessage" value="Send" class="btn btn-primary" onclick="@SendMessage" />
    <ul id="discussion">
        @foreach (var message in messages)
        {
            <li>@message</li>
        }
    </ul>
</div>
@functions {
    [Inject] IJSRuntime jsRuntime { get; set; }

    HubConnection connection;
    string Message = "";
    IList<string> messages = new List<string>();

    protected override async Task OnInitAsync()
    {
        connection = new HubConnectionBuilder(jsRuntime).WithUrl("https://localhost:51742/chathub").Build();
        connection.On<string, string>("broadcastMessage", this.OnBroadcastMessage);
        await connection.StartAsync();
    }

    Task OnBroadcastMessage(string name, string message)
    {
        messages.Add(name + " : " + message);
        StateHasChanged();
        return Task.CompletedTask;
    }

    async Task SendMessage()
    {
        await connection.InvokeAsync("Send", "Blazor Client", Message);
        Message = "";
    }
}


This post has been edited by aidenkael: 05 June 2019 - 02:05 PM


Is This A Good Question/Topic? 0
  • +

Replies To: SignalR throwing "ConnectionID Required". Can't figure ou

#2 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6968
  • View blog
  • Posts: 23,684
  • Joined: 05-May 12

Re: SignalR throwing "ConnectionID Required". Can't figure ou

Posted 05 June 2019 - 03:39 PM

Where is it throwing that exception?

Looking at this blogpost, it looks like you need to use different ports (they were using 5050 and 5001)...
Was This Post Helpful? 0
  • +
  • -

#3 aidenkael   User is offline

  • D.I.C Regular
  • member icon

Reputation: 66
  • View blog
  • Posts: 313
  • Joined: 22-October 13

Re: SignalR throwing "ConnectionID Required". Can't figure ou

Posted 06 June 2019 - 07:21 AM

View PostSkydiver, on 05 June 2019 - 05:39 PM, said:

Where is it throwing that exception?


When navigating to the page "http://localhost:51742/chathub".

View PostSkydiver, on 05 June 2019 - 05:39 PM, said:

Looking at this blogpost, it looks like you need to use different ports (they were using 5050 and 5001)...


This is just the port the app runs on, which in this case is 51742.

VS Version is 2019 16.2.0 Preview 1, and .NET Core version is 3.0.100-preview5-011568
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1