XML comments file could not be found - Swagger

2020-03-01 19:17发布

问题:

This is certainly one of those that drives you nuts. As the title indicates all I'm simply trying to do is display comments pulled from an xml file using swagger.

I appear to have taken all steps according to the swagger documentation but to no avail. Hopefully you kind folk can point me in the right direction.

Steps Taken:

Ensured file exists:

Configured SwaggerConfig.cs

I've tried changing the path too: @"bin/....xml"

Nothing seems to work.

**The Error "Could not find file": **

Can anyone point me in the right direction please?

Regards,

回答1:

These are the steps I needed in .Net Core 2.2:

Put this in Startup.cs:

    public void ConfigureServices(IServiceCollection services)
    {

        ...

        // Register the Swagger generator, defining 1 or more Swagger documents
        services.AddSwaggerGen(c =>
            {
                ...

                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });
    }

Edit your .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)\YOUR_PROJECT_NAME.xml</DocumentationFile>
  </PropertyGroup>


回答2:

in .net core 3.0

<GenerateDocumentationFile>true</GenerateDocumentationFile>

in PropertyGroup tag of .csproj file



回答3:

Make sure that the project properties in the XML Generate configuration match the XML name of your swagger configuration file. Follow print's to facilitate understanding

Project Properties

swagger configuration



回答4:

Ok, So I managed to get it to work by pointing to the root directory.

I still have no idea why it cannot detect the xml file in the bin directory. Again this worked by adding an xml file within the root.

Code Changes:

var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
                        //var commentsFileName = Assembly.GetExecutingAssembly().GetName().Name + ".XML";
                        var commentsFileName = "Comments" + ".XML";
                        var commentsFile = Path.Combine(baseDirectory, commentsFileName);

                        c.IncludeXmlComments(commentsFile);


回答5:

Using .Net Core 2, here's the lines I needed:

var pathIncludeXmlComments = $@"{env.ContentRootPath}\Events.xml";

services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new Info { Title = "Events API", Version = "v0.1" });
    c.OperationFilter<AuthorizationHeaderParameterOperationFilter>();
    c.IncludeXmlComments(pathIncludeXmlComments);
});

If you're having problems, put a breakpoint after that first line, check the value of "pathIncludeXmlComments", and confirm that VS2017 has stored an .xml file there.

And remember that under Project Properties, in the "Build" tab, you need to tick the "XML documentation file" box, and set the name to the same as shown in the filename above (Events.xml, in this example).