-1

Project Structure:

  1. Console application - Launch the WebAPI startup
  2. API Project(DLL) - Only configuration (Container and authorization and Controller class initialization using(AssemblyPart))
  3. ControllerClass(DLL) - Has API methods

SwaggerDocumentaion Configuration, ConfigureService Class Code:

    services.AddSwaggerGen(options =>
        {
            options.DescribeAllEnumsAsStrings();
            options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info
            {
                Title = "TestAPI",
                Version = "v1",
                Description = "TestApplication API for modular monolith .NET application.",
            });

            var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
            var commentsFileName = "Test.Modules.UserAccess.Facade.XML";//Assembly.GetExecutingAssembly().GetName().Name + ".XML";
            var commentsFile = Path.Combine(baseDirectory, commentsFileName);
            options.IncludeXmlComments(commentsFile);

            var securityToken = new Dictionary<string, IEnumerable<string>>
            {
                {"Bearer", new string[] { }},
            };

            options.AddSecurityDefinition("Bearer", new ApiKeyScheme
            {
                Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
                Name = "Authorization",
                In = "header",
                Type = "apiKey"
            });
            options.AddSecurityRequirement(securityToken);
        });

Configure Class:

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider 
  serviceProvider)
    {
        app.UseSwagger();
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "TestAPI");
            c.RoutePrefix = string.Empty;

        });
    }

I have generated the XML file and copied to the output directory.

Question:

I am getting the error "Failed to Load API Definition". Help me out sort out this issue.

Saiprabhu
  • 1
  • 1
  • Found the issue. Swashbuckle fails throws NotSupportedException exception. To fix the exception. Differentiate the get method correctly. e.g. [HTTPGet("{id}")] . After that document got generated. – Saiprabhu Dec 31 '19 at 10:56

1 Answers1

0

Can you double check Controller methods? For example, Swagger requires explicit [HttpPost] and [HttpGet] If you have Get(int id) or Post(..), this will return Failed to Load API Definition

Nainesh Patel
  • 488
  • 2
  • 5
  • 19