Skip to content

Commit 65e75b2

Browse files
Chris Martinezcommonsensesoftware
authored andcommitted
Remove the need for and use of UseApiVersioning
1 parent 86d9b9f commit 65e75b2

File tree

10 files changed

+36
-55
lines changed

10 files changed

+36
-55
lines changed

ApiVersioningWithSamples.sln

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26430.6
4+
VisualStudioVersion = 15.0.26430.14
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{4D5F5F21-0CB7-4B4E-A42F-732BD4AFD0FF}"
77
EndProject
@@ -74,11 +74,11 @@ Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Test.Common", "test\Test.Co
7474
EndProject
7575
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Acceptance.Test.Shared", "test\Acceptance.Test.Shared\Acceptance.Test.Shared.shproj", "{6CDFB878-2642-4F98-AE35-621BAC581181}"
7676
EndProject
77-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ByNamespaceSample", "samples\aspnetcore\ByNamespaceSample\ByNamespaceSample.csproj", "{83B21A5B-0779-4391-9700-58AEFEBFA615}"
77+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ByNamespaceSample", "samples\aspnetcore\ByNamespaceSample\ByNamespaceSample.csproj", "{83B21A5B-0779-4391-9700-58AEFEBFA615}"
7878
EndProject
79-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNet.WebApi.Versioning.ApiExplorer", "src\Microsoft.AspNet.WebApi.Versioning.ApiExplorer\Microsoft.AspNet.WebApi.Versioning.ApiExplorer.csproj", "{91E1F0B5-905D-446C-A2DD-4C1EDABFAF6C}"
79+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNet.WebApi.Versioning.ApiExplorer", "src\Microsoft.AspNet.WebApi.Versioning.ApiExplorer\Microsoft.AspNet.WebApi.Versioning.ApiExplorer.csproj", "{91E1F0B5-905D-446C-A2DD-4C1EDABFAF6C}"
8080
EndProject
81-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNet.WebApi.Versioning.ApiExplorer.Tests", "test\Microsoft.AspNet.WebApi.Versioning.ApiExplorer.Tests\Microsoft.AspNet.WebApi.Versioning.ApiExplorer.Tests.csproj", "{19A2C130-46B4-4CA3-B655-B7547BC414AC}"
81+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNet.WebApi.Versioning.ApiExplorer.Tests", "test\Microsoft.AspNet.WebApi.Versioning.ApiExplorer.Tests\Microsoft.AspNet.WebApi.Versioning.ApiExplorer.Tests.csproj", "{19A2C130-46B4-4CA3-B655-B7547BC414AC}"
8282
EndProject
8383
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer", "src\Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer\Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer.csproj", "{F7784C3A-5569-4590-AE28-B721C0426045}"
8484
EndProject
@@ -90,9 +90,9 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SwaggerWebApiSample", "samp
9090
EndProject
9191
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SwaggerODataWebApiSample", "samples\webapi\SwaggerODataWebApiSample\SwaggerODataWebApiSample.csproj", "{F3986F7B-AF76-43D1-A44F-303023A08CD3}"
9292
EndProject
93-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNet.OData.Versioning.ApiExplorer", "src\Microsoft.AspNet.OData.Versioning.ApiExplorer\Microsoft.AspNet.OData.Versioning.ApiExplorer.csproj", "{1B255310-A2B7-437F-804F-6E1D8C940A17}"
93+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNet.OData.Versioning.ApiExplorer", "src\Microsoft.AspNet.OData.Versioning.ApiExplorer\Microsoft.AspNet.OData.Versioning.ApiExplorer.csproj", "{1B255310-A2B7-437F-804F-6E1D8C940A17}"
9494
EndProject
95-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNet.OData.Versioning.ApiExplorer.Tests", "test\Microsoft.AspNet.OData.Versioning.ApiExplorer.Tests\Microsoft.AspNet.OData.Versioning.ApiExplorer.Tests.csproj", "{3B7E0FEF-8019-4A17-A55F-A6FA378DA856}"
95+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNet.OData.Versioning.ApiExplorer.Tests", "test\Microsoft.AspNet.OData.Versioning.ApiExplorer.Tests\Microsoft.AspNet.OData.Versioning.ApiExplorer.Tests.csproj", "{3B7E0FEF-8019-4A17-A55F-A6FA378DA856}"
9696
EndProject
9797
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Common.ApiExplorer", "src\Common.ApiExplorer\Common.ApiExplorer.shproj", "{26A67334-F6E6-49B8-8C5A-F88F28770966}"
9898
EndProject

samples/aspnetcore/BasicSample/Startup.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public void Configure( IApplicationBuilder app, IHostingEnvironment env, ILogger
3939
loggerFactory.AddConsole( Configuration.GetSection( "Logging" ) );
4040
loggerFactory.AddDebug();
4141
app.UseMvc();
42-
app.UseApiVersioning();
4342
}
4443
}
4544
}

samples/aspnetcore/ByNamespaceSample/Startup.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public void Configure( IApplicationBuilder app, IHostingEnvironment env, ILogger
2828
}
2929

3030
app.UseMvc();
31-
app.UseApiVersioning();
3231
}
3332
}
3433
}

samples/aspnetcore/ConventionsSample/Startup.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public void Configure( IApplicationBuilder app, IHostingEnvironment env, ILogger
5454
loggerFactory.AddConsole( Configuration.GetSection( "Logging" ) );
5555
loggerFactory.AddDebug();
5656
app.UseMvc();
57-
app.UseApiVersioning();
5857
}
5958
}
6059
}

samples/aspnetcore/SwaggerSample/Startup.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ public void Configure( IApplicationBuilder app, IHostingEnvironment env, ILogger
8484
loggerFactory.AddDebug();
8585

8686
app.UseMvc();
87-
app.UseApiVersioning();
8887
app.UseSwagger();
8988
app.UseSwaggerUI(
9089
options =>

src/Microsoft.AspNetCore.Mvc.Versioning/Microsoft.Extensions.DependencyInjection/IApplicationBuilderExtensions.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/Microsoft.AspNetCore.Mvc.Versioning/Microsoft.Extensions.DependencyInjection/IServiceCollectionExtensions.cs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using AspNetCore.Mvc.Routing;
66
using AspNetCore.Mvc.Versioning;
77
using Extensions;
8+
using Microsoft.AspNetCore.Builder;
9+
using Microsoft.AspNetCore.Hosting;
810
using Options;
911
using System;
1012
using System.Diagnostics.Contracts;
@@ -43,26 +45,38 @@ public static IServiceCollection AddApiVersioning( this IServiceCollection servi
4345
services.Add( Singleton<IOptions<ApiVersioningOptions>>( new OptionsWrapper<ApiVersioningOptions>( options ) ) );
4446
services.Replace( Singleton<IActionSelector, ApiVersionActionSelector>() );
4547
services.TryAddSingleton<IApiVersionRoutePolicy, DefaultApiVersionRoutePolicy>();
48+
services.AddTransient<IStartupFilter, InjectApiVersionRoutePolicy>();
49+
services.AddMvcCore( mvcOptions => AddMvcOptions( mvcOptions, options ) );
50+
services.AddRouting( routeOptions => routeOptions.ConstraintMap.Add( "apiVersion", typeof( ApiVersionRouteConstraint ) ) );
51+
52+
return services;
53+
}
54+
55+
static void AddMvcOptions( MvcOptions mvcOptions, ApiVersioningOptions options )
56+
{
57+
Contract.Requires( mvcOptions != null );
58+
Contract.Requires( options != null );
4659

4760
if ( options.ReportApiVersions )
4861
{
49-
services.AddSingleton<ReportApiVersionsAttribute>();
62+
mvcOptions.Filters.Add( new ReportApiVersionsAttribute() );
5063
}
5164

52-
services.AddMvcCore(
53-
mvcOptions =>
54-
{
55-
if ( options.ReportApiVersions )
56-
{
57-
mvcOptions.Filters.Add( typeof( ReportApiVersionsAttribute ) );
58-
}
59-
60-
mvcOptions.Conventions.Add( new ApiVersionConvention( options.DefaultApiVersion, options.Conventions ) );
61-
} );
65+
mvcOptions.Conventions.Add( new ApiVersionConvention( options.DefaultApiVersion, options.Conventions ) );
66+
}
6267

63-
services.AddRouting( routeOptions => routeOptions.ConstraintMap.Add( "apiVersion", typeof( ApiVersionRouteConstraint ) ) );
68+
sealed class InjectApiVersionRoutePolicy : IStartupFilter
69+
{
70+
public Action<IApplicationBuilder> Configure( Action<IApplicationBuilder> next )
71+
{
72+
Contract.Requires( next != null );
6473

65-
return services;
74+
return app =>
75+
{
76+
next( app );
77+
app.UseRouter( builder => builder.Routes.Add( builder.ServiceProvider.GetRequiredService<IApiVersionRoutePolicy>() ) );
78+
};
79+
}
6680
}
6781
}
6882
}

test/Microsoft.AspNetCore.Mvc.Acceptance.Tests/AcceptanceTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected virtual void Dispose( bool disposing )
6666
TestServer CreateServer()
6767
{
6868
var builder = new WebHostBuilder()
69-
.Configure( app => app.UseMvc( OnConfigureRoutes ).UseMvcWithDefaultRoute().UseApiVersioning() )
69+
.Configure( app => app.UseMvc( OnConfigureRoutes ).UseMvcWithDefaultRoute() )
7070
.ConfigureServices( OnConfigureServices )
7171
.UseContentRoot( GetContentRoot() );
7272

test/Microsoft.AspNetCore.Mvc.Versioning.Tests/Microsoft.Extensions.DependencyInjection/IServiceCollectionExtensionsTest.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ public void add_api_versioning_should_configure_mvc_with_custom_options()
6969
services.Single( sd => sd.ServiceType == typeof( IApiVersionReader ) ).ImplementationInstance.Should().NotBeNull();
7070
services.Single( sd => sd.ServiceType == typeof( IApiVersionSelector ) ).ImplementationInstance.Should().BeOfType<ConstantApiVersionSelector>();
7171
services.Single( sd => sd.ServiceType == typeof( IActionSelector ) ).ImplementationType.Should().Be( typeof( ApiVersionActionSelector ) );
72-
services.Single( sd => sd.ServiceType == typeof( ReportApiVersionsAttribute ) ).ImplementationType.Should().Be( typeof( ReportApiVersionsAttribute ) );
73-
mvcOptions.Filters.OfType<TypeFilterAttribute>().Single().ImplementationType.Should().Be( typeof( ReportApiVersionsAttribute ) );
72+
mvcOptions.Filters.Single().Should().BeOfType<ReportApiVersionsAttribute>();
7473
mvcOptions.Conventions.Single().Should().BeOfType<ApiVersionConvention>();
7574
routeOptions.ConstraintMap["apiVersion"].Should().Be( typeof( ApiVersionRouteConstraint ) );
7675
}

test/Microsoft.AspNetCore.Mvc.Versioning.Tests/Versioning/WebServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public WebServer( Action<ApiVersioningOptions> setupApiVersioning = null, Action
3131
}
3232

3333
var hostBuilder = new WebHostBuilder()
34-
.Configure( app => app.UseMvc( setupRoutes ).UseApiVersioning() )
34+
.Configure( app => app.UseMvc( setupRoutes ) )
3535
.ConfigureServices(
3636
services =>
3737
{

0 commit comments

Comments
 (0)