Swagger IncludeXmlComments PlatformServices (obsolete) replacement

We migrated a project to ASP.NET Core 2 (preview) and needed to configure swagger.

In ASP.NET Core v1 we used this code to load the auto generated xml file into swagger:

services.ConfigureSwaggerGen(options =>

{

//Determine base path for the application.

var basePath = PlatformServices.Default.Application.ApplicationBasePath;

 

//Set the comments path for the swagger json and ui.

options.IncludeXmlComments(System.IO.Path.Combine(basePath, "ProjectName.xml"));

});

Since PlatformServices is obsolete (https://github.com/aspnet/PlatformAbstractions), you shouldn't use this for your (new) projects anymore.
Like the github page states, you should use the equivalent .NET API instead.

Here is what I use instead of PlatFormAbstractions (PlatformServices) for loading the xml into swagger:

services.ConfigureSwaggerGen(options =>

{

//Determine base path for the application.

var basePath = AppContext.BaseDirectory;

 

var assemblyName = System.Reflection.Assembly.GetEntryAssembly().GetName().Name;

 

var fileName = System.IO.Path.GetFileName(assemblyName + ".xml");

 

//Set the comments path for the swagger json and ui.

options.IncludeXmlComments(System.IO.Path.Combine(basePath, fileName));

});

Hint: You could add a check here, if the file really exists before loading it.


Posted Jun 30 2017, 11:31 AM by Holger Vetter
developers.de is a .Net Community Blog powered by daenet GmbH.