From e1aaa25cbcf77026877c9a2a5098b34aaca18333 Mon Sep 17 00:00:00 2001 From: Stuart Ferguson Date: Sat, 3 Apr 2021 07:36:24 +0100 Subject: [PATCH 1/3] remove versioning --- .../MessagingServiceClient.cs | 3 - .../Common/ConfigureSwaggerOptions.cs | 73 +++++-------------- MessagingService/MessagingService.csproj | 22 +++--- MessagingService/Startup.cs | 40 +--------- 4 files changed, 29 insertions(+), 109 deletions(-) diff --git a/MessagingService.Client/MessagingServiceClient.cs b/MessagingService.Client/MessagingServiceClient.cs index b62992b..bf66440 100644 --- a/MessagingService.Client/MessagingServiceClient.cs +++ b/MessagingService.Client/MessagingServiceClient.cs @@ -42,9 +42,6 @@ public MessagingServiceClient(Func baseAddressResolver, HttpClient httpClient) : base(httpClient) { this.BaseAddressResolver = baseAddressResolver; - - // Add the API version header - this.HttpClient.DefaultRequestHeaders.Add("api-version", "1.0"); } #endregion diff --git a/MessagingService/Common/ConfigureSwaggerOptions.cs b/MessagingService/Common/ConfigureSwaggerOptions.cs index 08e9364..6b2895b 100644 --- a/MessagingService/Common/ConfigureSwaggerOptions.cs +++ b/MessagingService/Common/ConfigureSwaggerOptions.cs @@ -1,15 +1,8 @@ -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace MessagingService.Common +namespace MessagingService.Common { using System.Diagnostics.CodeAnalysis; - using Microsoft.AspNetCore.Mvc; - using Microsoft.AspNetCore.Mvc.Abstractions; + using System.Linq; using Microsoft.AspNetCore.Mvc.ApiExplorer; - using Microsoft.AspNetCore.Mvc.Versioning; - using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.SwaggerGen; @@ -22,36 +15,11 @@ namespace MessagingService.Common [ExcludeFromCodeCoverage] public class ConfigureSwaggerOptions : IConfigureOptions { - #region Fields - - /// - /// The provider - /// - private readonly IApiVersionDescriptionProvider provider; - - #endregion - - #region Constructors - - /// - /// Initializes a new instance of the class. - /// - /// The provider used to generate Swagger documents. - public ConfigureSwaggerOptions(IApiVersionDescriptionProvider provider) => this.provider = provider; - - #endregion - #region Methods /// public void Configure(SwaggerGenOptions options) { - // add a swagger document for each discovered API version - // note: you might choose to skip or document deprecated API versions differently - foreach (ApiVersionDescription description in this.provider.ApiVersionDescriptions) - { - options.SwaggerDoc(description.GroupName, ConfigureSwaggerOptions.CreateInfoForApiVersion(description)); - } } /// @@ -59,24 +27,19 @@ public void Configure(SwaggerGenOptions options) /// /// The description. /// - private static OpenApiInfo CreateInfoForApiVersion(ApiVersionDescription description) + private static OpenApiInfo CreateInfoForApiVersion() { OpenApiInfo info = new OpenApiInfo - { - Title = "Messaging API", - Version = description.ApiVersion.ToString(), - Description = "A REST Api to manage sending of various messages over different formats, currently only Email and SMS are supported.", - Contact = new OpenApiContact - { - Name = "Stuart Ferguson", - Email = "golfhandicapping@btinternet.com" - } - }; - - if (description.IsDeprecated) - { - info.Description += " This API version has been deprecated."; - } + { + Title = "Messaging API", + Version = "1.0", + Description = "A REST Api to manage sending of various messages over different formats, currently only Email and SMS are supported.", + Contact = new OpenApiContact + { + Name = "Stuart Ferguson", + Email = "golfhandicapping@btinternet.com" + } + }; return info; } @@ -92,6 +55,8 @@ private static OpenApiInfo CreateInfoForApiVersion(ApiVersionDescription descrip [ExcludeFromCodeCoverage] public class SwaggerDefaultValues : IOperationFilter { + #region Methods + /// /// Applies the filter to the specified operation using the given context. /// @@ -101,10 +66,6 @@ public void Apply(OpenApiOperation operation, OperationFilterContext context) { ApiDescription apiDescription = context.ApiDescription; - ApiVersion apiVersion = apiDescription.GetApiVersion(); - ApiVersionModel model = apiDescription.ActionDescriptor.GetApiVersionModel(ApiVersionMapping.Explicit | ApiVersionMapping.Implicit); - - operation.Deprecated = model.DeprecatedApiVersions.Contains(apiVersion); if (operation.Parameters == null) { @@ -123,5 +84,7 @@ public void Apply(OpenApiOperation operation, parameter.Required |= description.IsRequired; } } + + #endregion } -} +} \ No newline at end of file diff --git a/MessagingService/MessagingService.csproj b/MessagingService/MessagingService.csproj index 7486a24..b3b37f8 100644 --- a/MessagingService/MessagingService.csproj +++ b/MessagingService/MessagingService.csproj @@ -8,23 +8,19 @@ - - + + - - - - - - - - - - - + + + + + + + diff --git a/MessagingService/Startup.cs b/MessagingService/Startup.cs index 3b2be2d..71a8e52 100644 --- a/MessagingService/Startup.cs +++ b/MessagingService/Startup.cs @@ -33,14 +33,11 @@ namespace MessagingService using MediatR; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Diagnostics.HealthChecks; - using Microsoft.AspNetCore.Mvc.ApiExplorer; - using Microsoft.AspNetCore.Mvc.Versioning; using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Options; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using NLog.Extensions.Logging; - using NuGet.Versioning; using Service.Services.Email.IntegrationTest; using Service.Services.SMSServices.IntegrationTest; using Shared.DomainDrivenDesign.EventSourcing; @@ -234,30 +231,6 @@ private void ConfigureMiddlewareServices(IServiceCollection services) failureStatus: HealthStatus.Unhealthy, tags: new string[] { "db", "eventstore" }); - var version = ConfigurationReader.GetValue("AppSettings", "ApiVersion"); - var v = NuGetVersion.Parse(version); - services.AddApiVersioning( - options => - { - // reporting api versions will return the headers "api-supported-versions" and "api-deprecated-versions" - options.ReportApiVersions = true; - options.DefaultApiVersion = new ApiVersion(v.Major, v.Minor, $"Patch{v.Patch}"); - options.AssumeDefaultVersionWhenUnspecified = true; - options.ApiVersionReader = new HeaderApiVersionReader("api-version"); - }); - - services.AddVersionedApiExplorer( - options => - { - // add the versioned api explorer, which also adds IApiVersionDescriptionProvider service - // note: the specified format code will format the version as "'v'major[.minor][-status]" - options.GroupNameFormat = "'v'VVV"; - - // note: this option is only necessary when versioning by url segment. the SubstitutionFormat - // can also be used to control the format of the API version in route templates - options.SubstituteApiVersionInUrl = true; - }); - services.AddTransient, ConfigureSwaggerOptions>(); services.AddSwaggerGen(c => @@ -305,8 +278,7 @@ private void ConfigureMiddlewareServices(IServiceCollection services) } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory, - IApiVersionDescriptionProvider provider) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) { String nlogConfigFilename = "nlog.config"; @@ -350,15 +322,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF }); app.UseSwagger(); - app.UseSwaggerUI( - options => - { - // build a swagger endpoint for each discovered API version - foreach (ApiVersionDescription description in provider.ApiVersionDescriptions) - { - options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant()); - } - }); + app.UseSwaggerUI(); } } } From 34596a6d61473dc468358105d0f33f2580b5c594 Mon Sep 17 00:00:00 2001 From: Stuart Ferguson Date: Sat, 3 Apr 2021 07:58:52 +0100 Subject: [PATCH 2/3] some more changes --- ...ggerOptions.cs => SwaggerDefaultValues.cs} | 41 ------------------- MessagingService/Startup.cs | 15 ++++++- 2 files changed, 14 insertions(+), 42 deletions(-) rename MessagingService/Common/{ConfigureSwaggerOptions.cs => SwaggerDefaultValues.cs} (52%) diff --git a/MessagingService/Common/ConfigureSwaggerOptions.cs b/MessagingService/Common/SwaggerDefaultValues.cs similarity index 52% rename from MessagingService/Common/ConfigureSwaggerOptions.cs rename to MessagingService/Common/SwaggerDefaultValues.cs index 6b2895b..fdee1b1 100644 --- a/MessagingService/Common/ConfigureSwaggerOptions.cs +++ b/MessagingService/Common/SwaggerDefaultValues.cs @@ -3,50 +3,9 @@ using System.Diagnostics.CodeAnalysis; using System.Linq; using Microsoft.AspNetCore.Mvc.ApiExplorer; - using Microsoft.Extensions.Options; using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.SwaggerGen; - /// - /// Configures the Swagger generation options. - /// - /// This allows API versioning to define a Swagger document per API version after the - /// service has been resolved from the service container. - [ExcludeFromCodeCoverage] - public class ConfigureSwaggerOptions : IConfigureOptions - { - #region Methods - - /// - public void Configure(SwaggerGenOptions options) - { - } - - /// - /// Creates the information for API version. - /// - /// The description. - /// - private static OpenApiInfo CreateInfoForApiVersion() - { - OpenApiInfo info = new OpenApiInfo - { - Title = "Messaging API", - Version = "1.0", - Description = "A REST Api to manage sending of various messages over different formats, currently only Email and SMS are supported.", - Contact = new OpenApiContact - { - Name = "Stuart Ferguson", - Email = "golfhandicapping@btinternet.com" - } - }; - - return info; - } - - #endregion - } - /// /// Represents the Swagger/Swashbuckle operation filter used to document the implicit API version parameter. /// diff --git a/MessagingService/Startup.cs b/MessagingService/Startup.cs index 71a8e52..178ab6e 100644 --- a/MessagingService/Startup.cs +++ b/MessagingService/Startup.cs @@ -16,6 +16,7 @@ namespace MessagingService using System.IO; using System.Net.Http; using System.Reflection; + using System.Runtime.Intrinsics; using BusinessLogic.Common; using BusinessLogic.EventHandling; using BusinessLogic.RequestHandlers; @@ -35,6 +36,7 @@ namespace MessagingService using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Options; + using Microsoft.OpenApi.Models; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using NLog.Extensions.Logging; @@ -231,10 +233,21 @@ private void ConfigureMiddlewareServices(IServiceCollection services) failureStatus: HealthStatus.Unhealthy, tags: new string[] { "db", "eventstore" }); - services.AddTransient, ConfigureSwaggerOptions>(); + //services.AddTransient, ConfigureSwaggerOptions>(); services.AddSwaggerGen(c => { + c.SwaggerDoc("v1", new OpenApiInfo + { + Title = "Messaging API", + Version = "1.0", + Description = "A REST Api to manage sending of various messages over different formats, currently only Email and SMS are supported.", + Contact = new OpenApiContact + { + Name = "Stuart Ferguson", + Email = "golfhandicapping@btinternet.com" + } + }); // add a custom operation filter which sets default values c.OperationFilter(); c.ExampleFilters(); From 920395595570094c812d5b8fc6dcccf557457184 Mon Sep 17 00:00:00 2001 From: Stuart Ferguson Date: Sat, 3 Apr 2021 08:11:44 +0100 Subject: [PATCH 3/3] ... --- MessagingService/Startup.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/MessagingService/Startup.cs b/MessagingService/Startup.cs index 178ab6e..7e4e28c 100644 --- a/MessagingService/Startup.cs +++ b/MessagingService/Startup.cs @@ -232,9 +232,7 @@ private void ConfigureMiddlewareServices(IServiceCollection services) name: "Eventstore", failureStatus: HealthStatus.Unhealthy, tags: new string[] { "db", "eventstore" }); - - //services.AddTransient, ConfigureSwaggerOptions>(); - + services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo