Step 1: Update reference project .csproj
file and add DocumentationFile
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(MSBuildThisFileName).xml</DocumentationFile>
</PropertyGroup>
This will cause the build of the project to output XML document along with the assembly of compiled project inside the bin folder.
API/Web project can access all reference assembly with a reflection by calling Assembly.GetReferencedAssemblies()
method on the currently executing assembly.
Step 2: Update swagger configuration
builder.Services.AddSwaggerGen(options =>
{
options.CustomSchemaIds(type => type.ToString());
var currentAssembly = Assembly.GetExecutingAssembly();
var xmlDocs = currentAssembly.GetReferencedAssemblies()
.Union(new AssemblyName[] { currentAssembly.GetName() })
.Select(a => Path.Combine(Path.GetDirectoryName(currentAssembly.Location), $"{a.Name}.xml"))
.Where(f => File.Exists(f)).ToArray();
Array.ForEach(xmlDocs, (d) =>
{
options.IncludeXmlComments(d);
});
});