Aspnet-api-versioning: IApiVersionDescriptionProvider is not part of Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer package

Created on 11 Jun 2018  路  3Comments  路  Source: microsoft/aspnet-api-versioning

Hi,

In this example IApiVersionDescriptionProvider is used to resolve controller versions. This class is not resolved when Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer package is installed.

Example is working by referencing Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer project directly, not installing it as Nuget package. We are unable to integrate controller versions with Swashbuckle using this type.

answered asp.net core question

Most helpful comment

Sounds like your using the wrong package. Are you sure you are using Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer and not just Microsoft.AspNetCore.Mvc.ApiExplorer. The versioned API explorer is different and in a separate package. You also need to use services.AddVersionedApiExplorer().

For reference, the package is on NuGet here. The required types are definitely there; otherwise, it wouldn't work and a lot of people are using it.

Let me know if I've missed something or you have any other integration issues.

Thanks

All 3 comments

Sounds like your using the wrong package. Are you sure you are using Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer and not just Microsoft.AspNetCore.Mvc.ApiExplorer. The versioned API explorer is different and in a separate package. You also need to use services.AddVersionedApiExplorer().

For reference, the package is on NuGet here. The required types are definitely there; otherwise, it wouldn't work and a lot of people are using it.

Let me know if I've missed something or you have any other integration issues.

Thanks

Hi,

Thank you for your response. After installing Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer package I was able to use AddVersionedApiExplorer() method and IApiVersionDescriptionProvider type. However, all endpoints are disappeared from Swagger Ui. My Startup class looks like this:

public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer(options =>
                {
                    options.TokenValidationParameters = new TokenValidationParameters
                    {
                        ValidateIssuer = true,
                        ValidateAudience = true,
                        ValidateLifetime = true,
                        ValidateIssuerSigningKey = true,
                        ValidIssuer = "http://localhost:60900",
                        ValidAudience = "http://localhost:60900",
                        IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration.GetSection("JWTSecret").Value))
                    };
                });
            services.AddMvcCore().AddVersionedApiExplorer();

            services.AddMvc();



            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info { Title = "Retail Listing API", Version = "v1" });

                c.AddSecurityDefinition("Bearer", new ApiKeyScheme()
                {
                    Description = "JWT Authorization header using Bearer scheme. Example \"Authorization: Bearer {token}\"",
                    Name = "Authorization",
                    In = "header",
                    Type = "apiKey",

                });

                var dic = new Dictionary<string, IEnumerable<string>>();
                dic.Add("Bearer", null);

                var provider = services.BuildServiceProvider().GetRequiredService<IApiVersionDescriptionProvider>();

                //// add a swagger document for each discovered API version
                //// note: you might choose to skip or document deprecated API versions differently
                foreach (var description in provider.ApiVersionDescriptions)
                {
                    c.SwaggerDoc(description.GroupName, CreateInfoForApiVersion(description));
                }

                c.AddSecurityRequirement(dic);
                c.OperationFilter<SwaggerDefaultValues>();

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

                c.IncludeXmlComments(commentsFile);
            });

            services.AddApiVersioning(cfg => {
                cfg.AssumeDefaultVersionWhenUnspecified = true;
                cfg.DefaultApiVersion = new Microsoft.AspNetCore.Mvc.ApiVersion(1, 0);
            });
}

 static Info CreateInfoForApiVersion(ApiVersionDescription description)
        {
            var info = new Info()
            {
                Title = $"Sample API {description.ApiVersion}",
                Version = description.ApiVersion.ToString(),
                Description = "A sample application with Swagger, Swashbuckle, and API versioning.",
                Contact = new Contact() { Name = "Bill Mei", Email = "[email protected]" },
                TermsOfService = "Shareware",
                License = new License() { Name = "MIT", Url = "https://opensource.org/licenses/MIT" }
            };

            if (description.IsDeprecated)
            {
                info.Description += " This API version has been deprecated.";
            }

            return info;
        }

On top of my controller, [Route("api/v{version:apiVersion}/Listing")] and [ApiVersion("1.0")] are defined. Versioning working fine on request level, just methods went missing on Swagger. My Startup.cs is looking same with the example linked in the same message, am I missing something in my code?

I was missing few points. Just adding them in case someone else needs them.

Removed first line of Swagger config: c.SwaggerDoc("v1", new Info { Title = "Retail Listing API", Version = "v1" });

Added following config from the example:

app.UseSwaggerUI(
                 options =>
                 {
                    // build a swagger endpoint for each discovered API version
                    foreach (var description in provider.ApiVersionDescriptions)
                     {
                         options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
                     }
                 });

And it works. Thank you @commonsensesoftware for your help.

Was this page helpful?
0 / 5 - 0 ratings