question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SwaggerEndPoints configuration section is missing or empty exception

See original GitHub issue

I am using SwaggerForOcelot with .net core 3.1 for api gateway.

Our ocelot json is divided into modules like below:-

ocelot.call-log.json ocelot.column-chooser.json ocelot.contact-list.json ocelot.documentation.json ocelot.global.json ocelot.inventory-list.json ocelot.json <= this is generated automatically. ocelot.swagger.json <=define here swagger related configuration , pls find below

{ “ReRoutes”: [ { “DownstreamPathTemplate”: “/api/{everything}”, “DownstreamScheme”: “http”, “DownstreamHostAndPorts”: [ { “Host”: “xx.xx.xx.xx”, “Port”: 6004 } ], “UpstreamPathTemplate”: “{everything}”, “UpstreamHttpMethod”: [ “GET”, “POST”, “PUT”, “DELETE” ], “SwaggerKey”: “all” } ], “SwaggerEndPoints”: [ { “Key”: “all”, “Config”: [ { “Name”: “Test API V1”, “Version”: “v1”, “Url”: “http://xx.xx.xx.xx:6004/swagger/v1/swagger.json” } ] } ] }

On running it giving exception on below line:-

app.UseSwaggerForOcelotUI(Configuration, opt => { opt.PathToSwaggerGenerator = “/swagger/docs”; });

Exception details:- at Microsoft.AspNetCore.Builder.BuilderExtensions.GetConfiguration(IConfiguration configuration) at Microsoft.AspNetCore.Builder.BuilderExtensions.<>c__DisplayClass1_0.<UseSwaggerForOcelotUI>b__0(SwaggerUIOptions c) at Microsoft.AspNetCore.Builder.SwaggerUIBuilderExtensions.UseSwaggerUI(IApplicationBuilder app, Action1 setupAction) at Microsoft.AspNetCore.Builder.BuilderExtensions.UseSwaggerForOcelotUI(IApplicationBuilder app, IConfiguration configuration, Action1 setupAction) at SMARTS2.ApiGateway.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env) in C:\Users\rajesh.agrawal\source\repos\SMARTS 2.0 API\SMARTS2.0\SMARTS2.ApiGateway\Startup.cs:line 63 at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder) at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>c__DisplayClass4_0.<Build>b__0(IApplicationBuilder builder) at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)

Let me know why this error coming ?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
amadardcommented, May 1, 2020

Sure thing! I will get it done this weekend.

1reaction
amadardcommented, Apr 28, 2020

First step is to replace the Ocelot FileReRoute, which is why the SwaggerKey is being dropped:

	public class SwaggerFileReRoute: FileReRoute
	{
		public string SwaggerKey { get; set; }
	}

Then we need to replace the FileConfiguration to have the ReRoutes property use our new SwaggerFileReRoute model. This is a full redo, since we cant inherit and replace the ReRoutes property.

	public class SwaggerFileConfiguration
	{
		public List<SwaggerFileReRoute> ReRoutes { get; set; } = new List<SwaggerFileReRoute>();

		public List<FileDynamicReRoute> DynamicReRoutes { get; set; } = new List<FileDynamicReRoute>();

		// Seperate field for aggregates because this let's you re-use ReRoutes in multiple Aggregates
		public List<FileAggregateReRoute> Aggregates { get; set; } = new List<FileAggregateReRoute>();

		public FileGlobalConfiguration GlobalConfiguration { get; set; } = new FileGlobalConfiguration();

		public List<SwaggerEndPointOptions> SwaggerEndPoints { get; set; } = new List<SwaggerEndPointOptions>();
	}

Then we are replacing the AddOcelot(this IConfigurationBuilder builder, string folder, IWebHostEnvironment env) method with our own version that properly uses the above models, and handles the SwaggerEndPoints. This is copy and update of the AddOcelot.

        public static IConfigurationBuilder AddOcelotWithSwaggerSupport(this IConfigurationBuilder builder, string folder, IWebHostEnvironment env)
        {
            string primaryConfigFile = "ocelot.json";

            string globalConfigFile = "ocelot.global.json";

            string SwaggerEndPointsConfigFile = "ocelot.SwaggerEndPoints.json";

            string subConfigPattern = @"^ocelot\.(.*?)\.json$";

            string excludeConfigName = env?.EnvironmentName != null ? $"ocelot.{env.EnvironmentName}.json" : string.Empty;

            var reg = new Regex(subConfigPattern, RegexOptions.IgnoreCase | RegexOptions.Singleline);

            var files = new DirectoryInfo(folder)
                .EnumerateFiles()
                .Where(fi => reg.IsMatch(fi.Name) && (fi.Name != excludeConfigName))
                .ToList();

            var fileConfiguration = new SwaggerFileConfiguration();

            foreach (var file in files)
            {
                if (files.Count > 1 && file.Name.Equals(primaryConfigFile, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                var lines = File.ReadAllText(file.FullName);

                var config = JsonConvert.DeserializeObject<SwaggerFileConfiguration>(lines);

                if (file.Name.Equals(globalConfigFile, StringComparison.OrdinalIgnoreCase))
                {
                    fileConfiguration.GlobalConfiguration = config.GlobalConfiguration;
                }

                if (file.Name.Equals(SwaggerEndPointsConfigFile, StringComparison.OrdinalIgnoreCase))
                {
                    fileConfiguration.SwaggerEndPoints = config.SwaggerEndPoints;
                }

                fileConfiguration.Aggregates.AddRange(config.Aggregates);
                fileConfiguration.ReRoutes.AddRange(config.ReRoutes);
            }

            var json = JsonConvert.SerializeObject(fileConfiguration);

            File.WriteAllText(primaryConfigFile, json);

            builder.AddJsonFile(primaryConfigFile, false, false);

            return builder;
        }

This requires your SwaggerEndPoints be in a file named “ocelot.SwaggerEndPoints.json”.

Then you call that extension method in replacement of AddOcelot()

  webBuilder
  .ConfigureAppConfiguration((hostingContext, config) =>
  {
	  config
		  .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
		  .AddOcelotWithSwaggerSupport(folder, hostingContext.HostingEnvironment);
  })
Read more comments on GitHub >

github_iconTop Results From Across the Web

SwaggerEndPoints configuration section is missing or ...
Hi,. When I'm using per service ocelot.json configuration and file merging, I get System.InvalidOperationException: "SwaggerEndPoints ...
Read more >
Configuring Swagger with values from Iconfiguration object
Second check that this line actually returns correct value and not null or empty string (just put it in local var): this.Configuration.
Read more >
MMLib.SwaggerForOcelot 7.0.0
SwaggerEndPoint is configuration for downstream service swagger generator endpoint. Property Key is used to pair with the Route configuration.
Read more >
Configure Swagger on api gateway using ocelot in asp.net ...
In this article I will create an API gateway using ocelot and asp.net core application and show you how to configure swagger on...
Read more >
Help! My swagger controllers all return a 404 in .NET 6
I was playing around with .NET 6 last week and I was creating a new REST API using swagger. Nothing too complex right?...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found