Prachi Singh
Prachi Singh

Reputation: 37

Integrating Ocelot 16.0 with ASP.Net Core 3.1 not working as I need to use Swagger with Ocelot

I have used Ocelot with Asp.Net Core 2.1 and its working but when using with Asp.Net Core 3.1 it is not working at all. In my opinion, it is not picking up the "ocelot.json" file. Following is my Program.cs file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace ApiGateway
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((hostingContext, config) =>
                {
                    config
                        .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
                        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                        .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true)
                        .AddJsonFile($"ocelot.{hostingContext.HostingEnvironment.EnvironmentName}.json")
                        .AddEnvironmentVariables();
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }
}

And below is Startup.cs file :

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;

namespace ApiGateway
{
    public class Startup
    {
        private IConfiguration configuration;
        public Startup(IConfiguration configuration)
        {
            this.configuration = configuration;
        }


        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddOcelot(configuration);
        }

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

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context => {

                    await context.Response.WriteAsync("API GATEWAY FUNCIONANDO");
                });
            });

            app.UseOcelot().Wait();
        }
    }
}

Below is ocelot.Development.json file :

{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/TestApi/api/test",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": "80"
        }
      ],
      "UpstreamPathTemplate": "/getTest"
    },
    {
      "DownstreamPathTemplate": "/TestMicroS/api/values",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": "82"
        }
      ],
      "UpstreamPathTemplate": "/getValues"
    }
  ],
  "GlobalConfiguration": {
    
  }
}

I have referred all above configuration from YouTube but it is not working. Local website is working but when I try to hit the UpStream path it is giving me localhost not found 404 error. Please assist me to resolve this.

Thanks in advance.

Upvotes: 2

Views: 4623

Answers (2)

raw_hitt
raw_hitt

Reputation: 989

I had similar issue recently, apart from the ReRoutes to Routes , I had another problem in In the ocelote.json file

"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
 {
     "Host": "localhost",
     "Port": 7083
 }

The port number which I had configured was for https and not for http in launchsettings.json file of the project

"applicationUrl": "https://localhost:7083",

I changed it to https to make it work

"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
 {
     "Host": "localhost",
     "Port": 7083
 }

Upvotes: 2

Panos
Panos

Reputation: 61

Try renaming ReRoutes to Routes in your JSON file.

Upvotes: 6

Related Questions