在两个不同的端口上有两个 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/