2

I have a web API project where I use SignalR. Recently I added and configured Ocelot to be my gateway towards another web API. Ocelot routes the requests correctly but has messed up my SignalR configuration somehow.

Program.cs:

builder.Services.AddOcelot();   

var app = builder.Build();

app.UseCors("AllowAllApps");

JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

app.UseAuthentication();
app.UseAuthorization();

app.UseMvc();
app.UseOcelot().Wait();

app.MapHub<MyHub>("/myHub");

app.Run();

Client-side code:

// url = "http://localhost:5001/myHub"

connect(url: string, accessToken: string, debug: boolean): Promise<void> {
  this.connection = new signalR.HubConnectionBuilder()
    .withUrl(url, { accessTokenFactory: () => accessToken })
    .configureLogging(debug ? signalR.LogLevel.Information : signalR.LogLevel.Error)
    .withAutomaticReconnect()
    .build();

  return this.connection.start();
}

Ocelot.json

{
  "Routes": [
    {
      "DownstreamPathTemplate": "/{route}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "myotherapi",
          "Port": 80
        }
      ],
      "UpstreamPathTemplate": "/myotherapi/{route}",
      "UpstreamHttpMethod": [ "Post" ],
      "AuthenticationOptions": {
        "AuthenticationProviderKey": "Bearer",
        "AllowedScopes": []
      },
      "AddHeadersToRequest": {
        "UserId": "Claims[sub] > value"
      }
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "http://localhost:5001"
  }
}

I need Ocelot only for HTTP requests. It shouldn't affect websocket connections with this configuration as far as I understand.

If I comment out the Ocelot lines in my Program.cs SignalR works. If I leave it there my POST request to http://localhost:5001/myHub/negotiate?negotiateVersion=1 fails with 404.

Is it something to do with the order of the middleware that is causing the issue? I have tried multiple different orderings but to no avail.

This is a .NET 6 project with Ocelot version 18.0. Both projects are containerized with Docker.

Alternatex
  • 1,505
  • 4
  • 24
  • 47

1 Answers1

1

try adding this

{
      "DownstreamPathTemplate": "/myHub/negotiate",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "myotherapi",
          "Port": "80"
        }
      ],
      "UpstreamPathTemplate": "/myHub/negotiate",
      "UpstreamHttpMethod": [ "POST" ]
    }
CodingMytra
  • 2,234
  • 1
  • 9
  • 21