c# - ocelot api网关中的System.InvalidOperationExceptio

在两个不同的端口上有两个 API。我想用地址 (IP:port ) 来调用它们。为此,我使用了 ocelot api gateway。但是运行程序时,返回如下错误

System.InvalidOperationException: 'Unable to find the required services. Please add all the required services by calling 'IServiceCollection.AddAuthorization' inside the call to 'ConfigureServices(...)' in the application startup code.'

启动.cs:

    public void ConfigureServices(IServiceCollection services)
    {

    }

    public async void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
        }

        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });           
    }

豹猫.json

{
  "Routes": [
  {
  "DownstreamPathTemplate": "/api/User",
  "DownstreamScheme": "http",
  "FileCacheOptions": {
      "TtlSeconds": 15,
      "Region": "somename"
  },
  "DownstreamHostAndPorts": [
    {
      "Host": "127.0.0.1",
      "Port": 8072
    }
  ],
  "UpstreamPathTemplate": "/user",
  "UpstreamHttpMethod": [ "GET", "POST" ]
},
{
  "DownstreamPathTemplate": "/api/user/{id}",
  "DownstreamScheme": "http",
  "DownstreamHostAndPorts": [
    {
      "Host": "127.0.0.1",
      "Port": 8071
    }
  ],
  "UpstreamPathTemplate": "/user/{id}",
  "UpstreamHttpMethod": [ "GET" ]
}
] 
}

最佳答案

在您的 Configure 方法中,您需要告诉您的应用程序注册并使用 ocelot。从您的 json 文件中可以清楚地看出您使用了 FileCacheOptions。所以

编辑 Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
   services.AddOcelot()
  .AddCacheManager(x =>
   {
      x.WithDictionaryHandle();
   });
}

public async void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Error");
    }

    app.UseStaticFiles();

    app.UseRouting();

    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });   
   await app.UseOcelot(); 
}

你还应该在 Program.cs 文件中声明 ocelot.json 的地址:

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
         {
                webBuilder.UseStartup<Startup>();
         }).ConfigureAppConfiguration((hostingContext, config) =>
            {
                config.AddJsonFile("ocelot.json"); //The exact path of the ocelot.json
            });

有关更多信息,请参阅此链接 ocelot configuration

关于c# - ocelot api网关中的System.InvalidOperationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74183753/

相关文章:

c - 在分配内存之前对灵活成员数组的第一个元素使用 sizeof 是未定义的行为?

r - 如何合并两个数据框并提取列的唯一组合?

r - 使用 group_by() 根据条件折叠 R 中的数据集

javascript - 左侧条件分配

r - R中,如果有多个TRUE答案,选择第一个TRUE答案

c# - .NET 6 将参数检查替换为 ArgumentNullException.ThrowIf

python - 将扁平化列表转换为字典列表

r - 选定列乘以给定向量

r - 更快地填充矩阵

haskell - 如何声明一个参数可以是 (Int, Int) 或 Maybe (Int, Int