Burgyn / MMLib.SwaggerForOcelot

This repo contains swagger extension for ocelot.
MIT License
352 stars 94 forks source link

System Aggregate Exception #223

Closed Ugonna06 closed 2 years ago

Ugonna06 commented 2 years ago

I have been trying to configure a swagger for ocelot to display all my upstream routes in a swagger format but I keep getting this exception below;

System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: MMLib.SwaggerForOcelot.Aggregates.IRoutesDocumentationProvider Lifetime: Transient ImplementationType: MMLib.SwaggerForOcelot.Aggregates.RoutesDocumentationProvider': Unable to resolve service for type 'Ocelot.ServiceDiscovery.IServiceDiscoveryProviderFactory' while attempting to activate 'MMLib.SwaggerForOcelot.ServiceDiscovery.SwaggerServiceDiscoveryProvider'.) (Error while validating the service descriptor 'ServiceType: MMLib.SwaggerForOcelot.Repositories.IDownstreamSwaggerDocsRepository Lifetime: Transient ImplementationType: MMLib.SwaggerForOcelot.Repositories.DownstreamSwaggerDocsRepository': Unable to resolve service for type 'Ocelot.ServiceDiscovery.IServiceDiscoveryProviderFactory' while attempting to activate 'MMLib.SwaggerForOcelot.ServiceDiscovery.SwaggerServiceDiscoveryProvider'.) (Error while validating the service descriptor 'ServiceType: MMLib.SwaggerForOcelot.ServiceDiscovery.ISwaggerServiceDiscoveryProvider Lifetime: Transient ImplementationType: MMLib.SwaggerForOcelot.ServiceDiscovery.SwaggerServiceDiscoveryProvider': Unable to resolve service for type 'Ocelot.ServiceDiscovery.IServiceDiscoveryProviderFactory' while attempting to activate 'MMLib.SwaggerForOcelot.ServiceDiscovery.SwaggerServiceDiscoveryProvider'.) (Error while validating the service descriptor 'ServiceType: MMLib.SwaggerForOcelot.Aggregates.IAggregateRouteDocumentationGenerator Lifetime: Transient ImplementationType: MMLib.SwaggerForOcelot.Aggregates.AggregateRouteDocumentationGenerator': Unable to resolve service for type 'Ocelot.ServiceDiscovery.IServiceDiscoveryProviderFactory' while attempting to activate 'MMLib.SwaggerForOcelot.ServiceDiscovery.SwaggerServiceDiscoveryProvider'.)'

see below my program class.

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); //builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerForOcelot(builder.Configuration);

var app = builder.Build();

// Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwaggerForOcelotUI(opt => { opt.PathToSwaggerGenerator = "/swagger/docs"; }); }

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

After the service gets configured... it throws an exception on the builder.Build();

below is my ocelot.json

{ "Routes": [ { "DownstreamPathTemplate": "/api/WeatherForecast/{everything}", "DownstreamScheme": "https", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 44338 } ], "UpstreamPathTemplate": "/api/weathertest/{everything}", "UpstreamHttpMethod": [], "SwaggerKey": "Weatherforecast" }, { "DownstreamPathTemplate": "/api/User/{everything}", "DownstreamScheme": "https", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 44338 } ], "UpstreamPathTemplate": "/api/usertest/{everything}", "UpstreamHttpMethod": [], "SwaggerKey": "Weatherforecast" }, { "DownstreamPathTemplate": "/api/TestLogGate/{everything}", "DownstreamScheme": "https", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 44378 } ], "UpstreamPathTemplate": "/api/testLog/{everything}", "UpstreamHttpMethod": [], "SwaggerKey": "TestLogGate" }, { "DownstreamPathTemplate": "/api/TestWeather/{everything}", "DownstreamScheme": "https", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 44378 } ], "UpstreamPathTemplate": "/api/tWeather/{everything}", "UpstreamHttpMethod": [], "SwaggerKey": "TestWeather" } ], "SwaggerEndPoints": [ { "Key": "Weatherforecast", "Config": [ { "Name": "Second Test Api", "Version": "v1", "Url": "https://localhost:44338/swagger/v1/swagger.json" } ] }, { "Key": "TestLogGate", "Config": [ { "Name": "Test Logger Api", "Version": "v1", "Url": "https://localhost:44378/swagger/v1/swagger.json" } ] } ], "GlobalConfiguration": { "BaseUrl": "https://localhost:44396/" } }

Burgyn commented 2 years ago

Hi @Ugonna06,

I didn't see how you configured the Ocelot gateway in your Program.cs?

Ugonna06 commented 2 years ago

Hi Mino,

I just attached my program.cs to this email.

you'll be able to see the comment where I added swaggerforocelot in the services

On Thu, Mar 10, 2022 at 1:25 PM Miňo Martiniak @.***> wrote:

Hi @Ugonna06 https://github.com/Ugonna06,

I didn't see how you configured the Ocelot gateway in your Program.cs?

— Reply to this email directly, view it on GitHub https://github.com/Burgyn/MMLib.SwaggerForOcelot/issues/223#issuecomment-1064000429, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOIY6LLEQ4APBPW3HL4SA7LU7HS3VANCNFSM5QMNJS4Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you were mentioned.Message ID: @.***>

Burgyn commented 2 years ago

Hi, this package bring swagger documentation to the Ocelot Api Gateway. So you must configure and run the Ocelot. E.g.:

services.AddOcelot();
...
app.UseOcelot().Wait();

I didn't see it in your code. See the Ocelot documentation for more info.

Ugonna06 commented 2 years ago

Hello Mino,

Am I supposed to put the AddOcelot(); and the AddSwaggerForOcelot();?

On Thu, 10 Mar 2022, 7:06 PM Miňo Martiniak @.***> wrote:

Hi, this package bring swagger documentation to the Ocelot Api Gateway. So you must configure and run the Ocelot. E.g.:

services.AddOcelot(); ...app.UseOcelot().Wait();

I didn't see it in your code. See the Ocelot documentation https://ocelot.readthedocs.io/en/latest/introduction/gettingstarted.html for more info.

— Reply to this email directly, view it on GitHub https://github.com/Burgyn/MMLib.SwaggerForOcelot/issues/223#issuecomment-1064347297, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOIY6LLMFG6JC4Z7BSCQJR3U7I2Y5ANCNFSM5QMNJS4Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you were mentioned.Message ID: @.***>

Ugonna06 commented 2 years ago

Hello Mino,

I now have a "SwaggerEndPoints configuration section is missing or empty exception" exception at the pipeline.

your suggestion was helpful in passing the first one

Burgyn commented 2 years ago

What does your ocelot.json and program.cs look like now?

🙏 Please use markdown code formatting.

Ugonna06 commented 2 years ago

Hi Mino,

Thank you for the follow up.

for the program.cs the code is below


using Ocelot.DependencyInjection; using Ocelot.Middleware;

var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); //builder.Services.AddSwaggerGen(); builder.Services.AddOcelot(); builder.Services.AddSwaggerForOcelot(builder.Configuration);//This is where i configured the swagger for ocelot

var app = builder.Build();

// Configure the HTTP request pipeline. app.UseOcelot().Wait(); if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerForOcelotUI(opt => { opt.PathToSwaggerGenerator = "/swagger/docs"; opt.SwaggerEndpoint("/swagger/v1/swagger.json", "Gateway v1"); });

} app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.Run();


Then this is the ocelot.json config

{ "Routes": [ { "DownstreamPathTemplate": "/api/WeatherForecast/{everything}", "DownstreamScheme": "https", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": "44338" } ], "UpstreamPathTemplate": "/api/weathertest/{everything}", "UpstreamHttpMethod": [], "SwaggerKey": "Weatherforecast" }, { "DownstreamPathTemplate": "/api/User/{everything}", "DownstreamScheme": "https", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": "44338" } ], "UpstreamPathTemplate": "/api/usertest/{everything}", "UpstreamHttpMethod": [], "SwaggerKey": "Weatherforecast" }, { "DownstreamPathTemplate": "/api/TestLogGate/{everything}", "DownstreamScheme": "https", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": "44378" } ], "UpstreamPathTemplate": "/api/testLog/{everything}", "UpstreamHttpMethod": [], "SwaggerKey": "TestLogGate" }, { "DownstreamPathTemplate": "/api/TestWeather/{everything}", "DownstreamScheme": "https", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 44378 } ], "UpstreamPathTemplate": "/api/tWeather/{everything}", "UpstreamHttpMethod": [], "SwaggerKey": "TestWeather" } ], "SwaggerEndPoints": [ { "Key": "Weatherforecast", "Config": [ { "Name": "Second Test Api", "Version": "v1", "Url": "https://localhost:44338/swagger/v1/swagger.json" } ] }, { "Key": "TestLogGate", "Config": [ { "Name": "Test Logger Api", "Version": "v1", "Url": "https://localhost:44378/swagger/v1/swagger.json" } ] } ], "GlobalConfiguration": { "BaseUrl": "https://localhost:44396/" } }

thanks a lot, I look forward to hearing from you.

On Mon, Mar 14, 2022 at 11:04 AM Miňo Martiniak @.***> wrote:

What does your ocelot.json and program.cs look like now?

🙏 Please use markdown code formatting.

— Reply to this email directly, view it on GitHub https://github.com/Burgyn/MMLib.SwaggerForOcelot/issues/223#issuecomment-1066592237, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOIY6LOZAMAQ5DX2ATZGYDDU74FJ3ANCNFSM5QMNJS4Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you were mentioned.Message ID: @.***>

Burgyn commented 2 years ago

Hi @Ugonna06,

please read the Ocelot documentation carefully.

This project is just an addon to Ocelot API Gateway and is not designed to run without its correct configuration.

You are missed to add ocelot.json file to configuration builder. E.g.: builder.Configuration.AddJsonFile("ocelot.json");. And in your case you don't have to call app.UseOcelot().Wait();.

So after a little tweak, it may work:

using Ocelot.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);

builder.Configuration.AddJsonFile("ocelot.json");

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();

builder.Services.AddOcelot();
builder.Services.AddSwaggerForOcelot(builder.Configuration);

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerForOcelotUI(opt =>
    {
        opt.PathToSwaggerGenerator = "/swagger/docs";
        opt.SwaggerEndpoint("/swagger/v1/swagger.json", "Gateway v1");
    });

}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
Ugonna06 commented 2 years ago

Hi Mino,

Thanks a lot for this update, It ran successfully.

I really appreciate.

On Mon, Mar 14, 2022 at 8:29 PM Miňo Martiniak @.***> wrote:

Hi @Ugonna06 https://github.com/Ugonna06,

please read the Ocelot documentation https://ocelot.readthedocs.io/en/latest/introduction/gettingstarted.html#net-6-0 carefully.

This project is just an addon to Ocelot API Gateway and is not designed to run without its correct configuration.

You are missed to add ocelot.json file to configuration builder. E.g.: builder.Configuration.AddJsonFile("ocelot.json");. And in your case you don't have to call app.UseOcelot().Wait();.

So after a little tweak, it may work:

using Ocelot.DependencyInjection; var builder = WebApplication.CreateBuilder(args); builder.Configuration.AddJsonFile("ocelot.json"); builder.Services.AddControllers();builder.Services.AddEndpointsApiExplorer(); builder.Services.AddOcelot();builder.Services.AddSwaggerForOcelot(builder.Configuration); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerForOcelotUI(opt => { opt.PathToSwaggerGenerator = "/swagger/docs"; opt.SwaggerEndpoint("/swagger/v1/swagger.json", "Gateway v1"); });

}app.UseHttpsRedirection();app.UseAuthorization();app.MapControllers();app.Run();

— Reply to this email directly, view it on GitHub https://github.com/Burgyn/MMLib.SwaggerForOcelot/issues/223#issuecomment-1067205212, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOIY6LNNULWO3A375EWGADTU76HSFANCNFSM5QMNJS4Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you were mentioned.Message ID: @.***>