Swagger configuration

Two steps that i needed for this in .NET 6.

Add this to program.cs file;

// Register swagger generator, you can define multiple documents here
services.AddSwaggerGen(s =>
{
	s.SwaggerDoc("v1", new OpenApiInfo
	{
		Title = "FOO API",
		Version = "v1"
	});

	var xmlFile = $"{typeof(Presentation.AssemblyReference).Assembly.GetName().Name}.xml";
	var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
			s.IncludeXmlComments(xmlPath);
		});

Edit project .csproj file and add / change these nodes:

<PropertyGroup>

    <!-- 
    Make sure documentation XML is also included when publishing (not only when testing)
    see https://github.com/Azure/service-fabric-issues/issues/190
    -->
    <GenerateDocumentationFile>true</GenerateDocumentationFile>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <DocumentationFile>bin\$(Configuration)\$(AssemblyName).xml</DocumentationFile>
  </PropertyGroup>

This also works with .NET Core 3.1. more info can be found here.

FavoriteLoadingAdd to favorites
Spread the love

Author: Shahzad Khan

Software developer / Architect