From 1e807240ea4f089ddab19a75cbedd34c69d272b0 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Mon, 23 Jul 2018 13:46:22 +1200 Subject: [PATCH] Add GetChangeToken to EndpointDataSource (#649) --- .../CompositeEndpointDataSource.cs | 4 ++-- .../DataSourceDependentCache.cs | 2 +- src/Microsoft.AspNetCore.Routing/EndpointDataSource.cs | 10 +++++++++- .../RouteValuesBasedEndpointFinder.cs | 8 ++++---- .../CompositeEndpointDataSourceTest.cs | 6 +++--- 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/Microsoft.AspNetCore.Routing/CompositeEndpointDataSource.cs b/src/Microsoft.AspNetCore.Routing/CompositeEndpointDataSource.cs index a0009a7..bb4836d 100644 --- a/src/Microsoft.AspNetCore.Routing/CompositeEndpointDataSource.cs +++ b/src/Microsoft.AspNetCore.Routing/CompositeEndpointDataSource.cs @@ -69,8 +69,8 @@ namespace Microsoft.AspNetCore.Routing foreach (var dataSource in _dataSources) { Extensions.Primitives.ChangeToken.OnChange( - () => dataSource.ChangeToken, - () => HandleChange()); + dataSource.GetChangeToken, + HandleChange); } } } diff --git a/src/Microsoft.AspNetCore.Routing/DataSourceDependentCache.cs b/src/Microsoft.AspNetCore.Routing/DataSourceDependentCache.cs index 46ecba5..8df45ef 100644 --- a/src/Microsoft.AspNetCore.Routing/DataSourceDependentCache.cs +++ b/src/Microsoft.AspNetCore.Routing/DataSourceDependentCache.cs @@ -47,7 +47,7 @@ namespace Microsoft.AspNetCore.Routing { lock (_lock) { - var changeToken = _dataSource.ChangeToken; + var changeToken = _dataSource.GetChangeToken(); _value = _initializeCore(_dataSource.Endpoints); changeToken.RegisterChangeCallback(_initializerWithState, null); diff --git a/src/Microsoft.AspNetCore.Routing/EndpointDataSource.cs b/src/Microsoft.AspNetCore.Routing/EndpointDataSource.cs index a102ff2..74c79c3 100644 --- a/src/Microsoft.AspNetCore.Routing/EndpointDataSource.cs +++ b/src/Microsoft.AspNetCore.Routing/EndpointDataSource.cs @@ -8,7 +8,15 @@ namespace Microsoft.AspNetCore.Routing { public abstract class EndpointDataSource { - public abstract IChangeToken ChangeToken { get; } + public virtual IChangeToken ChangeToken { get; } + + // Plan is to replace ChangeToken property with GetChangeToken + // Temporarily have both to avoid breaking MVC + // https://github.com/aspnet/Routing/issues/634 + public virtual IChangeToken GetChangeToken() + { + return ChangeToken; + } public abstract IReadOnlyList Endpoints { get; } } diff --git a/src/Microsoft.AspNetCore.Routing/RouteValuesBasedEndpointFinder.cs b/src/Microsoft.AspNetCore.Routing/RouteValuesBasedEndpointFinder.cs index d124568..eb51c49 100644 --- a/src/Microsoft.AspNetCore.Routing/RouteValuesBasedEndpointFinder.cs +++ b/src/Microsoft.AspNetCore.Routing/RouteValuesBasedEndpointFinder.cs @@ -32,8 +32,8 @@ namespace Microsoft.AspNetCore.Routing // Register for changes in endpoints Extensions.Primitives.ChangeToken.OnChange( - () => _endpointDataSource.ChangeToken, - () => HandleChange()); + _endpointDataSource.GetChangeToken, + HandleChange); } public IEnumerable FindEndpoints(RouteValuesBasedEndpointFinderContext context) @@ -70,8 +70,8 @@ namespace Microsoft.AspNetCore.Routing // re-register the callback as the change token is one time use only and a new change token // is produced every time Extensions.Primitives.ChangeToken.OnChange( - () => _endpointDataSource.ChangeToken, - () => HandleChange()); + _endpointDataSource.GetChangeToken, + HandleChange); } private void BuildOutboundMatches() diff --git a/test/Microsoft.AspNetCore.Routing.Tests/CompositeEndpointDataSourceTest.cs b/test/Microsoft.AspNetCore.Routing.Tests/CompositeEndpointDataSourceTest.cs index 74a975b..52ba52d 100644 --- a/test/Microsoft.AspNetCore.Routing.Tests/CompositeEndpointDataSourceTest.cs +++ b/test/Microsoft.AspNetCore.Routing.Tests/CompositeEndpointDataSourceTest.cs @@ -114,7 +114,7 @@ namespace Microsoft.AspNetCore.Routing var endpoints = compositeDataSource.Endpoints; // Assert1 - var changeToken1 = compositeDataSource.ChangeToken; + var changeToken1 = compositeDataSource.GetChangeToken(); var token = Assert.IsType(changeToken1); Assert.False(token.HasChanged); // initial state @@ -126,7 +126,7 @@ namespace Microsoft.AspNetCore.Routing // Assert2 Assert.True(changeToken1.HasChanged); // old token is expected to be changed - var changeToken2 = compositeDataSource.ChangeToken; // new token is in a unchanged state + var changeToken2 = compositeDataSource.GetChangeToken(); // new token is in a unchanged state Assert.NotSame(changeToken2, changeToken1); token = Assert.IsType(changeToken2); Assert.False(token.HasChanged); @@ -139,7 +139,7 @@ namespace Microsoft.AspNetCore.Routing // Assert2 Assert.True(changeToken2.HasChanged); // old token is expected to be changed - var changeToken3 = compositeDataSource.ChangeToken; // new token is in a unchanged state + var changeToken3 = compositeDataSource.GetChangeToken(); // new token is in a unchanged state Assert.NotSame(changeToken3, changeToken2); Assert.NotSame(changeToken3, changeToken1); token = Assert.IsType(changeToken3);