skip to Main Content

I have an ASP.NET Web MVC and Web API communicating with SignalR. When I ran them on localhost everything worked fine, but now I need to deploy them and as I have a couple of Python grpc services also, I decided to do it with docker-compose. So my Web App is on https://fast_web_app:5001 and Web Api is on https://fast_api:7131, in Web App in js file I connect to hub like:

const hubConnection = new signalR.HubConnectionBuilder()
    .withUrl("https://fast_api:7131/pageUpdateHub")
    .withAutomaticReconnect()
    .configureLogging(signalR.LogLevel.Trace)
    .build();

and I also configured CORS in Web Api like that:

        app.UseHsts();
        app.UseHttpsRedirection();
        
        app.UseCors(builder =>
        {
            builder.WithOrigins("https://fast_web_app:5001")
                .AllowAnyHeader()
                .WithMethods("GET", "POST")
                .AllowCredentials();
        });
        
        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapHub<PageUpdateHub>("/pageUpdateHub");
            ...
        }

but I get an error:

[2022-06-04T00:22:50.715Z] Debug: Sending negotiation request: https://fast_api:7131/pageUpdateHub/negotiate?negotiateVersion=1.
POST https://fast_api:7131/pageUpdateHub/negotiate?negotiateVersion=1 net::ERR_NAME_NOT_RESOLVED

I also tried to connect to hub from Web App like that:

const hubConnection = new signalR.HubConnectionBuilder()
    .withUrl("https://fastml_api:7131/pageUpdateHub", {
        skipNegotiation: true,
        transport: signalR.HttpTransportType.WebSockets
    })
    .withAutomaticReconnect()
    .configureLogging(signalR.LogLevel.Trace)
    .build();

but then I get this error:

WebSocket connection to 'wss://fast_api:7131/pageUpdateHub' failed: 
Utils.ts:193 [2022-06-04T00:35:18.749Z] Error: Failed to start the connection: Error: WebSocket failed to connect. The connection could not be found on the server, either the endpoint may not be a SignalR endpoint, the connection ID is not present on the server, or there is a proxy blocking WebSockets. If you have multiple servers check that sticky sessions are enabled.

It looks like I missing something, any ideas how to make this work?

Besides SignalR Api and App also communicate with HTTP and everything works ok.

Part of Docker-compose:

services:
  fast_web_app:
    image: fast_web_app
    ports:
      - "5001:5001"
      - "5002:5002"
    networks:
      - fast
    environment:
      - ASPNETCORE_URLS=https://*:5001;http://*:5002
      - ASPNETCORE_Kestrel__Certificates__Default__Password=***
      - ASPNETCORE_Kestrel__Certificates__Default__Path=/app/Certificates/certificate.pfx
      - ASPNETCORE_HTTPS_PORT=5001
    volumes:
      - /Users/***/Certificates/:/app/Certificates
      - fast_server_storage:/app/wwwroot

  fast_api:
    image: fast_api
    ports:
      - "7131:7131"
      - "7132:7132"
    networks:
      - fast
    environment:
      - ASPNETCORE_URLS=https://*:7131;http://*:7132
      - ASPNETCORE_Kestrel__Certificates__Default__Password=***
      - ASPNETCORE_Kestrel__Certificates__Default__Path=/app/Certificates/certificate.pfx
      - ASPNETCORE_HTTPS_PORT=7131
    volumes:
      - /Users/***/Certificates/:/app/Certificates
      - fast_server_storage:/app/wwwroot

2

Answers


  1. Chosen as BEST ANSWER

    Finally solved this problem by replacing "https://fast:7131/pageUpdateHub" to "https://0.0.0.0:7131/pageUpdateHub" in client js code. Cors policy:

        services.AddCors(options => options.AddPolicy("CorsPolicy",
                builder =>
                {
                    builder
                        .AllowAnyMethod()
                        .AllowAnyHeader()
                        .AllowCredentials()
                        .SetIsOriginAllowed(_ => true);
                }));
            services.AddSignalR();
    
      app.UseCors("CorsPolicy");
            app.UseRouting();
    
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapHub<PageUpdateHub>("/pageUpdateHub");
    

  2. Addition to the previous answer. It was enough to remove the port when the name of the container is used instead of IP: https://fast/pageUpdateHub

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search