There are two APIs on two different ports. I want to call these with an address (IP:port ). I used ocelot api gateway
for this purpose. But when running the program, the following error is returned
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.'
Startup.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();
});
}
ocelot.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" ]
}
]
}
CodePudding user response:
In your Configure method you need to tell your application to register and use ocelot
. It is clear from your json file that you have used FileCacheOptions
. So
edit the 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();
}
You should also declare the address of the ocelot.json in the Program.cs file:
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
});
for more information see this link ocelot configuration