0

New to Ocelot and .net core. I am trying to implement caching in .net core 3.0 microservice in Ocelot gateway. As per Ocelot guideline (https://ocelot.readthedocs.io/en/latest/features/caching.html)

As per second step, my startup.cs looks like:

using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;

namespace MS_APIGateway
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddOcelot().AddCacheManager(x => **// Getting error here**
                {
                    x.WithDictionaryHandle(); 
                });
        }

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

            app.UseHttpsRedirection();

            app.UseRouting();
            //JWT
            app.UseAuthentication();
            //JWT
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
            await app.UseOcelot();
        }
    }
}


The compiler is throwing error at AddCacheManager line and the error message is:

Error   CS0411  The type arguments for method 'ServiceCollectionExtensions.AddCacheManager<T>(IServiceCollection, IConfiguration, string, Action<ConfigurationBuilder>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Please help. Thank you.

TechTurtle
  • 2,667
  • 4
  • 22
  • 31

2 Answers2

1

we have to add using Ocelot.Cache.CacheManager;

TechTurtle
  • 2,667
  • 4
  • 22
  • 31
0

Need to add nuget package for Ocelot.Cache.CacheManager

Then need to add the below code in program.cs file (.net 6.0 and higher)

builder.Services.AddOcelot(builder.Configuration)
    .AddCacheManager(x =>
    {
        x.WithDictionaryHandle();
    });

Then need to add the following configuration in ocelot.json file (specially FileCacheOptions)

{
      "DownstreamPathTemplate": "/api/product/getProduct",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": "44300"
        }
      ],
      "FileCacheOptions": {
        "TtlSeconds": 10 // This is for caching
      },
      "UpstreamPathTemplate": "/getProduct",
      "Key": "Product"
    }
Khabir
  • 5,370
  • 1
  • 21
  • 33