Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

AspNetCore.SignalR does not generate proxy scripts in WebApi #2577

Closed
littleowlnest opened this issue Jul 2, 2018 · 5 comments
Closed

AspNetCore.SignalR does not generate proxy scripts in WebApi #2577

littleowlnest opened this issue Jul 2, 2018 · 5 comments
Assignees
Labels
status: Investigate Investigation item status: Waiting More info is needed or we're waiting for a response

Comments

@littleowlnest
Copy link

I had this working before migrating to AspNetCore. I have Microsoft.AspNetCore.SignalR (1.0.1) via Nuget and currently running Microsoft.AspNetCore (2.0.5)

Going to /signalr produces a 400 error with:
Connection ID required (this seems correct because I just went to the URL from PostMan)

But going to /signalr/hubs produces a 404 error not found.

Is this an issue or I'm doing something wrong? I've added logging to the WebApi, but get nothing in the log files.

I have a WebApi project running in IIS. The relevant parts of the configuration set up

        public void ConfigureServices(IServiceCollection services)
        {

            services.AddMvcCore(options => {
                options.Filters.Add(typeof(ValidateModelStateAttribute));
                })
                .AddAuthorization()
                .AddJsonFormatters();


            services.AddCors(options =>
            {
                // this defines a CORS policy called "default"
                options.AddPolicy("default", policy =>
                {
                    policy.AllowAnyOrigin()
                        .AllowAnyHeader()
                        .AllowAnyMethod();
                });
            });

            services.AddSignalR();
        }

        public void Configure(IApplicationBuilder app)
        {
            
            app.UseAuthentication();
            app.UseCors("default");
            app.UseSignalR(routes =>
            {
                routes.MapHub<MessageHub>("/signalr");
            });

            app.UseMvc();
        }

The Hub class is as follows:


 public class MessageHub : Hub
    {
        public async Task SendMessage(string user, string message)
        {
            await Clients.All.SendAsync("ReceiveMessage", user, message);
        }

        public Task SendMessageToCaller(string message)
        {
            return Clients.Caller.SendAsync("ReceiveMessage", message);
        }

        public Task SendMessageToGroups(string message)
        {
            List<string> groups = new List<string>() { "SignalR Users" };
            return Clients.Groups(groups).SendAsync("ReceiveMessage", message);
        }

        public override async Task OnConnectedAsync()
        {
            await Groups.AddToGroupAsync(Context.ConnectionId, "SignalR Users");
            await base.OnConnectedAsync();
        }

        public override async Task OnDisconnectedAsync(Exception exception)
        {
            await Groups.RemoveFromGroupAsync(Context.ConnectionId, "SignalR Users");
            await base.OnDisconnectedAsync(exception);
        }
    }
@mikaelm12 mikaelm12 assigned mikaelm12 and analogrelay and unassigned mikaelm12 Jul 3, 2018
@analogrelay
Copy link
Contributor

But going to /signalr/hubs produces a 404 error not found.

What client packages are you using? /signalr/hubs is the URL that ASP.NET SignalR used. ASP.NET Core SignalR should not be trying to use this URL.

currently running Microsoft.AspNetCore (2.0.5)

Note that ASP.NET Core SignalR is not officially supported on ASP.NET Core 2.0. SignalR requires ASP.NET Core 2.1

@analogrelay analogrelay added status: Investigate Investigation item status: Waiting More info is needed or we're waiting for a response labels Jul 3, 2018
@analogrelay
Copy link
Contributor

Also ASP.NET Core SignalR does not provide automatic proxy generation. You need to use the HubConnection API and call methods using .invoke or .send, providing the method name as a string. See the documentation on the JavaScript client for more info.

@littleowlnest
Copy link
Author

@anurse Thanks for the feedback.

What client packages are you using? /signalr/hubs is the URL that ASP.NET SignalR used. ASP.NET Core SignalR should not be trying to use this URL.

When you say that it shouldn't be using that URL, you mean it uses a different one or it is not required as per your follow up comment. (using .invoke and .send)

I'll upgrade the packages and revert on the results.

@analogrelay
Copy link
Contributor

When you say that it shouldn't be using that URL, you mean it uses a different one or it is not required as per your follow up comment.

The URL you should put into HubConnectionBuilder.withUrl is the URL you gave for MapHub. So you should be using:

let connection = new HubConnectionBuilder()
    .withUrl("/signalr")
    .build();

The /signalr/hubs URL is what ASP.NET SignalR (the version for ASP.NET 4.x) uses to server the auto-generated proxies, but as I said that's not supported in ASP.NET Core SignalR.

@littleowlnest
Copy link
Author

You can mark this as closed... I'm now hitting the same issue as this one
#2095

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: Investigate Investigation item status: Waiting More info is needed or we're waiting for a response
Projects
None yet
Development

No branches or pull requests

3 participants