Search code examples
asp.net-corenuget-packageasp.net-core-2.1swashbuckle.aspnetcore

How to publish XML files from a NuGet package to an ASP.NET Core 2.1 application


I have an ASP.NET Core 2.1 application (say, MyWebApiApp) that references a NuGet package (say, MyPackage), which contains models used in a Web API. These models are documented using XML comments, and the resulting XML file is published along with the NuGet package (in lib\netstandard2.0\MyPackage.xml).

I want to use the XML documentation in Swagger, so I add the following to my SwaggerGenOptions in Startup.cs:

c.IncludeXmlComments(Path.ChangeExtension(typeof(MyPackage.MyModel).Assembly.Location, ".xml"));

So far, so good. When I run the application from Visual Studio 2017, it successfully locates the XML documentation file in the NuGet package cache and generates the Swagger documentation.

Now I want to publish the web site. When I publish the web site:

  • The XML documentation file for application (MyWebApiApp.xml) is copied to the publish folder (I have included <GenerateDocumentationFile>true</GenerateDocumentationFile> in MyWebApiApp.csproj).
  • But the XML documentation file from my NuGet package (MyPackage.xml) is not copied.

I've tried adding the incantation from this blog post to MyWebApiApp.csproj, without success.


Solution

  • I found a solution here.

    The following incantation needs to be added to the pubxml file:

    <Target Name="_ResolvePublishNuGetPackagePdbsAndXml"
          AfterTargets="RunResolvePublishAssemblies">
      <ItemGroup>
        <ResolvedFileToPublish
          Include="@(ResolvedAssembliesToPublish->'%(RootDir)%(Directory)%(Filename).pdb')"
          RelativePath="$([System.IO.Path]::ChangeExtension(%(ResolvedAssembliesToPublish.DestinationSubPath), '.pdb'))"
          DestinationSubPath="$([System.IO.Path]::ChangeExtension(%(ResolvedAssembliesToPublish.DestinationSubPath), '.pdb'))"
          Condition="'%(ResolvedAssembliesToPublish.PackageName)' != ''
                      and Exists('%(RootDir)%(Directory)%(Filename).pdb')" />
        <ResolvedFileToPublish
          Include="@(ResolvedAssembliesToPublish->'%(RootDir)%(Directory)%(Filename).xml')"
          RelativePath="$([System.IO.Path]::ChangeExtension(%(ResolvedAssembliesToPublish.DestinationSubPath), '.xml'))"
          DestinationSubPath="$([System.IO.Path]::ChangeExtension(%(ResolvedAssembliesToPublish.DestinationSubPath), '.xml'))"
          Condition="'%(ResolvedAssembliesToPublish.PackageName)' != ''
                      and Exists('%(RootDir)%(Directory)%(Filename).xml')" />
      </ItemGroup>
    </Target>