Earendur
Earendur

Reputation: 21

Unable to connect signalr with ocelot

I can access and use the signalR service myself using javascript. But when I try it via Ocelot, I get the following errors.

I checked the cors settings.

I checked the ocelot.json file.

I checked DownstreamScheme.

Ocelot Program.cs


using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using Ocelot.Values;

var builder = WebApplication.CreateBuilder(args);

// Configure CORS
builder.Services
    .AddCors(options =>
    {
        options.AddPolicy("CorsPolicy", policy =>
        {
            policy
                .WithOrigins(builder.Configuration.GetSection("CORS:Origins").Get<string[]>() ?? [])
                .AllowAnyMethod()
                .AllowCredentials()
                .AllowAnyHeader()
                .SetIsOriginAllowedToAllowWildcardSubdomains()
                .SetIsOriginAllowed(x => true).AllowAnyMethod().AllowAnyHeader();
        });
    });

// add ocelot 
builder.Services.AddOcelot(builder.Configuration);
builder.Services.AddSignalR();

builder.Configuration.AddJsonFile("ocelot.json");
// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseCors(policy => policy.AllowAnyHeader()
.AllowAnyMethod()
.SetIsOriginAllowed(origin => true)
.AllowCredentials());

app.UseRouting();

app.UseHttpsRedirection();

app.UseAuthorization();

app.UseEndpoints(_ => { });

app.UseCors("CorsPolicy");

app.UseWebSockets();

await app.UseOcelot();

await app.RunAsync();

Ocelot.json

{
  "Routes": [
    {
      "DownstreamPathTemplate": "/api/Authentication/{url}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "192.168.1.23",
          "Port": 6001
        }
      ],
      "UpstreamPathTemplate": "/api/Authentication/{url}",
      "Priority": 1,
      "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ],
      "LoadBalancerOptions": {
        "Type": "LeastConnection"
      }
    },
    {
      "DownstreamPathTemplate": "/api/Teacher/{url}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "192.168.1.23",
          "Port": 6001
        }
      ],
      "UpstreamPathTemplate": "/api/Teacher/{url}",
      "Priority": 1,
      "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ],
      "LoadBalancerOptions": {
        "Type": "LeastConnection"
      }
    },
    {
      "DownstreamPathTemplate": "/api/Certification/{url}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "192.168.1.23",
          "Port": 6001
        }
      ],
      "UpstreamPathTemplate": "/api/Certification/{url}",
      "Priority": 1,
      "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ],
      "LoadBalancerOptions": {
        "Type": "LeastConnection"
      }
    },
    {
      "DownstreamPathTemplate": "/api/Student/{url}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "192.168.1.23",
          "Port": 6001
        }
      ],
      "UpstreamPathTemplate": "/api/Student/{url}",
      "Priority": 1,
      "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ],
      "LoadBalancerOptions": {
        "Type": "LeastConnection"
      }
    },
    {
      "DownstreamPathTemplate": "/api/SchoolLevel/{url}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "192.168.1.23",
          "Port": 6001
        }
      ],
      "UpstreamPathTemplate": "/api/SchoolLevel/{url}",
      "Priority": 1,
      "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ],
      "LoadBalancerOptions": {
        "Type": "LeastConnection"
      }
    },
    {
      "DownstreamPathTemplate": "/api/Country/{url}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "192.168.1.23",
          "Port": 6001
        }
      ],
      "UpstreamPathTemplate": "/api/Country/{url}",
      "Priority": 1,
      "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ],
      "LoadBalancerOptions": {
        "Type": "LeastConnection"
      }
    },
    {
      "DownstreamPathTemplate": "/api/Expertise/{url}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "192.168.1.23",
          "Port": 6001
        }
      ],
      "UpstreamPathTemplate": "/api/Expertise/{url}",
      "Priority": 1,
      "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ],
      "LoadBalancerOptions": {
        "Type": "LeastConnection"
      }
    },
    {
      "DownstreamPathTemplate": "/api/Parameter/{url}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "192.168.1.23",
          "Port": 6001
        }
      ],
      "UpstreamPathTemplate": "/api/Parameter/{url}",
      "Priority": 1,
      "UpstreamHttpMethod": [ "Get", "Post", "Put" ],
      "LoadBalancerOptions": {
        "Type": "LeastConnection"
      }
    },
    {
      "DownstreamPathTemplate": "/api/LessonComment/{url}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "192.168.1.23",
          "Port": 7001
        }
      ],
      "UpstreamPathTemplate": "/api/LessonComment/{url}",
      "Priority": 1,
      "UpstreamHttpMethod": [ "Get", "Post", "Put" ],
      "LoadBalancerOptions": {
        "Type": "LeastConnection"
      }
    },
    {
      "DownstreamPathTemplate": "/api/LessonComplaint/{url}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "192.168.1.23",
          "Port": 7001
        }
      ],
      "UpstreamPathTemplate": "/api/LessonComplaint/{url}",
      "Priority": 1,
      "UpstreamHttpMethod": [ "Get", "Post", "Put" ],
      "LoadBalancerOptions": {
        "Type": "LeastConnection"
      }
    },
    {
      "DownstreamPathTemplate": "/api/LessonScheduled/{url}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "192.168.1.23",
          "Port": 7001
        }
      ],
      "UpstreamPathTemplate": "/api/LessonScheduled/{url}",
      "Priority": 1,
      "UpstreamHttpMethod": [ "Get", "Post", "Put" ],
      "LoadBalancerOptions": {
        "Type": "LeastConnection"
      }
    },
    {
      "DownstreamPathTemplate": "/api/LessonCancellation/{url}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "192.168.1.23",
          "Port": 7001
        }
      ],
      "UpstreamPathTemplate": "/api/LessonCancellation/{url}",
      "Priority": 1,
      "UpstreamHttpMethod": [ "Get", "Post", "Put" ],
      "LoadBalancerOptions": {
        "Type": "LeastConnection"
      }
    },
    {
      "DownstreamPathTemplate": "/api/Chat/{url}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "192.168.1.23",
          "Port": 8001
        }
      ],
      "UpstreamPathTemplate": "/api/Chat/{url}",
      "Priority": 1,
      "UpstreamHttpMethod": [ "GET", "POST", "PUT", "DELETE", "OPTIONS" ],
      "LoadBalancerOptions": {
        "Type": "LeastConnection"
      }
    },
    {
      "DownstreamPathTemplate": "/Chat/{url}", 
      "DownstreamScheme": "ws",
      "DownstreamHostAndPorts": [
        {
          "Host": "192.168.1.23",
          "Port": 8001
        }
      ],
      "UpstreamPathTemplate": "/Chat/{url}",
      "UpstreamHttpMethod": [ "GET", "POST", "PUT", "DELETE", "OPTIONS" ]
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "https://192.168.1.23:9001"
  }
}



Error:

enter image description here

Network:

enter image description here

I edited the CORS settings.

I reviewed all the sample topics on Stack Overflow.

Upvotes: 2

Views: 53

Answers (0)

Related Questions